我如何修复飞扬的小鸟游戏中的碰撞检测代码

问题描述 投票:0回答:0

我是一名新程序员,决定制作一款飞扬的小鸟克隆游戏。我目前拥有游戏的所有机制,例如管道移动到新位置和小鸟跳跃,但我无法让程序在小鸟撞到管道时检测到碰撞。这是我的完整程序,可以帮助您解决有关代码的任何问题,感谢您的帮助:

import turtle as t
import random

# screen setup
wn = t.Screen()
wn.screensize(canvwidth = 2,canvheight = 1)
wn.title("Flappy Bird")
wn.bgcolor("light blue")

# list(s)
bird = [0,0,0,1,1]
pipe_1 = [250,250,-7,0,1] 
pipe_2 = [250,-250,-7,0,1]
color_list = ["yellow"]
inputs = ["Up"]

# radius of bird and pipe
pipe_radius = 125
bird_radius = 125

# making bird
bird_color = color_list
b = t.Turtle()
b.penup()
b.shape("circle")
b.shapesize(bird[4] * 2)
b.color(bird_color)

# create list of pipes1
pipes1 = []
# create the first pipe1
pipe1 = t.Turtle()
pipe1.hideturtle()
pipe1.shape("turtle")
pipe1.fillcolor("light blue")
pipe1.speed(0)
pipe1.penup()
pipe1.goto(300,random.randint(-200,250))
pipe1.shape('square')
pipe1.shapesize(20,5)
pipe1.color('green')
pipe1.dx = -99
pipes1.append(pipe1)

# create list of pipes2
pipes2 = []
# create the first pipe2
pipe2 = t.Turtle()
pipe2.hideturtle()
pipe2.shape("turtle")
pipe2.fillcolor("light blue")
pipe2.speed(0)
pipe2.penup()
pipe2.goto(300,random.randint(-250,-200))
pipe2.shape('square')
pipe2.shapesize(20,5)
pipe2.color('green')
pipe2.dx = -20
pipes2.append(pipe1)

y_pos = 0
def flap():
    global y_pos
    y_pos += 70
wn.onkeypress(flap, "Up")
wn.listen()


# movement mechanics
while (True):
    bird[0] += bird[2]
    if (abs(bird[0] - pipe_1[0]) <= bird_radius + pipe_radius) and (abs(bird[1] - pipe_1[1])     <= bird_radius + pipe_radius):
        bird[0] -= bird[2]
        bird[2] = 0
    bird[1] += bird[3] 
    if (abs(bird[0] - pipe_1[0]) <= bird_radius + pipe_radius) and (abs(bird[1] - pipe_1[1]) <= bird_radius + pipe_radius):
        bird[1] -= bird[3]
        bird[3] = 0
    bird[3] -= 1 
    if (abs(bird[0] - pipe_2[0]) <= bird_radius + pipe_radius) and (abs(bird[1] - pipe_2[1]) <= bird_radius + pipe_radius):
        bird[0] -= bird[2]
        bird[2] = 0
    bird[1] += bird[3] 
    if (abs(bird[0] - pipe_2[0]) <= bird_radius + pipe_radius) and (abs(bird[1] - pipe_2[1]) <= bird_radius + pipe_radius):
        bird[1] -= bird[3]
        bird[3] = 0
    y_pos -= 10

    # move pipe1
    for pipe1 in pipes1:
        pipe1.setx(pipe1.xcor()+pipe1.dx)

    # move pipe2
    for pipe2 in pipes2:
        pipe2.setx(pipe2.xcor()+pipe2.dx)

        # check if pipe1 is off the screen
        if pipe1.xcor() < -400:
            pipes1.remove(pipe1)
            new_pipe1 = t.Turtle()
            new_pipe1.shape("turtle")
            new_pipe1.fillcolor("light blue")
            new_pipe1.speed(0)
            new_pipe1.penup()
            new_pipe1.goto(400,random.randint(350,400))
            new_pipe1.shape('square')
            new_pipe1.shapesize(30,5)
            new_pipe1.color('green')
            new_pipe1.dx = -10
            pipes1.append(new_pipe1)

        # check if pipe2 is off the screen
        if pipe2.xcor() < -400:
            pipes2.remove(pipe2)
            new_pipe2 = t.Turtle()
            new_pipe2.shape("turtle")
            new_pipe2.fillcolor("light blue")
            new_pipe2.speed(0)
            new_pipe2.penup()
            new_pipe2.goto(400,random.randint(-400,-350))
            new_pipe2.shape('square')
            new_pipe2.shapesize(30,5)
            new_pipe2.color('green')
            new_pipe2.dx = -10
            pipes2.append(new_pipe2)

    # update the screen
    wn.update()

    b.goto(bird[0], y_pos)

wn.mainloop()`
python collision-detection collision flappy-bird-clone
© www.soinside.com 2019 - 2024. All rights reserved.