如何在屏幕上显示和更新乐谱?

时间:2019-04-22 06:49:24

标签: python python-3.x tkinter tkinter-canvas

我的问题是关于显示和更新文本,以便在屏幕上显示乐谱。

我想创建一个像真实游戏一样出现在屏幕上的乐谱。但是在研究了Google之后,我还没有发现有人希望在屏幕上增加分数...

的确,我希望分数每次在管道之间通过时都增加,因此,每当管道的X值为67像素时,分数都会增加。有人知道怎么做吗?

from tkinter import *
import random
from random import randint

def sauter(event):
    canvas.move(image_oiseau, 0, -10*DY)


def deplacement():
    global tuyx,tuyx2,h,H,oisx,oisy,solx,sol2x

    x0, y0, x1, y1 = canvas.bbox(image_oiseau)
    if y1 < 416:
        canvas.move(image_oiseau, 0, DY)

    canvas.coords(image_sol,solx,512)  
    if solx >= -144:
        solx=solx-5
    else:
        solx=144

    canvas.coords(image_sol2,sol2x,512)
    if sol2x >= 144:
        sol2x=sol2x-5
    else:
        sol2x=432

    canvas.coords(image_tuyau_haut,tuyx,h)
    canvas.coords(image_tuyau_bas,tuyx,h-241)
    if tuyx>=-28:
        tuyx=tuyx-5
    else:
        tuyx=316
        h=randint(256,505)

    canvas.coords(image_tuyau_haut2,tuyx2,H)
    canvas.coords(image_tuyau_bas2,tuyx2,H-241)
    if tuyx2>=-28:
        tuyx2=tuyx2-5
    else:
        tuyx2=316
        H=randint(256,505)

    canvas.after(40,deplacement)


LARGEUR = 286
HAUTEUR = 510
DY = 5
tuyx=316
tuyx2=488 
h=randint(256,505)
H=randint(256,505)
oisx=67
oisy=244
solx=144
sol2x=432


fenetre = Tk()
canvas = Canvas(fenetre, width=LARGEUR, height=HAUTEUR)

fond = PhotoImage(file="background-day.png")
fond2 = PhotoImage(file="background-night.png")
fond=[fond,fond2]
F= random.choice(fond)
canvas.create_image(144,256, anchor=CENTER,image=F)

tuyau_haut = PhotoImage(file="tuyau_vers_le_haut.png")
image_tuyau_haut = canvas.create_image(tuyx,h,anchor=CENTER,image=tuyau_haut)
image_tuyau_haut2 = canvas.create_image(tuyx2,H,anchor=CENTER,image=tuyau_haut)

tuyau_bas = PhotoImage(file="tuyau_vers_le_bas.png")
image_tuyau_bas = canvas.create_image(tuyx,h,anchor=CENTER,image=tuyau_bas)
image_tuyau_bas2 = canvas.create_image(tuyx2,H,anchor=CENTER,image=tuyau_bas)

sol = PhotoImage(file="sol-day.png")
image_sol = canvas.create_image(144,512, anchor=S,image=sol)
image_sol2 = canvas.create_image(432,512, anchor=S,image=sol)

oiseau = PhotoImage(file="yellowbird-midflap.png")
oiseau2 = PhotoImage(file="bluebird-midflap.png")
oiseau3 = PhotoImage(file="redbird-midflap.png")
oiseau=[oiseau,oiseau2,oiseau3]
O=random.choice(oiseau)
image_oiseau=canvas.create_image(oisx,oisy, anchor=W,image=O) 

deplacement()

canvas.pack()
canvas.focus_set()
canvas.bind("<space>",sauter) 

fenetre.mainloop() 

有人可以向我解释这个问题,因为我认为这很容易:( 这是游戏的图片:)

Here are the pictures of the game

1 个答案:

答案 0 :(得分:1)

这是一种显示分数的方法:它使用tk.Label,并在分数增加的同时进行更新。

当前,提高得分的触发因素是对on_change的随机调用;您可以将其修改为测试,如果管道x坐标变得低于小鸟x坐标(小鸟成功越过障碍物)

如果要在画布上重新放置乐谱标签,则可以。

import random
import tkinter as tk


WIDTH, HEIGHT = 500, 500


def 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 pipes


def 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 = 0
score_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()