2017-05-08 15 views
-3

これは今まで私が持っていたものです。私はキャラクター(今は単なるサークル)を持っているゲームを作っています。壁に触れることなくゲームをしなければなりません。それはあなたが本当に多くすることさえ必要としない最初のレベルを見ることができるにつれてますます難しくなります。私のやり方がわからないのは、「ゲームのタイトルと開始ボタンと、まだ600x600のキャンバスである間に、指示ボタンを使って開始画面を作成することです。私は見てきました、フレームとクラスが使用されていますほとんどの状況がありますが、.gridで.packを使用しないでください。誰かが私にちょうど1つの画面を作る方法を知ってもらえれば、残りの部分をまとめていただければ幸いです。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

ありがとうございました!申し訳ありませんがこれは遅れていますが、私はそれを1つの画面にして、毎回キャンバスをクリアすることに決めました。 – WeAreConfusing

+0

心配する必要はありませんが、ただの警告ですが、頻繁に更新する必要がある場合は、そのたびにクリアして再描画すると速度が低下することがあります。それはちょうどときどきその方法よりも良いはずです。 –

+0

私はそれがあまりにも多くの影響を与えるべきではないと思った3つのレベルを持っているので、うん、:) – WeAreConfusing

関連する問題