这是一种显示分数的方法:它使用
tk.Label,在分数增加的同时进行更新。
增加分数的触发因素目前是随机调用
on_change; 如果管道的x坐标变得低于小鸟的x坐标(小鸟成功越过障碍物),则可以将其修改为测试
如果要在画布上重新放置乐谱标签,则可以。
import randomimport tkinter as tkWIDTH, HEIGHT = 500, 500def create_pipes(): pipes = [] for x in range(0, WIDTH, 40): y1 = random.randrange(50, HEIGHT - 50) y0 = y1 + 50 pipes.append(canvas.create_line(x, 0, x, y1)) pipes.append(canvas.create_line(x, y0, x, HEIGHT)) return pipesdef move_pipes(): for pipe in pipes: canvas.move(pipe, -2, 0) x, y0, _, y1 = canvas.coords(pipe) if x < 0: canvas.coords(pipe, WIDTH+20, y0, WIDTH+20, y1) if random.randrange(0, 20) == 10: on_change() root.after(40, move_pipes)def on_change(): global score score += 1 score_variable.set(f'score: {score}')root = tk.Tk()tk.Button(root, text='start', command=move_pipes).pack()score = 0score_variable = tk.StringVar(root, f'score: {score}')score_lbl = tk.Label(root, textvariable=score_variable)score_lbl.pack()canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="cyan")canvas.pack()pipes = create_pipes()root.mainloop()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)