0

I am making a flappy bird replica and I can't get a collision mechanism. At the moment i'm trying to use .coliderect() but i'm not 100% sure how to. Here is the two classes. I'd like the program to do something, or just print('x') when the bird and the pipe collide, but when they collide at the moment the program does not do or output anything.

import pygame vec = pygame.math.Vector2 BLACK = (0,0,0) WIDTH = 500 HEIGHT = 400 pygame.init() window = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Flappy Bird') clock = pygame.time.Clock() class Bird(): def __init__(self): self.skin = pygame.image.load('bird2.png') self.rect = self.skin.get_rect() self.rect.center = (WIDTH / 2, HEIGHT / 2) self.vx = 0 self.vy = 0 self.pos = vec(WIDTH / 2, HEIGHT / 2) self.vel = vec(0, 0) self.acc = vec(0, 0) def update(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() window.fill(BLACK) self.acc = vec(0, 0.7) #having 0.5 adds gravity self.vy = 0 keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: self.vel.y = -7 if keys[pygame.K_LEFT]: self.acc.x = -0.5 #change to y to add vertical motion if keys[pygame.K_RIGHT]: self.acc.x = 0.5 #change to y to add vertical motion #applys friction self.acc.x += self.vel.x * -0.08 #FRICTION #motion equations self.vel += self.acc self.pos += self.vel + 0.5 * self.acc self.rect.center = self.pos window.blit(self.skin, self.pos) class Pipe(): def __init__(self,x,y): self.image = pygame.Surface((50,60)) self.image.fill(RED) self.rect = self.image.get_rect() self.pos = vec(x,y) def blit_pipe(self): window.blit(self.image, self.pos) def border_check(): if (flappy.pos.y)+32 > HEIGHT: #this is the very top of the flappy print("You are under\n") pygame.quit() quit() if flappy.pos.y < 0: print("You are over\n") #this is the very top of the flappy pygame.quit() quit() pipey = Pipe(300,200) pipey.blit_pipe() pipey2 = Pipe(100,200) pipey2.blit_pipe() flappy = Bird() window.blit(flappy.skin, flappy.pos) while True: border_check() flappy.update() pipey.blit_pipe() pipey2.blit_pipe() if flappy.rect.colliderect(pipey.rect): print('x') clock.tick(30) pygame.display.update() 
1
  • Sorry, I've tried to shorten it down to this Commented Dec 23, 2017 at 21:37

1 Answer 1

1

You never set the position of the rects of the Pipe instances so they are still positioned at the default coords (0, 0). There are several ways to set the coords, for example you can pass the coords as the topleft argument to get_rect.

self.rect = self.image.get_rect(topleft=(x, y)) 

If something is wrong with the collision detection, print the rects to see the actual positions.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh thank you! Why is it specifically topleft? Is it because that is there the points (0,0) are or not?
It just sets the top left coords of the rect to x and y. You can also use the other rect attributes, for example center, bottomleft, or just x, y: self.image.get_rect(x=x, y=y). Alternatively, you can do the same as in the Bird class and set the position in the next line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.