我在tkinter创建一个游戏作为一个学校项目,它崩溃没有错误

时间:2018-04-07 16:04:54

标签: python python-3.x tkinter

我是10年级的GCSE学生,所以我刚刚开始我的课程和正确的编码。我们设置了一项任务来创建一个名为botmod的游戏,您可以控制一个收集人物然后将其返回基地的角色。我使用一般知识创建了游戏,并从Google获得了我不理解的帮助。完成主程序后,我开始创建一个开始菜单,用户可以为游戏设置自己的变量,例如网格大小等。一旦我用主程序实现了这个,这就是问题的开始。 一旦我浏览了两个菜单并选择了所有选项,程序就会停止响应然后崩溃。

我知道这段代码非常混乱,我不应该使用全局变量或至少尽可能地避免使用它们但是我们还没有正确地启动OOP所以我决定不再深入研究它。

提前感谢您的帮助。

from tkinter import *
from random import choice, shuffle

def Ending_Conditions():
    global Player_Power, Players_Collected
    if Players_Collected == 3:
        Game_End("YOU WIN!")
    elif Player_Power <= 0 and Players_Collected != 3:
        Game_End("Game Over")

def Game_End(End_Option):
    global Playing_Game, Game_Finished
    if End_Option == "EXIT":
        Playing_Game = False
        Root.destroy()
    else:
        Game_Finished = True
        Game_Canvas.create_rectangle(120, 120, 480, 300, fill = "#ffffff")
        Game_Canvas.create_text(300, 210, text = End_Option, font = ('Abadi', 35, "bold"))
        Continue_Button = Button(Root, text = 'Continue', command = lambda: Game_Restart('CONTINUE'))
        Exit_Button = Button(Root, text = 'Exit', command = lambda: Game_Restart('EXIT'))
        Continue_Button.pack()
        Exit_Button.pack()

def Game_Restart(Restart_Option):
    if Restart_Option == 'CONTINUE':
        Root.destroy()
        Restart_Game = True
    if Restart_Option == 'EXIT':
        Game_End('EXIT')

def Paused_UI():
    global Game_Paused, Close_PausedUI
    Close_PausedUI = Button(Root, text = 'Exit', command = Close_Paused_UI)
    Close_PausedUI.pack()
    Game_Paused = True

def Close_Paused_UI():
    global Game_Paused, Close_PausedUI
    Close_PausedUI.pack_forget()
    Game_Paused = False

def Generate_Game():
    global Game_Canvas, Game_Player, Game_Base, Player_Power, Power_Counter, People_Locations, Game_Paused, All_People, Game_Grid
    Current_Colour = 0
    Grid_Colour = []
    Player_PosX, Player_PosY, Player_Storage, Players_Collected = 30, 30, 0, 0, 
    People_Locations = {0:[150, 450, 0], 1:[270, 330, 1], 2:[450, 90, 2]}
    Game_Finished = False
    # Generate UI
    Game_Menu = Button(Root, text = 'Menu', command = Paused_UI)
    Game_Menu.pack()
    # Define grid colours
    Grid_Colours = ['#009446', '#D4F0FF', '#5f6365']
    # Generate power counter and pack canvas
    Power_Counter = Label(Root, text = Player_Power)
    Power_Counter.pack()
    Game_Canvas.pack()
    # Generate Grid and Colours
    for y in range(Game_Grid):
        for x in range(Game_Grid):
            shuffle(Grid_Colours)
            Game_Grid = Game_Canvas.create_rectangle(0+60*x, 0+60*y, 60+60*x, 60+60*y, fill = Grid_Colours[0])
            Grid_Colour.append(Grid_Colours[0])
    # Generate Robot Player
    Game_Player = Game_Canvas.create_text(Player_PosX, Player_PosY, text = "R", font = ('Abadi', 35, "bold"))
    # Generate Player Base
    Game_Base = Game_Canvas.create_text(450, 330, text = "B", font = ('Abadi', 35, "bold"))
    # Generate People
    for i in range(Game_People):
        Person_Location = People_Locations[0+i]
        Game_Person = Game_Canvas.create_text(Person_Location[0], Person_Location[1], text = "P", font = ('Abadi', 35, "bold"), tag = 'Game_Person')
    All_People = Game_Canvas.find_withtag('Game_Person')
    All_People = list(All_People)

