一:出现拖影的代码:
import random
import tkinter as tk
def show_one(base_canvas, blocks, xy, move=[0, 0], colour="lightgreen"):
# base_canvas是本体,blocks是细胞坐标[[1,2].......],xy组织坐标[m,n],move运动方向[a,b],colour颜色
for block in blocks:
block_x1 = (block[0] + xy[0] + move[0]) * SIZE
block_y1 = (block[1] + xy[1] + move[1]) * SIZE
block_x2 = block_x1 + SIZE
block_y2 = block_y1 + SIZE
base_canvas.create_rectangle(block_x1, block_y1, block_x2, block_y2,
fill=colour, outline="white", width=1)
def show_move(base_canvas, blocks, xxyy, move=[0, 0], colour="lightgreen"):
# base_canvas是本体,blocks是细胞坐标[[1,2].......],xy组织坐标[m,n],move运动方向[a,b],colour颜色
show_one(base_canvas, blocks, xxyy, [0, 0], ) # 默认地图色
xxyy[0] = xxyy[0] + move[0]
xxyy[1] = xxyy[1] + move[1]
show_one(base_canvas, blocks, xxyy, move, shap_colour)
def maps():
map_big = []
for x in range(X_X):
for y in range(Y_Y):
map_big.append([x, y])
return map_big
colour_list = ["blue", "red", "yellow", "purple", "orange", "Cyan"]
SHAPES = [[[-1, -1], [0, -1], [-1, 0], [0, 0]],
[[-1, 0], [0, 0], [0, -1], [1, -1]],
[[-1, 0], [0, 0], [0, -1], [1, 0]],
[[0, 1], [0, 0], [0, -1], [0, -2]],
[[-1, 0], [0, 0], [-1, -1], [-1, -2]],
[[-1, 0], [0, 0], [0, -1], [0, -2]],
[[-1, -1], [0, -1], [0, 0], [1, 0]]]
shape_one = random.choice(SHAPES)
shap_colour=random.choice(colour_list)
base = tk.Tk()
FPS = 400
X_X = 10
Y_Y = 20
SIZE = 30
width = X_X * SIZE + 200
height = Y_Y * SIZE + 200
basement = tk.Canvas(base, width=width, height=height, )
basement.pack()
mn = [4, 4] # 初始坐标在外面,可以变化,在函数内部,不会变化
movement = [0, 1]
def game_loop():
show_move(basement, shape_one, mn, movement, )
base.after(FPS, game_loop)
show_one(basement, maps(), [0, 0], [0, 0])
base.after(FPS, game_loop)
base.mainloop()
二:解决掉拖影的代码
import random
import tkinter as tk
def sh_one(base_canvas, block, xy, colour="lightgreen"):
block_x1 = (block[0] + xy[0]) * SIZE
block_y1 = (block[1] + xy[1]) * SIZE
block_x2 = block_x1 + SIZE
block_y2 = block_y1 + SIZE
base_canvas.create_rectangle(block_x1, block_y1, block_x2, block_y2,
fill=colour, outline="white", width=1)
def sh_move(base_canvas, blocks, xxyy, move=[0, 0], colour="lightgreen"):
# base_canvas是本体,blocks是细胞坐标[[1,2].......],xy组织坐标[m,n],move运动方向[a,b],colour颜色
for bb in blocks:
sh_one(base_canvas, bb, xxyy, ) # 默认地图色
xxyy[0] = xxyy[0] + move[0]
xxyy[1] = xxyy[1] + move[1]
for bbb in blocks:
sh_one(base_canvas, bbb, xxyy, shap_colour)
def show_maps():
map_big = []
for x in range(X_X):
for y in range(Y_Y):
map_big.append([x, y])
for bb in map_big:
sh_one(basement, bb, [0, 0], colour="lightgreen")
colour_list = ["blue", "red", "yellow", "purple", "orange", "Cyan"]
SHAPES = [[[-1, -1], [0, -1], [-1, 0], [0, 0]],
[[-1, 0], [0, 0], [0, -1], [1, -1]],
[[-1, 0], [0, 0], [0, -1], [1, 0]],
[[0, 1], [0, 0], [0, -1], [0, -2]],
[[-1, 0], [0, 0], [-1, -1], [-1, -2]],
[[-1, 0], [0, 0], [0, -1], [0, -2]],
[[-1, -1], [0, -1], [0, 0], [1, 0]]]
shape_one = random.choice(SHAPES)
shap_colour = random.choice(colour_list)
base = tk.Tk()
FPS = 200
X_X = 10
Y_Y = 20
SIZE = 30
width = X_X * SIZE + 200
height = Y_Y * SIZE + 200
basement = tk.Canvas(base, width=width, height=height, )
basement.pack()
mn = [5, 1]
def game_loop():
sh_move(basement, shape_one, mn, [0, 1], )
base.after(FPS, game_loop)
show_maps()
base.after(FPS, game_loop)
base.mainloop()