2017-05-08 75 views
-3

這就是我到目前爲止所做的,我正在製作一個有角色的遊戲(現在它只是一個圓圈),並且必須通過遊戲才能完成遊戲,而不必觸摸牆壁。它會逐漸變得越來越難,因爲你可以看到第一個層次,你不會真的做得太多。我不知道該怎麼做的是創建一個帶有遊戲標題和開始按鈕以及指令按鈕的「開始」屏幕,同時仍然是600x600的畫布。我環顧四周,並且正在使用的框架和類大多數情況下,但我也讀過一些你不應該使用.pack with .grid的地方?如果任何人都可以給我一個想法,甚至可以製作一個屏幕,我相信我可以將其餘部分拼湊在一起,這將是不勝感激!在畫布上的GUI遊戲中創建不同的屏幕?

from Tkinter import * 


'''global for all three levels''' 

root=Tk() 
root.wm_title("Game title") 
canvas = Canvas(root, width=600, height=600, bg = 'white') 
canvas.grid() 

circle = canvas.create_oval(280, 280, 300, 300, fill = 'purple') 
dx, dy = 0, 0 

def move(e): 
    global dx, dy 
    if e.keysym == 'Left': 
     dx, dy = -5, 0 
    elif e.keysym == 'Right': 
     dx, dy = 5, 0 
    elif e.keysym == 'Up': 
     dx, dy = 0, -5 
    elif e.keysym == 'Down': 
     dx, dy = 0, 5 
    elif e.keysym == 's': 
     dx, dy = 0,0 

def animate(): 
    x1, y1, x2, y2 = canvas.coords(circle) 
    canvas.coords(circle, x1 + dx, y1 + dy, x2 + dx, y2 + dy) 
    canvas.after(100, animate)  

root.bind('<Left>', move) 
root.bind('<Right>', move) 
root.bind('<Up>', move) 
root.bind('<Down>', move) 
animate() 


#class main_Screen()   

#start button, instructions button, want it to be 600x600 same as level screens    



#class levels() 

    #def level_One() 

     #wall1 = canvas.create_rectangle(0, 0, 650, 200, fill='#00ff00') 
     #wall2 = canvas.create_rectangle(0, 400, 650, 600, fill='#00ff00') 


    #def level_Two(): 

     #canvas.create_rectangle(500, 0, 600, 600, fill = '#000000') 
     #canvas.create_rectangle(490, 600, 500, 0, fill = '#000000') 
     #canvas.create_rectangle(400, 90, 490, 360, fill = '#000000') 
     #canvas.create_rectangle(440, 40, 0, 0, fill = '#000000') 
     #canvas.create_rectangle(260, 40, 350, 210, fill = '#000000') 
     #canvas.create_rectangle(60, 90, 210, 240, fill = '#000000') 
     #canvas.create_rectangle(0, 0, 10, 310, fill = '#000000') 
     #canvas.create_rectangle(0, 290, 120, 600, fill = '#000000') 
     #canvas.create_rectangle(400, 600, 440, 410, fill = '#000000') 
     #canvas.create_rectangle(440, 410, 270, 500, fill = '#000000') 
     #canvas.create_rectangle(490, 360, 220, 260, fill = '#000000') 
     #canvas.create_rectangle(220, 260, 170, 560, fill = '#000000') 
     #canvas.create_rectangle(170, 560, 210, 240, fill = '#000000') 
     #canvas.create_rectangle(350, 560, 220, 550, fill = '#000000') 



    #def level_Three() 


root.mainloop() 
+0

我也不是很喜歡編程中的類如何工作,或者你怎麼知道要放什麼。對不起,我是一個新手,但任何評論幫助! – WeAreConfusing

+0

你想讓開始屏幕成爲一個單獨的窗口嗎?創建一個「Toplevel」的實例。否則,創建一個'Frame'並在框架中放置任何你想要的東西。 –

+0

我不想要一個單獨的窗口,我希望它只是使用同一個窗口,但然後能夠打開不同的屏幕,並且我試圖使用Toplevel,但似乎無法使其工作。我會再試一次,謝謝。 – WeAreConfusing

回答

0

您應該能夠使用以下代碼作爲在遊戲中使用不同「屏幕」的基礎。基本上,您將所有要使用的框架堆疊在一起,然後使用在您想要看到的框架上輸入frame.tkraise()的命令。

import Tkinter as tk # Import the Tkinter library 
root = tk.Tk() 

top_level_container = tk.Frame(root) # Set up the null top level container 
top_level_container.pack() 

# These are where you delcare all of the frames you wish to switch between 
main_frame = tk.Frame(top_level_container) 
game_frame = tk.Frame(top_level_container) 

# Stack all of these frame on top of each other 
for frame in (main_frame, game_frame): 
    frame.grid(row=0, column=0, sticky='news', padx=0, pady=0) 


def switch_main(): # Bring the main frame to the front 
    main_frame.tkraise() 


def switch_game(): # Bring the game frame to the front 
    game_frame.tkraise() 

switch_main() # Start with the main frame in front 

# Create the contents of the main frame 
main_text = tk.Label(main_frame, text="This is the main screen") 
main_text.pack() 
main_button = tk.Button(main_frame, text="Switch screens", 
         command=switch_game, background="blue") 
main_button.pack() 

# Create the contents of the game frame 
game_text = tk.Label(game_frame, text="This is the game screen") 
game_text.pack() 
game_button = tk.Button(game_frame, text="Switch screens", 
         command=switch_main, background="green") 
game_button.pack() 

root.mainloop() # Execute the main event handler 
+0

謝謝!對不起,這是遲來的,但我決定保持全部一個屏幕,每次只清除畫布。 – WeAreConfusing

+0

不用擔心,只是一個警告,如果你需要在任何高頻率更新的東西,你可能會發現清理和重繪每一次會減慢你的速度。如果只是偶爾,那個方法應該沒問題。 –

+0

是的,因爲我只有三個級別,我認爲它不應該影響太多:) – WeAreConfusing