def Move_Player(event):
    global Game_Canvas, Game_Player, Game_Base, Player_PosY, Player_PosX, Current_Colour, Player_Power, Power_Counter, Player_Storage, Players_Collected, Game_Finished, Current_Person
    if event.char.lower() == '\x1b' and Game_Paused == False:
        Paused_UI()
    elif event.char.lower() == '\x1b' and Game_Paused == True:
        Close_Paused_UI()
    if Game_Paused == False and Game_Finished != True:
        Movement_Value = Movement_Values[event.char.lower()]
        if Player_PosX != Movement_Value[0] and Player_PosY != Movement_Value[1]:
            # Detect current grid colour and configure power accordingly
            Player_Power = Player_Power - Power_Values[Grid_Colour[Current_Colour]]
            Power_Counter.configure(text = Player_Power)
            Current_Colour = Current_Colour + Movement_Value[4]
            # Delete the player then recreate in its new position
            Game_Canvas.delete(Game_Player)
            Player_PosX, Player_PosY = Player_PosX + Movement_Value[2], Player_PosY + Movement_Value[3]
            Game_Player = Game_Canvas.create_text(Player_PosX, Player_PosY, text = "R", font = ('Abadi', 35, "bold"))
            # Detect Person
            for item in People_Locations:
                Person_Location = People_Locations[item]
                if Player_PosX  == Person_Location[0] and Player_PosY == Person_Location[1] and Player_Storage != 1:
                    Current_Person = Person_Location[2]
                    Game_Canvas.delete(All_People[Current_Person])
                    Player_Storage += 1
            if Player_PosX == 450 and Player_PosY == 330:
                if Player_Storage == 1:
                    Player_Storage -= 1
                    Players_Collected += 1
                    Clear_Current = People_Locations[Current_Person]
                    Clear_Current = Clear_Current[2]
                    People_Locations.pop(Clear_Current)
        Ending_Conditions()

def Start_Game():
    global Start_Canvas
    Start_Canvas = Canvas(Root, width = 600, height = 500, bd=0, highlightthickness=0, bg = '#ffffff')
    Start_Canvas.pack()

    Title = Start_Canvas.create_text(300, 163, text = "BOTMOD", font = ('Cinema Gothic BTN Shadow', 75, 'normal'))

    Play_Button = Button(text = "PLAY", width = 10, font=('Arial', 15), command = Play_Game)
    Play_Button = Start_Canvas.create_window(300, 325, window = Play_Button)

    Exit_Button = Button(text = "EXIT", width = 10, font=('Arial', 15), command = lambda: Game_End("EXIT"))
    Exit_Button = Start_Canvas.create_window(300, 375, window = Exit_Button)

