Ad

Increase The Speed Of An Object As It Moves (Tkinter)

I'm still programming my Flappy Bird game and I'm starting the "debugging" part now For that, I repaired some problems like the text of the score which did not appear and I even customized the icon as well as the name of the window (little attention to detail). Except that I have to correct a rather unfortunate detail, the speed of the fall of the bird is too slow. Thus, there are displacements impossible to achieve for the player, especially when the bird goes from a pair of very high pipes to a pair of very low pipes. However, the bird's jumps are perfect and I do not want to change them of course!

It was then logical for me to increase the speed of fall of the bird as it falls but nothing to do, I used various methods to program this increasing speed but they have not not completed. I tried, for example, to break down the movement of the bird but it did not change anything. I am aware that I have little experience but I thought to be able and I can not find similar topics on the net

In order for you to help me, I have reduced my program to the strict minimum and there is only the movement of the objects of the game, I am aware that I ask you too much help during this week and I am sorry but promised, after this adjustment you will never see me again!

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()
Ad

Answer

To increase the downwards speed, you must increase DY, it is currently fixed at 5; however, you are also using DY in the sauter method, that you want to keep intact...

I suggest to add one variable DY_fall, and set it at a larger value than the current DY; this way, you do not need to modify sauter.

You will need to:

initialize DY_fall with the value you want.
change the movement of the bird in movement, to use DY_fall:
canvas.move(image_oiseau, 0, DY_fall)

[EDIT]:

I added a variable GRAVITY that increase the velocity of a downward fall, without interfering with the sauter function.

This required adding yet another variable, dy_fall that is increased by the acceleration from GRAVITY, as long as the bird falls. This is reset to the default DY_fall value each time the bird 'jumps'.

You will need to tweak the values to get a smooth game, but I think it should behave as you wished.

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()
Ad
source: stackoverflow.com
Ad