-1

我已经对代码进行了一些编辑,这个新代码是否可以工作,如果不是,我应该怎么做才能让它工作

import winsound

import tkinter as tk
from functools import partial # a quick way to make a callback function
import choose_your_own_adventure_game_pt2 as AG
import time
winsound.PlaySound('Adrenaline - Intense Suspense Music (No Copyright).wav', winsound.SND_ASYNC)
print('welcome to the')
AG.title()
time.sleep(1)
print('please read each line carefuly once the gui launches')
time.sleep(13)


class Situation(tk.Frame):
    
    def __init__(self, master=None, story='',song=[], buttons=[], **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        story_lbl = tk.Label(self, text=story, justify=tk.LEFT, anchor=tk.NW, font=("Play", 10))
        story_lbl.pack()
        
        for btn_text, new_situation in buttons:
            btn = tk.Button(self, text=btn_text, command=partial(self.quit_, new_situation),padx=10, pady=10)
            btn.pack()
        def song_startup(self, new_sound):
            
            winsound.PlaySound(btn_song_filename, winsound.SND_ASYNC|winsound.SND_FILENAME)
        
            for btn_song_filename, new_sound in song:
                btn_song = tk.Button(self, text=btn_song_filename, command=partial(self.song_startup, new_sound))
                btn_song.pack()

    def quit_(self, new_situation):
        self.destroy()
        load(new_situation)

def load(situation=None):
    frame = Situation(root, **SITUATIONS.get(situation))
    frame.pack()

SITUATIONS = {
    
    None:{
        'song':
        'intro to the game.wav',
        'story':
        
"""



hello player you have been chosen to embark on your first adventure,

this as a button based game so all your decisions are based on a press of a button,

you will play as a character named Sarah who has a wolf goddess inside her, the goddess' name is Winter,

you as Sarah will have the powers given to you by Winter to get through this game but if you use too much
of that power at any given time your game will end,

pretty soon you will be asked a series of questions your job is to choose the answer that best suits your style of playing the game

but be warned you may end up loosing in the end, so without furthur addo lets start this thing

welcome to 
                                      
MOON LANDING EXPIDITION                                             
                                             
male scientist:Hello Sarah can you hear me?


""",

        'buttons':[
            ('YES', 'situation_2'),
            
            ('NO','situation_2_1')
            ]
        },
    'situation_2':{
        'story':
"""


male scientist:good... do you know where you are?
        
        
        
""",
'song':[

('backgroundmusic.wav')
],
        'buttons' :[
            ('yes', 'situation_3'),
            
            ('no', 'situation_3_1')
            ]                  
        },
    'situation_2_1':{
        'story':
"""

male scientist: hmm if you can't hear me
then you wouldn't have responded


""",
        'buttons':[
            ('-_-',None)
            
            ]
        
        },
    'situation_3':{
        'story':
"""

male scientist:alright so do you know why you are here?
""",
         'buttons':[
             ('yes','situation_4'),

             ('no','situation_4_1')
             
             ]
        
        },
    'situation_3_1':{
        'story':
"""

male scientist: you are in a top secret military space center
""",
       'buttons':[
           ('alright','situation_3')
           
           ]    
        },
    'situation_4':{
        'story':
"""
male scientist:well then you seem to have the rare ability to read people's minds

(a second scientist enters the room and starts talking to the first on the other side of the glass)

female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location

Dr. Smith: alright Susan we need to transport Sarah to the space explorer

Susan:Yes Sir

(Susan looks in your direction)

Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
     'buttons':[
         ('what is going on?','situation_5'),
         ('do not press', 'never_gonna_give_you_up'),
         ('am i in danger?','situation_5_1'),
         ('ok','situation_5_2')
         
         ],    
        },
            'situation_4_1':{
        'story':
"""
male scientist:you were chosen out of five million individuals to take part in a space exploration mission to the planet nexu...

(a second scientist enters the room and starts talking to the first on the other side of the glass)

female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location

Dr. Smith: alright Susan we need to transport Sarah to the space explorer

Susan:Yes Sir

(Susan looks in your direction)

Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
     'buttons':[
         ('what is going on?','situation_5'),
         ('am i in danger?','situation_5_1'),
         ('ok','situation_5_2')
         
         ],    
        },    
            'never_gonna_give_you_up':{
                'story':
"""
were no strangers to love...


you know the rules and so do i...

iiiiiiiii just wanna tell you how im feeling 

gotta make you UNDERSTAND!!!

never gonna give you up never gonna let you down 

*rick astly.exe has sotpped working you will die now*
""",
    'buttons':[
        ('death', None)
        
        ],
  
                },
    }
def beginning():
    start_button.destroy()
    load() # load the first story
    winsound.PlaySound(None, winsound.SND_ASYNC)
#WINDOW
root = tk.Tk()
root.geometry('500x500-500-300')
root.title('Moon landing expedition')
root.attributes('-fullscreen', True)
#START
start_button = tk.Button(root, text="START", command=beginning)
start_button.place(relx=.5, rely=.5, anchor='c')




#THE LOOP
root.mainloop()

感谢所有帮助我让我的代码正常工作的人我非常感谢我收到的所有帮助,我希望你们知道我会尝试每一个建议。我希望我能让我的代码最终工作,因为让它工作对我来说真的很重要

4

1 回答 1

0

song在每种情况下添加一个项目,例如:

'situation_2': {
    'story': ...
    'song': 'situation_2.wav',
    'buttons': [...]
},

然后添加song参数Situation.__init__()并使用以下方法播放歌曲PlaySound()

class Situation(tk.Frame):
    def __init__(self, master=None, story='', buttons=[], song=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        story_lbl = tk.Label(self, text=story, justify=tk.LEFT, anchor=tk.NW, font=("Play", 10))
        story_lbl.pack()
        
        for btn_text, new_situation in buttons:
            btn = tk.Button(self, text=btn_text, command=partial(self.quit_, new_situation), padx=10, pady=10)
            btn.pack()

        if song:
            winsound.PlaySound(song, winsound.SND_ASYNC)
于 2021-01-15T16:05:14.297 回答