如何处理坐标碰撞? (在python中)

时间:2019-04-24 09:14:42

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

我正在编写“飞扬的小鸟”,我快完成了,但是我只想念一件事:小鸟和管道之间的碰撞……我设法确保了小鸟撞到地面或从中消失了屏幕上,游戏结束(由于功能fin())。如果鸟碰到管道,我也希望游戏也能结束,但是我做不到,而要求这种东西的人通常会使用Pygame:(。

预先感谢您的帮助,希望该解决方案很简单,但我只是没有找到它。

我试图单独处理碰撞(我们可以在程序中看到相应的部分),但是鸟儿随机停了……

以下是游戏的图片(这是一个Mediafire链接,用于下载图片的zip文件)!

Here are the pictures !

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

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

    if y1<=-5:
        fin()

#Here are the parts supposed to handle collisions ...
    if y1 >= h :
        fin()

    if y1 >= H :
        fin()

    if y0<=h-379.8:
        fin()

    if y0<=h-379.8:
        fin()
#That's the end of the parts supposed to handle collsions...

    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)
        score+=1

    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)
        score+=1

    lscore.config(text=str(score))    
    mouvement =canvas.after(40,deplacement)  

def debut():
    pause=1
    if pause==1:
        M.destroy()
        deplacement()
        canvas.bind("<space>",sauter)  

def fin():
    pause=0
    if pause==0:
        canvas.after_cancel(mouvement)
        canvas.unbind("<space>",sauter)

def rejouer():
    global tuyx,tuyx2,oisx,oisy,solx,sol2x,score
    tuyx=316
    tuyx2=488
    oisx=67
    oisy=244
    solx=144
    sol2x=432
    score=0



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
score=0
mouvement=None


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) 

lscore=Label(fenetre,text='0')
lscore.pack()

menu_debut=PhotoImage(file="menu_jeu.png")
M=Button(fenetre,image=menu_debut,relief=FLAT,command=debut)
Menu_w = canvas.create_window(144,256,window=M)

canvas.pack()
canvas.focus_set()

fenetre.mainloop() 

1 个答案:

答案 0 :(得分:3)

您有两种方法:

1-确定要检查与鸟的周长,安全区,顶部和底部y,管道的宽度,管道的运动以及其他障碍物等是否发生碰撞的坐标,所有这些对于实施和检查都非常有趣,但是有点乏味并且容易出错。

2-充分利用tkinter canvas的功能,并使用方法 find_overlapping(x0, y0, x1, y1) ,该方法返回与给定矩形重叠或被其完全包围的所有项目的元组,并使用它来确定是否发生了碰撞,也许可以使用tag来标记障碍物。

我的建议是使用find_overlapping

也许是这样的(伪代码)

obstacles = canvas.find_withtag('obstacle')
for item in canvas.find_overlapping(canvas.coords(bird)):
    if item in obstacles:
        bird.is_dead()