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,最近的一个解决了?如果我使用递归,否则它会在内部工作,但不会出现在画布上。
或者你可以告诉我为什么这个按钮不起作用。它只是自动执行这两种方法一次,然后我就不能再交互了。按钮已经死了。你认为有可能以这种方式编写俄罗斯方块,还是我需要彻底改变一些东西?