0

私は一般的にプログラミングにかなり慣れていないので、妹のために小さなゲームを作成しています...

ゲームを終了するオプションが必要なwhileループがありますが、私が知っている終了テクニックはどれも機能していないようです:

#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

4 に答える 4

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)
于 2010-01-19T02:04:28.690 に答える
2

たぶんあなたのコードだと思います

 if input_quit == 'Quit':

する必要があります

 if input_event_1 == 'Quit':

他の小切手と同じように?私はそのことに慣れていませんbuttonboxが、この1つのチェックと他のチェックの不一致はすぐに不快になり、目に飛び込みます...

編集: ああ、見つけました、それはeasyguiからのものです (使用しているフレームワークについて言及すると役立つでしょう!-) -- そうです、ボタンに関連付けられた文字列を返すので、上で提案した変更は間違いなく重要なポイントです。

また、if input_event_1最初の文字列の後のすべてを(複数の文字列と等しくすることはできないelif input_event_1ため、等しい文字列を見つけたら、なぜチェックし続ける必要があるのでしょうか?-) に変更することは、厳密には必要ではありませんが、改善。

于 2010-01-19T02:05:44.330 に答える
0

この方法はよりエレガントです。

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

したがって、システムに 10000 トンの重みを与える代わりに、while ループを適切に終了します。

于 2010-01-19T02:04:35.270 に答える
0

いくつかのチェックポイント

  • sysをインポートしましたか?
  • if input_event_quit == 'Quit':する必要がありますinput_quit == 'Quit':
于 2010-01-19T02:05:38.577 に答える