def Play_Game():
    global Menu_Canvas, Grid_Entry, Power_Entry, People_Entry
    Start_Canvas.destroy()
    Menu_Canvas = Canvas(Root, width = 600, height = 500, bd=0, highlightthickness=0, bg = '#C0C0C0')
    Menu_Canvas.pack()

    Title_Rectangle = Menu_Canvas.create_rectangle(120, 10, 480, 100, fill = '#000000')
    Slider_Rectangle = Menu_Canvas.create_rectangle(120, 110, 480, 430, fill = '#000000')
    Footer_Rectangle = Menu_Canvas.create_rectangle(120, 440, 480, 490, fill = '#000000')

    Title = Menu_Canvas.create_text(300, 55, text = "BOTMOD", font = ('Cinema Gothic BTN Shadow', 55, 'normal'), fill = '#ffffff')

    Description_Text = Label(text = "Set Game Options", font=('Arial', 15, 'bold'), bg = '#000000', fg = '#ffffff')
    Description_Text = Menu_Canvas.create_window(300, 145, window = Description_Text)

    Power_Text = Label(text = "Robot Power", font=('Cinema Gothic BTN Shadow', 15), bg = '#000000', fg = '#ffffff')
    Power_Text = Menu_Canvas.create_window(300, 195, window = Power_Text)
    Power_Entry = Scale(from_= 50, to = 350, orient = HORIZONTAL, bg = '#000000', fg = '#ffffff', highlightthickness=0, bd = 0)
    Power_Entry = Menu_Canvas.create_window(300, 225, window = Power_Entry)

    People_Text = Label(text = "Person Count", font=('Cinema Gothic BTN Shadow', 15), bg = '#000000', fg = '#ffffff')
    People_Text = Menu_Canvas.create_window(300, 280, window = People_Text)
    People_Entry = Scale(from_= 3, to = 10, orient = HORIZONTAL, bg = '#000000', fg = '#ffffff', highlightthickness=0, bd = 0, state=DISABLED)
    People_Entry = Menu_Canvas.create_window(300, 310, window = People_Entry)

    Grid_Text = Label(text = "Grid Count", font=('Cinema Gothic BTN Shadow', 15), bg = '#000000', fg = '#ffffff')
    Grid_Text = Menu_Canvas.create_window(300, 365, window = Grid_Text)
    Grid_Entry = Scale(from_= 5, to = 15, orient = HORIZONTAL, bg = '#000000', fg = '#ffffff', highlightthickness=0, bd = 0)
    Grid_Entry = Menu_Canvas.create_window(300, 395, window = Grid_Entry)

    Play_Button = Button(text = "OK", width = 15, font=('Cinema Gothic BTN Shadow', 10), command = lambda: Init_Game("DEFAULT"), bg = '#000000', fg = '#ffffff')
    Play_Button = Menu_Canvas.create_window(400, 465, window = Play_Button)
    PlayWD_Button = Button(text = "Use Defaults", width = 15, font=('Cinema Gothic BTN Shadow', 10), command = lambda: Init_Game("CUSTOM"), bg = '#000000', fg = '#ffffff')
    PlayWD_Button = Menu_Canvas.create_window(200, 465, window = PlayWD_Button)

def Init_Game(Game_Options):
    global Grid_Count, Player_PosY, Player_PosX, Game_Paused, Game_Finished, Playing_Game, Player_Power, Game_Grid, Player_Movement, Game_People, Game_Canvas, Menu_Canvas, Grid_Entry, Power_Entry, People_Entry, Grid_Rectangle
    if Game_Options == "DEFAULT":
        Game_Grid = 10
        Grid_Rectangle = 600/10
        Player_Movement = Grid_Rectangle/2
        Player_Power = 150
        Game_People = 3
    else:
        Game_Grid = Grid_Entry.get()
        Grid_Rectangle = 600/Game_Grid
        Player_Movement = Grid_Rectangle/2
        Player_Power = Power_Entry.get()
        Game_People = People_Entry.get()

    Menu_Canvas.destroy()        
    Playing_Game = True
    Game_Paused = False
    Restart_Game = False

    Movement_Values = {'w':(1000, 30, 0, -60, -10), 'a':(30, 1000, -60, 0, -1), 's':(1000, 570, 0, 60, 10), 'd':(570, 1000, 60, 0, 1)}
    Power_Values = {'#009446':2, '#D4F0FF':1, '#5f6365':3}
    People_Locations = {0:[150, 450, 0], 1:[270, 330, 1], 2:[450, 90, 2]}
    while Playing_Game == True:
            Restart_Game = False
            while Restart_Game == False:
                if Playing_Game == False:
                    break
                else:
                    Root = Tk()
                    Game_Canvas = Canvas(Root, width = 600, height = 600, bd=0, highlightthickness=0)
                    Generate_Game()
                    Root.bind('<KeyPress>', Move_Player)                  
                    Root.protocol("WM_DELETE_WINDOW", lambda: Game_End("EXIT"))
            if Playing_Game == False:
                break

Root = Tk()
Start_Game()
Root.mainloop()

1 个答案:

答案 0 :(得分:0)

今天看了一下,事实证明底部的循环是导致问题不是100%确定我做了什么,因为它是试验和错误但我仍然会发布代码。

此代码尚未完全完成。

from tkinter import *
from random import choice, shuffle

