在Python中启用退出功能的问题

0 投票
4 回答
613 浏览
提问于 2025-04-15 18:11

我对编程还很陌生,现在正在为我妹妹制作一个小游戏...

我有一个循环,想要在里面设置一个退出游戏的选项,但我知道的退出方法都不管用:

#main game:
while 1:
    input_event_1 = gui.buttonbox(
        msg = 'Hello, what would you like to do with your Potato Head?',
        title = 'Main Screen',
        choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
    if input_event_1 == 'Check Stats':
        myPotatoHead.check_p_h_stats()
    if input_event_1 == 'Change Favourite Thing':
        myPotatoHead.change_favourite_thing()
    if input_quit == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',
        title = 'Confirm quit',
        choices = ('Quit', 'Cancel'))
        if input_event_quit == 'Quit':
            sys.exit(1)

非常感谢任何帮助。

谢谢


-----更新-----

感谢你的建议,但还是不行:

这是更新后的代码:

#import the required modules:
import easygui as gui
import os


#-----CLASS-----------------------------------
#Class:
class PotatoHead:

#Atributes:
    def __init__(self):
        self.data = game_data
        self.first_name = self.data[0]
        self.last_name = self.data[1]
        self.gender = self.data[2]
        self.colour = self.data[3]
        self.fav_thing = self.data[4]
        self.toys = []
        self.toys.append(self.data[5])
        self.age = '0.0'
        self.hunger = '0.0'
        self.health = '0.0'
        self.fitness = '0.0'
        self.education = '0.0'
        self.happiness = '0.0'
        self.tiredness = '0.0'

    def check_p_h_stats(self):
        self.toys_string = str(self.toys)
        gui.msgbox(
            msg = '''
Name: ''' + self.first_name + ' ' + self.last_name + '''
Gender: ''' + self.gender + '''
Colour: ''' + self.colour + '''
Favourite Thing: ''' + self.colour + '''
Toys:''' + self.toys_string + '''
Age: ''' + self.age + '''
Hunger: ''' + self.hunger + '''
Health: ''' + self.health + '''
Fitness: ''' + self.fitness + '''
Education: ''' + self.education + '''
Happiness: ''' + self.happiness + '''
Tiredness: ''' + self.tiredness + '''
''',
            title = 'Potato Head Stats',
            ok_button = 'Continue')

    def change_favourite_thing(self):
        new_fav_thing = gui.enterbox(
            msg = 'Enter the new favourite thing:',
            title = 'Change Favourite Thing',
            default = 'Type Here')
        self.fav_thing = new_fav_thing

#Methods:
#-----MAIN PROGRAM----------------------------
#set up game:
image = 'nest.gif'
game_choice = gui.ynbox(
    msg = """Would you like to start a new game,
or load a previously saved one?""",
    title = 'Start/Load Game',
    choices = ('New Game', 'Load Game'),
    image = image)
if game_choice == 1:
    fieldNames = ['First Name', 'Last Name', 'Gender', 'Colour', 'Favourite Thing',     'First Toy']
    new_p_head_data = []
    new_p_head_data = gui.multenterbox(
        msg = 'Fill in the starting information about your Potato Head:',
        title = 'New Game',
        fields = fieldNames,
        values = ('', '', 'Male/Female', 'Red, Green, Blue, Yellow, White, Black', '',     'Choose either Rattle, Pacifier, Teddy, Doll, or Soft Ball'))
    game_data = new_p_head_data
else:
    gui.msgbox('This function is not yet supported, please start again...')

myPotatoHead = PotatoHead()

#main game:
while 1:
    input_event_1 = gui.buttonbox(
        msg = 'Hello, what would you like to do with your Potato Head?',
        title = 'Main Screen',
        choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
    if input_event_1 == 'Check Stats':
        myPotatoHead.check_p_h_stats()
    elif input_event_1 == 'Change Favourite Thing':
        myPotatoHead.change_favourite_thing()
    elif input_event_1 == 'Quit':
        input_quit = gui.ynbox(
            msg = 'Are you sure you want to quit?',
            title = 'Confirm quit',
            choices = ('Quit', 'Cancel'))
        if input_quit == 'Quit':
            sys.exit(1)

我在Mac上使用的是Python 2.5.4,使用的是Easygui 0.83

再次感谢任何建议

4 个回答

0

这样做更优雅:

she_wants_to_play = true
while she_wants_to_play:
    ask input
    if input == quit:
        she_wants_to_play = false

这样你就能优雅地退出循环,而不是像用一吨重的东西去撞击系统。

2
 if input_event_1 == 'Quit':

我觉得你的代码可能是

 if input_quit == 'Quit':

应该像你其他的检查那样?我对这个 buttonbox 的东西不太熟悉,但这一条检查和其他的差别太明显了,一眼就能看出来...

编辑:哦,找到了,它来自 easygui(提到你使用的框架会很有帮助!)-- 是的,它返回与按钮相关的字符串,所以我上面建议的改动绝对是关键点。

另外,把第一个 if input_event_1 之后的所有检查改成 elif input_event_1(因为它不可能同时等于多个字符串,既然你已经找到了那个等于的字符串,为什么还要继续检查呢?)虽然不是绝对必要,但这样改进会更好。

2

我觉得这就是你的问题:

if input_quit == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',

应该改成

if input_event_1 == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',

补充说明:根据EasyGui的教程,它之所以还是不工作,是因为 ynbox 返回的是0或1,而不是你选择的字符串值。所以要改成

    elif input_event_1 == 'Quit':
        input_quit = gui.ynbox(
            msg = 'Are you sure you want to quit?',
            title = 'Confirm quit',
            choices = ('Quit', 'Cancel'))
        if input_quit == 1:
            sys.exit(1)

撰写回答