Files
RPG_curses/menus.py
2025-05-18 11:48:19 +08:00

78 lines
2.7 KiB
Python

# menus.py
import curses
from config import difficulty
def main_menu(stdscr):
options = ["Start Game","Settings","Credits","Exit"]
sel=0
while True:
stdscr.clear()
h,w=stdscr.getmaxyx()
if h<20 or w<50:
stdscr.addstr(h//2,w//2-10,"Terminal too small!")
stdscr.refresh(); stdscr.getch(); return -1
# ASCII 标题
draw_title(stdscr,h,w)
for i,opt in enumerate(options):
x=w//2-len(opt)//2-2; y=h//2+i
if i==sel:
stdscr.addstr(y,x,f"> {opt} <",curses.color_pair(2))
else:
stdscr.addstr(y,x,f" {opt} ",curses.color_pair(3))
k=stdscr.getch()
if k in (ord('w'),curses.KEY_UP): sel=(sel-1)%len(options)
elif k in (ord('s'),curses.KEY_DOWN): sel=(sel+1)%len(options)
elif k==ord('\n'): return sel
def draw_title(stdscr,h,w):
title=r"""
_______ _______ _______
( ____ \( ___ )( ____ \
| ( \/| ( ) || ( \/
| | | | | || (__
| | | | | || __)
| | | | | || (
| (____/\| (___) || (____/\
(_______/(_______)(_______/
"""
for i,line in enumerate(title.splitlines()):
stdscr.addstr(h//2-10+i, w//2-len(line)//2, line, curses.color_pair(4))
def start_game_menu(stdscr):
return _generic(stdscr,["Infinite World","Maze","Back"])
def settings_menu(stdscr):
global difficulty
opts=["Easy","Normal","Hard"]; idx=opts.index(difficulty)
while True:
stdscr.clear(); h,w=stdscr.getmaxyx()
stdscr.addstr(h//2-2,w//2-7,"Select Difficulty",curses.color_pair(4))
for i,d in enumerate(opts):
x=w//2-len(d)//2; y=h//2+i
if i==idx: stdscr.addstr(y,x,f"> {d} <",curses.color_pair(2))
else: stdscr.addstr(y,x,f" {d} ",curses.color_pair(3))
k=stdscr.getch()
if k in (ord('w'),curses.KEY_UP): idx=(idx-1)%3
elif k in (ord('s'),curses.KEY_DOWN): idx=(idx+1)%3
elif k==ord('\n'): difficulty=opts[idx]; return
def credits_menu(stdscr):
stdscr.clear(); h,w=stdscr.getmaxyx()
msg="Made by 陈坤阳"
stdscr.addstr(h//2,w//2-len(msg)//2,msg,curses.color_pair(4))
stdscr.refresh(); stdscr.getch()
def _generic(stdscr,opts):
sel=0
while True:
stdscr.clear(); h,w=stdscr.getmaxyx()
for i,o in enumerate(opts):
x=w//2-len(o)//2; y=h//2+i
if i==sel: stdscr.addstr(y,x,f"> {o} <",curses.color_pair(2))
else: stdscr.addstr(y,x,f" {o} ",curses.color_pair(3))
k=stdscr.getch()
if k in (ord('w'),curses.KEY_UP): sel=(sel-1)%len(opts)
elif k in (ord('s'),curses.KEY_DOWN): sel=(sel+1)%len(opts)
elif k==ord('\n'): return sel