提高物体移动的速度(Tkinter)

时间:2019-04-27 09:53:55

标签: python python-3.x tkinter tkinter-canvas flappy-bird-clone

我仍在编写Flappy Bird游戏,现在开始“调试”部分 为此,我修复了一些问题,例如未出现的乐谱文本,甚至定制了图标和窗口名称(很少注意细节)。 除了不得不纠正一个非常不幸的细节外,这只鸟掉下来的速度太慢了。 因此,对于玩家来说,不可能实现位移,特别是当鸟从一对非常高的管子变成一对非常低的管子时。 但是,鸟的跳跃是完美的,我当然不想改变它们!

这对我来说增加鸟落下时的下落速度是合乎逻辑的,但无济于事,我使用了各种方法来编程这种增加的速度,但尚未完成。例如,我试图破坏鸟的活动,但它没有改变任何东西。 我知道我经验不足,但是我认为自己有能力,而且在网上找不到类似的主题

为了帮助您,我将程序限制在了最低限度,并且游戏中的对象始终处于运动状态,我知道我在本周内会向您寻求太多帮助,对不起但承诺,调整之后,您将再也见不到我!

Here is the .zip file of the images of my game!

from tkinter import *
import random
from random import randint

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

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

    x0, y0, x1, y1 = canvas.bbox(image_oiseau)

    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-379.8)
    if tuyx>=-28:
        tuyx=tuyx-5
    else:
        tuyx=316
        h=randint(272,523)

    canvas.coords(image_tuyau_haut2,tuyx2,H)
    canvas.coords(image_tuyau_bas2,tuyx2,H-379.8)
    if tuyx2>=-28:
        tuyx2=tuyx2-5
    else:
        tuyx2=316
        H=randint(272,523)
    canvas.after(40,deplacement)

LARGEUR = 286
HAUTEUR = 510
DY = 5
tuyx=316
tuyx2=488
h=randint(272,523)
H=randint(272,523)
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)

canvas.pack()
canvas.focus_set()

deplacement()
canvas.bind("<space>",sauter)

fenetre.mainloop()

1 个答案:

答案 0 :(得分:3)

要提高下降速度,必须增加DY,当前固定为5;但是,您也要在sauter方法中使用DY,以便保持完整...

我建议添加一个变量DY_fall,并将其设置为比当前DY大的值;这样,您无需修改​​sauter

您将需要:

使用所需的值初始化DY_fall
更改鸟类在运动中的运动,请使用DY_fall
canvas.move(image_oiseau, 0, DY_fall)

[编辑]:

我添加了一个变量GRAVITY,该变量增加了下降速度,而不会干扰sauter函数。

这需要添加另一个变量dy_fall,只要鸟落下,该变量就会从GRAVITY开始加速。每次鸟“跳”时,都会将其重置为默认DY_fall值。

您需要调整这些值才能获得流畅的游戏,但我认为它的表现应如您所愿。

from tkinter import *
import random
from random import randint


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

def deplacement():
    global mouvement, dy_fall
    global tuyx,tuyx2,h,H,oisx,oisy,solx,sol2x,score,pause

    x0, y0, x1, y1 = canvas.bbox(image_oiseau)

    canvas.move(image_oiseau, 0, dy_fall)
    dy_fall *= GRAVITY

    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-379.8)
    if tuyx>=-28:
        tuyx=tuyx-5
    else:
        tuyx=316
        h=randint(272,523)

    canvas.coords(image_tuyau_haut2,tuyx2,H)
    canvas.coords(image_tuyau_bas2,tuyx2,H-379.8)
    if tuyx2>=-28:
        tuyx2=tuyx2-5
    else:
        tuyx2=316
        H=randint(272,523)
    canvas.after(100, deplacement)

LARGEUR = 286
HAUTEUR = 510
DY = 5
dy_fall = DY_fall = 5
GRAVITY = 1.5
tuyx=316
tuyx2=488
h=randint(272,523)
H=randint(272,523)
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)

canvas.pack()
canvas.focus_set()

deplacement()
canvas.bind("<space>",sauter)

fenetre.mainloop()