def Ending_Conditions():
    global Player_Power, Players_Collected
    if Players_Collected == 3:
        Game_End("YOU WIN!")
    elif Player_Power <= 0 and Players_Collected != 3:
        Game_End("Game Over")

def Game_End(End_Option):
    global Playing_Game, Game_Finished
    if End_Option == "EXIT":
        Playing_Game = False
        Root.destroy()
    else:
        Game_Finished = True
        Game_Canvas.create_rectangle(120, 120, 480, 300, fill = "#ffffff")
        Game_Canvas.create_text(300, 210, text = End_Option, font = ('Abadi', 35, "bold"))
        Continue_Button = Button(Root, text = 'Continue', command = Game_Restart)
        Exit_Button = Button(Root, text = 'Exit', command = lambda: Game_End('EXIT'))
        Continue_Button.pack()
        Exit_Button.pack()

def Game_Restart():
    global Already_Played
    Root.destroy()
    Restart_Game = True
    Already_Played = True

def Paused_UI():
    global Game_Paused, Close_PausedUI
    Close_PausedUI = Button(Root, text = 'Exit', command = Close_Paused_UI)
    Close_PausedUI.pack()
    Game_Paused = True

def Close_Paused_UI():
    global Game_Paused, Close_PausedUI
    Close_PausedUI.pack_forget()
    Game_Paused = False

