master = Tk()
canvas_width = 100
canvas_height = 250
w = Canvas(master,
width=canvas_width,
height=canvas_height)
w.pack()
class Tetriminos(object):
def __init__(self, position, pivot, color):
self.position = position
self.pivot = pivot
self.color = color
def left_shift(self):
backup = copy.copy(self.position)
for i in range(len(self.position)):
change = str(int(self.position[i][0]) - 1) + self.position[i][1:]
if "-" in change:
self.position = backup
return False
for j in self.position:
if "0" in self.position:
self.position = backup
return False
self.position[i] = change
self.pivot[0] -=1
def right_shift(self):
backup = copy.copy(self.position)
for i in range(len(self.position)):
change = str(int(self.position[i][0]) + 1) + self.position[i][1:]
if "0" in change:
self.position = backup
return False
self.position[i] = change
self.pivot[0] += 1
def rotate(self): #direction?
for i in range(len(self.position)):
a = int(self.position[i][0]) - self.pivot[0]
b = int(self.position[i][1:]) - self.pivot[1]
a, b = a*0 + b*(-1), a*1 + b*0
a += self.pivot[0]
b += self.pivot[1]
if a >= 0 and b >= 0:
self.position[i] = str(a) + str(b)
else:
pass
def drop(self):
pass
def slow_drop(self):
for i in range(len(self.position)):
change = self.position[i][0] + str(int(self.position[i][1:]) + 1)
if int(change[1:]) < 26: #and no other tetrimino is in the way...
self.position[i] = change
else:
return False
self.pivot[1] += 1
def draw(self):
for i in range(len(self.position)):
a = int(self.position[i][0])*10
b = (int(self.position[i][1:])-1)*10 #Das Koordinatensystem ist gedreht!!
c = (int(self.position[i][0])+1)*10
d = (int(self.position[i][1:]))*10
w.create_rectangle(a, b, c, d, fill=self.color)
w.create_rectangle(a, b, c, d, fill=self.color)
w.create_rectangle(a, b, c, d, fill=self.color)
w.create_rectangle(a, b, c, d, fill=self.color)
tetrimino1 = Tetriminos(["302", "402", "502", "602"], [4, 2], "green")
tetrimino2 = Tetriminos(["402", "502", "401", "501"], [5, 1], "grey")
tetrimino3 = Tetriminos(["302", "402", "502", "401"], [4.5, 1.5], "yellow")
tetrimino4 = Tetriminos(["302", "402", "502", "301"], [4.5, 1.5], "blue")
tetrimino5 = Tetriminos(["302", "402", "502", "501"], [4.5, 1.5], "red")
tetrimino6 = Tetriminos(["302", "402", "401", "501"], [4, 1], "white")
tetrimino7 = Tetriminos(["402", "502", "301", "401"], [5, 1], "orange")
tetrimino_list = [tetrimino1, tetrimino2, tetrimino3, tetrimino4,tetrimino5,
tetrimino6, tetrimino7]
settled_tetriminos = []
def draw():
w.delete(ALL)
current.draw()
for t in settled_tetriminos:
t.draw
w.update_idletasks()
time.sleep(0.1)
current.slow_drop()
print(current.position)
if int(current.position[1][1:]) == 25:
w.delete(ALL)
current.draw()
w.update_idletasks()
settled_tetriminos.append(current)
return False
w.delete(ALL)
current.draw()
for t in settled_tetriminos:
t.draw
w.update_idletasks()
w.after(100, draw)
current = random.choice(tetrimino_list)
draw()
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame,
text="Left", fg="red",
command=self.left())
self.button.pack(side=LEFT)
self.button2 = Button(frame,
text="Right", fg="red",
command=self.right())
self.button2.pack(side=RIGHT)
def left(self):
print("dsdsd")
current.left_shift()
current.draw()
def right(self):
print("ffff")
current.right_shift()
current.draw()
mainloop()
好吧,它現在的作品。但只限於一件。如果我使用任何while循環或遞歸函數,它不會如你所說。但是我怎麼能避免循環,如果我想每次都得到新的tetriminos,最近的一個已經解決了?如果我使用遞歸,否則它在內部工作,但不會顯示在畫布上。運行過程中的畫布更新
或者你可以告訴我爲什麼這個按鈕不起作用。它只是自動執行這兩個方法,然後我再也不能進行交互了。按鈕已經死了。 你認爲可以這樣編程俄羅斯方塊,還是我需要從根本上改變一些東西?
你沒有調用'update_idletasks':你需要'w.update_idletasks()' - 不確定是否會修復它。 – mgilson
'update_idletasks'不會專門更新畫布;相反,它運行任何「空閒」任務。屏幕重繪被認爲是一項閒置任務。 –