0

I am pretty fresh to this site so sorry if I do anything wrong. I would need some help with making the player collide with the pipes in my Flappy Bird clone. None of the methods I can find in pygame work for me because of how weirdly I coded the pipes (the Pipe() class doesn't have the 'rect' argument which is necessary for those to work AFAIK) but I have no clue how to implement it any other way for them to still have the gap between them. Please help, thanks in advance!

import pygame
from pygame.locals import *
import random

pygame.init()


screen_height = 1280
screen_width = 720
screen = pygame.display.set_mode([screen_width, screen_height])
vec = pygame.math.Vector2
jump_power = 16.5 #9.5
FPS = 60 #144
FramePerSec = pygame.time.Clock()


spawnpipe = pygame.userevent + 1
pygame.time.set_timer(spawnpipe, 2250)


class Player(pygame.sprite.Sprite):
    
    def __init__(self): 
        super(Player, self).__init__()
        self.surf = pygame.Surface([70, 70])
        self.surf.fill([255, 255, 102])
        self.rect = self.surf.get_rect()
        
        self.pos = vec(screen_width / 2, screen_height / 2)
        self.vel = vec(0,0)
        self.acc = vec(0,0)
        
    def move(self):
        self.acc = vec(0, 0.6) #0.17
        self.vel += self.acc
        self.pos += self.vel + 0.5 * self.acc
        
        if self.pos.y > screen_height:
            self.pos.y = screen_height
        if self.pos.y < 70:
            self.pos.y = 70
            self.vel.y += 1.5
            
        self.rect.midbottom = self.pos 
        
    def jump(self):
        self.vel.y = -jump_power
        
        
class Pipe(pygame.sprite.Sprite):

    def __init__(self):
        super(Pipe, self).__init__()
        self.speed = 4
        self.width = 130
        self.gap_begin = random.randint(300, 780)
        self.gap = random.randint(290, 325)
        
        self.surf_top = pygame.Surface([self.width, self.gap_begin])
        self.surf_top.fill([0, 204, 0])
        self.pipe_top = [self.surf_top, [screen_width, 0], self.gap_begin]
        
        self.surf_bot = pygame.Surface([self.width, screen_height - (self.gap_begin + self.gap)])
        self.surf_bot.fill([204, 0, 0])
        self.pipe_bot = [self.surf_bot, [screen_width, self.gap_begin + self.gap], screen_height - (self.gap_begin + self.gap)]
        
    def update(self):
        self.pipe_top[1][0] -= self.speed 
        self.pipe_bot[1][0] -= self.speed
        
        if self.pipe_bot[1][0] + self.width < 0:
            pipes.remove(self.pipe_bot)
            pipes.remove(self.pipe_top)

player = Player()
pipes = pygame.sprite.Group()

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            
            if event.key == K_SPACE or event.key == K_UP:
                player.jump()   
        
        elif event.type == spawnpipe:
            new_pipe = Pipe()
            pipes.add(new_pipe)


    player.move()
    pipes.update()
    
    screen.fill([153, 204, 255])
    screen.blit(player.surf, player.rect)
  
    for entity in pipes:
       screen.blit(entity.pipe_top[0], (entity.pipe_top[1][0], entity.pipe_top[1][1]))
       screen.blit(entity.pipe_bot[0], (entity.pipe_bot[1][0], entity.pipe_bot[1][1]))
    
    
    pygame.display.flip()
    FramePerSec.tick(FPS)
    

pygame.quit()
Cheehee
  • 13
  • 3
  • Use [`pygame.Rect`](https://www.pygame.org/docs/ref/rect.html) objects and [`collidelist()`](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect). You don't need `.rect` attributes. – Rabbid76 May 31 '21 at 19:31
  • `entity_rect = entity.pipe_top[0].get_rect(topleft = entity.pipe_top[1])` – Rabbid76 May 31 '21 at 19:37
  • `if entity_rect.colliderect(player.rect):` `print('hit')` – Rabbid76 May 31 '21 at 19:38
  • 1
    Thank you so much @Rabbid76!! It turns out that it was really simple after all, I tried a few times to create the rectangle based on the surface of the pipe but just couldn't make it work in the end. I really appreciate the help! – Cheehee May 31 '21 at 19:52

0 Answers0