def Generate_Game():
    global Game_Canvas, Game_Player, Game_Base, Player_Power, Power_Counter, People_Locations, Game_Paused, All_People, Game_Gridcount, Current_Colour, Player_PosY, Player_PosX, Grid_Colour, Players_Collected, Player_Storage
    Current_Colour = 0
    Grid_Colour = []
    Player_PosX, Player_PosY, Player_Storage, Players_Collected, Players_Removed = 300/Game_Gridcount, 300/Game_Gridcount, 0, 0, 0
    People_Locations = {0:[150, 450, 0], 1:[270, 330, 1], 2:[450, 90, 2]}
    Game_Finished = False
    # Generate UI
    Game_Menu = Button(Root, text = 'Menu', command = Paused_UI)
    Game_Menu.pack()
    # Define grid colours
    Grid_Colours = ['#009446', '#D4F0FF', '#5f6365']
    # Generate power counter and pack canvas
    Power_Counter = Label(Root, text = Player_Power)
    Power_Counter.pack()
    Game_Canvas.pack()
    # Generate Grid and Colours
    for y in range(Game_Gridcount):
        for x in range(Game_Gridcount):
            shuffle(Grid_Colours)
            Game_Grid = Game_Canvas.create_rectangle(Grid_Rectangle*x, Grid_Rectangle*y, Grid_Rectangle+Grid_Rectangle*x, Grid_Rectangle+Grid_Rectangle*y, fill = Grid_Colours[0])
            Grid_Colour.append(Grid_Colours[0])
    # Generate Robot Player
    Game_Player = Game_Canvas.create_text(Player_PosX, Player_PosY, text = "R", font = ('Abadi', 450//Game_Gridcount, "bold"))
    # Generate Player Base
    Game_BaseX, Game_BaseY = Grid_Rectangle*(Game_Gridcount-1.5), Grid_Rectangle*(Game_Gridcount-2.5)
    Game_Base = Game_Canvas.create_text(Game_BaseX, Game_BaseY, text = "B", font = ('Abadi', 450//Game_Gridcount, "bold"))
    # Generate People
    for i in range(Game_People):
        Person_Location = People_Locations[i] Grid_Rectangle*(Game_Gridcount-1.5)
        Game_Person = Game_Canvas.create_text(Grid_Rectangle*(Game_Gridcount-Person_Location[0]), (Grid_Rectangle*(Game_Gridcount-Person_Location[1]), text = "P", font = ('Abadi', 450//Game_Gridcount, "bold"), tag = 'Game_Person')
    All_People = Game_Canvas.find_withtag('Game_Person')
    All_People = list(All_People)

def Move_Player(event):
    global Game_Canvas, Game_Player, Game_Base, Player_PosY, Player_PosX, Current_Colour, Player_Power, Power_Counter, Player_Storage, Players_Collected, Game_Finished, Current_Person
    if event.char.lower() == '\x1b' and Game_Paused == False:
        Paused_UI()
    elif event.char.lower() == '\x1b' and Game_Paused == True:
        Close_Paused_UI()
    if Game_Paused == False and Game_Finished != True:
        Movement_Value = Movement_Values[event.char.lower()]
        if Player_PosX != Movement_Value[0] and Player_PosY != Movement_Value[1]:
            # Detect current grid colour and configure power accordingly
            Player_Power = Player_Power - Power_Values[Grid_Colour[Current_Colour]]
            Power_Counter.configure(text = Player_Power)
            Current_Colour = Current_Colour + Movement_Value[4]
            # Delete the player then recreate in its new position
            Game_Canvas.delete(Game_Player)
            Player_PosX, Player_PosY = Player_PosX + Movement_Value[2], Player_PosY + Movement_Value[3]
            Game_Player = Game_Canvas.create_text(Player_PosX, Player_PosY, text = "R", font = ('Abadi', 450//Game_Gridcount, "bold"))
            # Detect Person
            for item in People_Locations:
                Person_Location = People_Locations[item]
                if Player_PosX  == Person_Location[0] and Player_PosY == Person_Location[1] and Player_Storage != 1:
                    Current_Person = Person_Location[2]
                    Game_Canvas.delete(All_People[Current_Person])
                    Player_Storage += 1
            if Player_PosX == 450 and Player_PosY == 330:
                if Player_Storage == 1:
                    Player_Storage -= 1
                    Players_Collected += 1
                    Clear_Current = People_Locations[Current_Person]
                    Clear_Current = Clear_Current[2]
                    People_Locations.pop(Clear_Current)
        Ending_Conditions()

def Start_Game():
    global Start_Canvas
    Start_Canvas = Canvas(Root, width = 600, height = 500, bd=0, highlightthickness=0, bg = '#ffffff')
    Start_Canvas.pack()

    Title = Start_Canvas.create_text(300, 163, text = "BOTMOD", font = ('Cinema Gothic BTN Shadow', 75, 'normal'))

    Play_Button = Button(text = "PLAY", width = 10, font=('Arial', 15), command = Play_Game)
    Play_Button = Start_Canvas.create_window(300, 325, window = Play_Button)

    Exit_Button = Button(text = "EXIT", width = 10, font=('Arial', 15), command = lambda: Game_End("EXIT"))
    Exit_Button = Start_Canvas.create_window(300, 375, window = Exit_Button)

def Play_Game():
    global Menu_Canvas, Grid_Entry, Power_Entry, People_Entry
    if Already_Played == False:
        Start_Canvas.destroy()
    Menu_Canvas = Canvas(Root, width = 600, height = 500, bd=0, highlightthickness=0, bg = '#C0C0C0')
    Menu_Canvas.pack()

    Title_Rectangle = Menu_Canvas.create_rectangle(120, 10, 480, 100, fill = '#000000')
    Slider_Rectangle = Menu_Canvas.create_rectangle(120, 110, 480, 430, fill = '#000000')
    Footer_Rectangle = Menu_Canvas.create_rectangle(120, 440, 480, 490, fill = '#000000')

    Title = Menu_Canvas.create_text(300, 55, text = "BOTMOD", font = ('Cinema Gothic BTN Shadow', 55, 'normal'), fill = '#ffffff')

    Description_Text = Label(text = "Set Game Options", font=('Arial', 15, 'bold'), bg = '#000000', fg = '#ffffff')
    Description_Text = Menu_Canvas.create_window(300, 145, window = Description_Text)

    Power_Text = Label(text = "Robot Power", font=('Cinema Gothic BTN Shadow', 15), bg = '#000000', fg = '#ffffff')
    Power_Text = Menu_Canvas.create_window(300, 195, window = Power_Text)
    Power_Entry = Scale(Root, from_= 50, to = 350, orient = HORIZONTAL, bg = '#000000', fg = '#ffffff', highlightthickness=0, bd = 0)
    Power_Entrys = Menu_Canvas.create_window(300, 225, window = Power_Entry)

    People_Text = Label(text = "Person Count", font=('Cinema Gothic BTN Shadow', 15), bg = '#000000', fg = '#ffffff')
    People_Text = Menu_Canvas.create_window(300, 280, window = People_Text)
    People_Entry = Scale(Root, from_= 2, to = 10, orient = HORIZONTAL, bg = '#000000', fg = '#ffffff', highlightthickness=0, bd = 0)
    People_Entrys = Menu_Canvas.create_window(300, 310, window = People_Entry)

    Grid_Text = Label(text = "Grid Count", font=('Cinema Gothic BTN Shadow', 15), bg = '#000000', fg = '#ffffff')
    Grid_Text = Menu_Canvas.create_window(300, 365, window = Grid_Text)
    Grid_Entry = Scale(Root, from_= 6, to = 14, orient = HORIZONTAL, resolution = 2, bg = '#000000', fg = '#ffffff', highlightthickness=0, bd = 0)
    Grid_Entrys = Menu_Canvas.create_window(300, 395, window = Grid_Entry)

    Play_Button = Button(text = "OK", width = 15, font=('Cinema Gothic BTN Shadow', 10), command = lambda: Init_Game("CUSTOM"), bg = '#000000', fg = '#ffffff')
    Play_Button = Menu_Canvas.create_window(400, 465, window = Play_Button)
    PlayWD_Button = Button(text = "Use Defaults", width = 15, font=('Cinema Gothic BTN Shadow', 10), command = lambda: Init_Game("DEFAULT"), bg = '#000000', fg = '#ffffff')
    PlayWD_Button = Menu_Canvas.create_window(200, 465, window = PlayWD_Button)

def Init_Game(Game_Options):
    global Player_PosY, Player_PosX, Game_Paused, Game_Finished, Playing_Game, Player_Power, Game_Gridcount, Player_Movement, Game_People, Game_Canvas, Movement_Values, Power_Values, Grid_Rectangle
    if Game_Options == "DEFAULT":
        Game_Gridcount = 10
        Grid_Rectangle = 600/10
        Player_Movement = Grid_Rectangle/2
        Player_Power = 150
        Game_People = 3
    elif Game_Options == "CUSTOM":
        Game_Gridcount = Grid_Entry.get()
        Grid_Rectangle = 600/Game_Gridcount
        Player_Movement = Grid_Rectangle/2
        Player_Power = Power_Entry.get()
        Game_People = People_Entry.get()

    Movement_Values = {'w':(1000, 300/Game_Gridcount, 0, -600/Game_Gridcount, 10), 'a':(300/Game_Gridcount, 1000, -600/Game_Gridcount, 0, -1), 's':(1000, 600-Player_Movement, 0, 600/Game_Gridcount, Game_Gridcount-Game_Gridcount*2), 'd':(600-Player_Movement, 1000, 600/Game_Gridcount, 0, 1)}
    Power_Values = {'#009446':2, '#D4F0FF':1, '#5f6365':3}
    People_Locations = {0:[1.5, 1.5, 0], 1:[1.5, 4.5, 1], 2:[1.5, 1.5, 2], 3:[1.5, 1.5, 3], 4:[1.5, 1.5, 4], 5:[1.5, 1.5, 5], 6:[1.5, 1.5, 6], 7:[1.5, 1.5, 7], 8:[1.5, 1.5, 8], 9:[1.5, 1.5, 9], 10:[1.5, 1.5, 10]}
    Menu_Canvas.destroy()
    Game_Canvas = Canvas(Root, width = 600, height = 600, bd=0, highlightthickness=0)
    Generate_Game()
    Root.bind('<KeyPress>', Move_Player)                  
    Root.protocol("WM_DELETE_WINDOW", lambda: Game_End("EXIT"))

Playing_Game = True
Game_Paused = False
Restart_Game = False
Already_Played = False

while True:
    while Restart_Game == False:
        if Playing_Game == False:
            break
        else:
            Game_Finished = False
            Grid_Colour = []
            Power_Values = {'#009446':2, '#D4F0FF':1, '#5f6365':3}
            Root = Tk()
            if Already_Played == True:
                Play_Game()
                Root.mainloop()
            elif Already_Played == False:
                Start_Game()
                Root.mainloop()
    break