我的俄罗斯方块游戏总是崩溃 (tkinter) (python)
My tetris game keeps collapsing (tkinter) (python)
我正在尝试在 Tkinter 中制作一个简单的俄罗斯方块游戏,但实际 window 打不开。如果我不使用 time() 函数使游戏碎片掉落,它将绘制,但是一旦我添加 time() 函数,window 就会消失。我可以看到程序正在通过打印下落件的坐标来工作,但是我的代码的下落方面似乎干扰了 tkinter window.
的绘制
为什么程序不绘制 window?
from tkinter import *
from tkinter import ttk
import random
import time
root = Tk()
root.title('Emblas tetris game')
x_init = root.winfo_pointerx()
y_init = root.winfo_pointery()
WIDTH = root.winfo_screenwidth()
HEIGHT = root.winfo_screenheight()
basicbox = Canvas(root, width=WIDTH, height=HEIGHT)
basicbox.pack()
gameboard = basicbox.create_rectangle(WIDTH/6, 0, WIDTH * 0.84, 720,
outline='gray', fill='white', width=4)
#------- FIGURES -------------------------------------------
def countpointsinfig(): #redraws each figure as it is falling, updating all coordinates
global x0, y0, x1, y1, x2, y2, x3, y3
numpoints = len(points)
if numpoints == 8:
x0, y0, x1, y1, x2, y2, x3, y3 = basicbox.coords(drawshape)
print(x0, y0, x1, y1, x2, y2, x3, y3)
def I_shape():
global drawshape, points
points = [WIDTH/2 - 10, 0,#0
WIDTH/2 + 10, 0,#1
WIDTH/2 + 10, 80,#2
WIDTH/2 - 10, 80,#3
]
drawshape = basicbox.create_polygon(points, outline='yellow', fill='yellow')
def O_shape():
global drawshape, points
points = [WIDTH/2 - 20, 0,#0
WIDTH/2 + 20, 0,#1
WIDTH/2 + 20, 40,#2
WIDTH/2 - 20, 40,#3
]
drawshape = basicbox.create_polygon(points, outline='red', fill='red')
#--------- GAME -----------------------------------------------
def startgame():
global rand_shape, pickone
pickone = [I_shape, O_shape]
rand_shape = random.choice(pickone)()
autofall = True #very very VERY interesting....... This is the problem. This function should move my tetris piece down. If i remove this bit the code works. Why?
while autofall:
time.sleep(1)
basicbox.move(drawshape, 0, 20)
countpointsinfig()
#----------- MOVE FIGURES -----------------------
def leftmove(event):
countpointsinfig()
basicbox.move(drawshape, -20, 0)
def rightmove(event):
countpointsinfig()
basicbox.move(drawshape, 20, 0)
def downmove(event):
countpointsinfig()
basicbox.move(drawshape, 0, 20)
root.bind('<Left>', leftmove)
root.bind('<Right>', rightmove)
root.bind('<Up>', upmove)
root.bind('<Down>', downmove)
#----------------------------------------------------------------
startgame()
root.mainloop()
root.mainloop()
一起[=13=]就是问题所在。
在中你可以理解mainloop()
的作用。
解决您的问题的简单方法是使用 after()
方法。
更改这段代码:
def startgame():
global rand_shape, pickone
pickone = [I_shape, O_shape]
rand_shape = random.choice(pickone)()
autofall = True
while autofall:
time.sleep(1)
basicbox.move(drawshape, 0, 20)
countpointsinfig()
为此:
def startgame():
global rand_shape, pickone
pickone = [I_shape, O_shape]
rand_shape = random.choice(pickone)()
autofall()
def autofall():
basicbox.move(drawshape, 0, 20)
countpointsinfig()
basicbox.after(1000,autofall)
我正在尝试在 Tkinter 中制作一个简单的俄罗斯方块游戏,但实际 window 打不开。如果我不使用 time() 函数使游戏碎片掉落,它将绘制,但是一旦我添加 time() 函数,window 就会消失。我可以看到程序正在通过打印下落件的坐标来工作,但是我的代码的下落方面似乎干扰了 tkinter window.
的绘制为什么程序不绘制 window?
from tkinter import *
from tkinter import ttk
import random
import time
root = Tk()
root.title('Emblas tetris game')
x_init = root.winfo_pointerx()
y_init = root.winfo_pointery()
WIDTH = root.winfo_screenwidth()
HEIGHT = root.winfo_screenheight()
basicbox = Canvas(root, width=WIDTH, height=HEIGHT)
basicbox.pack()
gameboard = basicbox.create_rectangle(WIDTH/6, 0, WIDTH * 0.84, 720,
outline='gray', fill='white', width=4)
#------- FIGURES -------------------------------------------
def countpointsinfig(): #redraws each figure as it is falling, updating all coordinates
global x0, y0, x1, y1, x2, y2, x3, y3
numpoints = len(points)
if numpoints == 8:
x0, y0, x1, y1, x2, y2, x3, y3 = basicbox.coords(drawshape)
print(x0, y0, x1, y1, x2, y2, x3, y3)
def I_shape():
global drawshape, points
points = [WIDTH/2 - 10, 0,#0
WIDTH/2 + 10, 0,#1
WIDTH/2 + 10, 80,#2
WIDTH/2 - 10, 80,#3
]
drawshape = basicbox.create_polygon(points, outline='yellow', fill='yellow')
def O_shape():
global drawshape, points
points = [WIDTH/2 - 20, 0,#0
WIDTH/2 + 20, 0,#1
WIDTH/2 + 20, 40,#2
WIDTH/2 - 20, 40,#3
]
drawshape = basicbox.create_polygon(points, outline='red', fill='red')
#--------- GAME -----------------------------------------------
def startgame():
global rand_shape, pickone
pickone = [I_shape, O_shape]
rand_shape = random.choice(pickone)()
autofall = True #very very VERY interesting....... This is the problem. This function should move my tetris piece down. If i remove this bit the code works. Why?
while autofall:
time.sleep(1)
basicbox.move(drawshape, 0, 20)
countpointsinfig()
#----------- MOVE FIGURES -----------------------
def leftmove(event):
countpointsinfig()
basicbox.move(drawshape, -20, 0)
def rightmove(event):
countpointsinfig()
basicbox.move(drawshape, 20, 0)
def downmove(event):
countpointsinfig()
basicbox.move(drawshape, 0, 20)
root.bind('<Left>', leftmove)
root.bind('<Right>', rightmove)
root.bind('<Up>', upmove)
root.bind('<Down>', downmove)
#----------------------------------------------------------------
startgame()
root.mainloop()
root.mainloop()
一起[=13=]就是问题所在。
在mainloop()
的作用。
解决您的问题的简单方法是使用 after()
方法。
更改这段代码:
def startgame():
global rand_shape, pickone
pickone = [I_shape, O_shape]
rand_shape = random.choice(pickone)()
autofall = True
while autofall:
time.sleep(1)
basicbox.move(drawshape, 0, 20)
countpointsinfig()
为此:
def startgame():
global rand_shape, pickone
pickone = [I_shape, O_shape]
rand_shape = random.choice(pickone)()
autofall()
def autofall():
basicbox.move(drawshape, 0, 20)
countpointsinfig()
basicbox.after(1000,autofall)