Pygame Flappy Bird Collision

时间:2017-07-23 12:22:46

标签: python pygame collision-detection collision

我正在制作一个有趣的鸟类克隆游戏,我试图将这只鸟与管子碰撞。当鸟撞到上管时,我没有收到任何错误消息,只是打印(“碰撞”)没有显示。问题出在班级MainWindow

import pygame

pygame.init()

WIDTH = 800
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("FlapPY Bird")
clock = pygame.time.Clock()

# colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# --> variables
FPS = 60

# classes
class MainWindow(object):
    def __init__(self, w, h):
        self.width = w
        self.height = h
        self.Main()

    def Main(self):
        loop = True

        bird_width = 25
        bird_height = 25
        self.bird_x = 150
        self.bird_y = HEIGHT/2 - int(bird_height/2)
        bird_x_move = 0
        bird_y_move = 0
        pipe_spacing = 350
        pipe_speed = 1

        space = 100
        p1_x = 300
        p1_y = 400
        p1_w = 50
        p1_h = HEIGHT

        p2_x = p1_x + pipe_spacing
        p2_y = 250
        p2_w = 50
        p2_h = HEIGHT

        p3_x = p2_x + pipe_spacing
        p3_y = 250
        p3_w = 50
        p3_h = HEIGHT

        pipe1 = Pipes(p1_x, p1_y, p1_w, p1_h, space, pipe_speed, red)
        pipe2 = Pipes(p2_x, p2_y, p2_w, p2_h, space, pipe_speed, green)
        pipe3 = Pipes(p3_x, p3_y, p3_w, p3_h, space, pipe_speed, blue)

        while loop:
            for event in pygame.event.get():
                #print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        bird_y_move = -7

                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_SPACE:
                        bird_y_move = 3

            screen.fill(white)

            self.bird_x += bird_x_move
            self.bird_y += bird_y_move
            bird = FlappyBird(self.bird_x, self.bird_y, bird_width, bird_height)
            bird.draw()

            pipe1.draw_pipes()
            pipe2.draw_pipes()
            pipe3.draw_pipes()

            pipe1.check_if()
            pipe2.check_if()
            pipe3.check_if()

            p1_new_x = pipe1.pipe_move()
            p2_new_x = pipe2.pipe_move()
            p3_new_x = pipe3.pipe_move()


            if self.bird_y <= p1_y - space and self.bird_y >= p1_y and self.bird_x+bird_width >= p1_new_x and self.bird_x <= p1_new_x+p1_w:
                print("collision")

            pygame.display.update()
            clock.tick(FPS)

class FlappyBird(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def draw(self):
        pygame.draw.rect(screen, red, (self.x, self.y, self.width, self.height))


class Pipes(object):
    def __init__(self, x, y, width, height, space, speed, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.space = space
        self.speed = speed
        self.color = color

    def draw_pipes(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
        pygame.draw.rect(screen, self.color, (self.x, self.y-self.space-self.height, self.width, self.height))

    def pipe_move(self):
        self.x -= self.speed
        return self.x

    def check_if(self):
        if self.x < 0:
            self.x = 1000

MainWindow(WIDTH, HEIGHT)

1 个答案:

答案 0 :(得分:2)

你甚至缩进吗? :/
编辑:这是固定的。

import pygame

pygame.init()

WIDTH = 800
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("FlapPY Bird")
clock = pygame.time.Clock()

# colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# --> variables
FPS = 60

# classes


class MainWindow(object):
    def __init__(self, w, h):
        self.width = w
        self.height = h
        self.Main()

    def Main(self):
        loop = True

        bird_width = 25
        bird_height = 25.0
        self.bird_x = 150
        self.bird_y = ((HEIGHT / 2) - int(bird_height / 2.0))
        bird_x_move = 0
        bird_y_move = 0
        pipe_spacing = 350
        pipe_speed = 1

        space = 100
        p1_x = 300
        p1_y = 400
        p1_w = 50
        p1_h = HEIGHT

        p2_x = p1_x + pipe_spacing
        p2_y = 250
        p2_w = 50
        p2_h = HEIGHT

        p3_x = p2_x + pipe_spacing
        p3_y = 250
        p3_w = 50
        p3_h = HEIGHT

        pipe1 = Pipes(p1_x, p1_y, p1_w, p1_h, space, pipe_speed, red)
        pipe2 = Pipes(p2_x, p2_y, p2_w, p2_h, space, pipe_speed, green)
        pipe3 = Pipes(p3_x, p3_y, p3_w, p3_h, space, pipe_speed, blue)

        while loop:
            for event in pygame.event.get():
                #print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        bird_y_move = -7

                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_SPACE:
                        bird_y_move = 3

            screen.fill(white)

            self.bird_x += bird_x_move
            self.bird_y += bird_y_move
            bird = FlappyBird(self.bird_x, self.bird_y, bird_width, bird_height)
            bird.draw()

            pipe1.draw_pipes()
            pipe2.draw_pipes()
            pipe3.draw_pipes()

            pipe1.check_if()
            pipe2.check_if()
            pipe3.check_if()

            p1_new_x = pipe1.pipe_move()
            p2_new_x = pipe2.pipe_move()
            p3_new_x = pipe3.pipe_move()


            if (self.bird_y <= (p1_y - space)) and (self.bird_y <= p1_y) and ((self.bird_x + bird_width) >= p1_new_x) and (self.bird_x <= (p1_new_x + p1_w)):
                print("collision")
                break # If you don't break the loop, it will keep printing "collision" to the terminal
                exit(0) # Just in case.

            pygame.display.update()
            clock.tick(FPS)

class FlappyBird(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def draw(self):
        pygame.draw.rect(screen, red, (self.x, self.y, self.width, self.height))


class Pipes(object):
    def __init__(self, x, y, width, height, space, speed, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.space = space
        self.speed = speed
        self.color = color

    def draw_pipes(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
        pygame.draw.rect(screen, self.color, (self.x, self.y-self.space-self.height, self.width, self.height))

    def pipe_move(self):
        self.x -= self.speed
        return self.x

    def check_if(self):
        if self.x < 0:
            self.x = 1000

MainWindow(WIDTH, HEIGHT)

不确定这是否是您想要的,但它确实有效。 我所做的是将self.bird_y >= p1_y更改为self.bird_y <= p1_y