Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to add a bomb into my flappy bird game, but it won't detect the collision. here is the code

Tags:

pygame

I want to add a bomb into my flappy bird game, but it won't detect the collision. I cannot get the bird to be detected if it made contact with the bomb The bomb shows up, but I can't make it detect the collision with the bird, here is the code I have so far.Also,i did not class a new object for the bomb, I put it in with the pipes. I think there is a problem with the colliderect If you need the images please tell me. Can someone help me? I would be really grateful. Thanks in advance.

import pygame
from pygame.locals import *  # noqa
import sys
import random


class FlappyBird:
    def __init__(self):
        self.screen = pygame.display.set_mode((400, 708))
        self.bird = pygame.Rect(65, 50, 50, 50)
        self.background = pygame.image.load("background.png")
        self.birdSprites = [pygame.image.load("1.png"),
                            pygame.image.load("2.png"),
                            pygame.image.load("dead.png")]
        self.wallUp = pygame.image.load("bottom.png")
        self.wallDown = pygame.image.load("top.png")
        self.bomb = pygame.image.load("bomb.png")
        self.bombx = 600
        self.gap = 130
        self.wallx = 400
        self.birdY = 350
        self.jump = 0
        self.jumpSpeed = 10
        self.gravity = 5
        self.dead = False
        self.sprite = 0
        self.counter = 0
        self.offset = random.randint(-110, 110)

    def updateWalls(self):
        self.wallx -= 2
        if self.wallx < -80:
            self.wallx = 400
            self.counter += 1
            self.offset = random.randint(-110, 110)
            self.bombx -= 2
        if self.bombx < -80:
            self.bombx = 600
            self.counter += 1
            self.offset = random.randint(-110, 110)


    def birdUpdate(self):
        if self.jump:
            self.jumpSpeed -= 1
            self.birdY -= self.jumpSpeed
            self.jump -= 1
        else:
            self.birdY += self.gravity
            self.gravity += 0.2
        self.bird[1] = self.birdY
        upRect   = pygame.Rect(self.wallx,
                               360 + self.gap - self.offset + 10,
                               self.wallUp.get_width() - 10,
                               self.wallUp.get_height())
        downRect = pygame.Rect(self.wallx,
                               0 - self.gap - self.offset - 10,
                               self.wallDown.get_width() - 10,
                               self.wallDown.get_height())
        bombRect = pygame.Rect(self.bombx,
                               0 - self.gap - self.offset - 10,
                               self.bomb.get_width() - 10,
                               self.bomb.get_height()
                               )
        if upRect.colliderect(self.bird):
            self.dead = True
        if downRect.colliderect(self.bird):
            self.dead = True
        if bombRect.colliderect(self.bird):
            self.bomb = pygame.image.load("bombexplode")
            self.dead = True
        if not 0 < self.bird[1] < 720:
            self.bird[1] = 50
            self.birdY = 50
            self.dead = False
            self.counter = 0
            self.wallx = 400
            self.offset = random.randint(-110, 110)
            self.gravity = 5

    def run(self):
        clock = pygame.time.Clock()
        pygame.font.init()
        font = pygame.font.SysFont("Arial", 50)
        while True:
            clock.tick(60)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not self.dead:
                    self.jump = 17
                    self.gravity = 5
                    self.jumpSpeed = 10

            self.screen.fill((255, 255, 255))
            self.screen.blit(self.background, (0, 0))
            self.screen.blit(self.wallUp,
                             (self.wallx, 360 + self.gap - self.offset))
            self.screen.blit(self.wallDown,
                             (self.wallx, 0 - self.gap - self.offset))
            self.screen.blit(self.bomb,
                             (self.bombx, 0 - self.gap - self.offset))
            self.screen.blit(font.render(str(self.counter),
                                         -1,
                                         (255, 255, 255)),
                             (200, 50))
            if self.dead:
                self.sprite = 2
            elif self.jump:
                self.sprite = 1
            self.screen.blit(self.birdSprites[self.sprite], (70, self.birdY))
            if not self.dead:
                self.sprite = 0
            self.updateWalls()
            self.birdUpdate()
            pygame.display.update()

if __name__ == "__main__":
    FlappyBird().run()
like image 311
Leo Liu Avatar asked Jan 19 '26 19:01

Leo Liu


1 Answers

I don't know much about using sprites, but if you can get the position and size of each object, you can give them rectangular hitboxes and check if their corners are inside each other: image Another way is to use circular hitboxes if those fit better. For these, find the distace between the centers and check if it is smaller than their radius' combined.

Edit :

You can use

self.bird = pygame.Rect(65, 50, 50, 50)
# creating bomb hitbox
self.rect_bomb = pygame.Rect(100, 100, 200, 200)

if self.bird.colliderect(self.rect_bomb):
    # end up your code
like image 196
pythonian 23 Avatar answered Jan 21 '26 11:01

pythonian 23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!