这是我到目前为止所做的,我正在制作一个游戏,你有一个角色(现在它只是一个圆圈),你必须通过游戏,而不是触摸墙壁。它会逐渐变得更难,因为你可以看到你不必真正做多的第一级。我不知道该怎么做是创建一个“开始屏幕,其中包含游戏标题和开始按钮以及指令按钮,同时仍然是600x600画布。我环顾四周,有框架和类正在使用大多数情况,但我也读过你不应该使用.pack与.grid的地方?如果有人能让我知道如何制作一个屏幕,我相信我可以将其余部分拼凑起来,我将不胜感激! / p>
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 :(得分:0)
您应该可以使用以下代码作为使用不同&#39;屏幕的基础。在你的游戏中。基本上,您将要使用的所有帧堆叠在一起,然后在要显示的帧上使用命令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