Im making a flappy bird clone game in pygame and im using classes, because I want to learn how to use classes.

I created 2 classes a "FlappyBird" and "Pipes" class.

The problem is with the collision. The variable BIRD_Y which is passed into the Pipes class from the MainWindow class and the variable BIRD_Y is chaning its value, but the problem here is that the variable does not change in the Pipes class! I tried with inheritance, still no luck.

I'm sorry if this is like the easy question but I don't have the practice and experience in pygame classes.

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
        bird_x = 150
        bird_y = HEIGHT/2 - int(bird_height/2)
        bird_x_move = 0
        bird_y_move = 0
        bird = FlappyBird(bird_x, bird_y, bird_width, bird_height)
        pipe_spacing = 350
        pipe_speed = 3

        space = 100
        p1_x = 300
        p1_y = 250
        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)
        pipe2 = Pipes(p2_x, p2_y, p2_w, p2_h, space)
        pipe3 = Pipes(p3_x, p3_y, p3_w, p3_h, space)

        while loop:
            for event in pygame.event.get():
                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)

            bird.draw()
            bird.move(bird_x_move, bird_y_move)

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

            pipe1.pipe_move(pipe_speed)
            pipe2.pipe_move(pipe_speed)
            pipe3.pipe_move(pipe_speed)

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

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

    def move(self, dx, dy):
        self.y += dy
        self.x += dx


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

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

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

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

        print(bird_y)


MainWindow(WIDTH, HEIGHT)

1 Answer 1

The variable you are giving to Pipe.check_if() is not updated.

You should get the real coordinate used by the bird, you can do this by calculate the new value before update the bird position (and give directly the new position, new_x and new_y).

The other solution is to add a return statement in the method FlappyBird.move() with the new coordinates, so when you call bird.move(bird_x_move, bird_y_move), you can assign the returned value to new_x and new_y.

# class bird
def move(self, dx, dy):
    self.y += dy
    self.x += dx
    return(self.x, self.y)

# class MainWindow    
new_x, new_y = bird.move(bird_x_move, bird_y_move)
#[...]
pipe1.check_if(new_y)
pipe2.check_if(new_y)
pipe3.check_if(new_y)
    
Thank you very much I really aprecciate it! – 20021307 StrozeR 9 hours ago
    
@20021307StrozeR glad you can move to the next step! by the way, you should extract the loop from the main class, this exercise will make you dig into classes – PRMoureu 9 hours ago
    
Thanks.. ill look into it. – 20021307 StrozeR 8 hours ago