0

I am currently struggling to create a code which drops bombs and if it hits the player the game will end. Everything runs fine apart from the collisions, I have tried many different methods to try and get it to work but unfortunately it hasn't. Any help would be appreciated. The error reads: , line 79, in bullets.checkCollision(bullet.image, player.image) AttributeError: 'Group' object has no attribute 'checkCollision'

import pygame
from pygame.locals import *
import os
import random

pygame.init()
lives = 3
#all needed modules

W, H= 1200, 800 
window = pygame.display.set_mode((W, H))# set the size of the window

pygame.display.set_caption("Als game") # caption for the window


#set up the players character
class Player(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        super().__init__()
        self.image= pygame.image.load("user proto.png").convert_alpha() # imports the image
        self.rect=self.image.get_rect()
        self.width = width
        self.height = height
        self.rect.x= 100
        self.rect.y=130 # sets where it will spawn
        runner.add(self)

    def move(self, dx, dy):
        #begin the movement feature by setting the locations
        self.rect.x+= dx
        self.rect.y+= dy
#set up the bomb/bullet class
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("bomby.png").convert_alpha()#loads in the image for the bullets
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(-800,800)#randomly spawn them in along the roof.
        self.rect.y = -200
        bullets.add(self)
    def move(self):
        self.rect.y += 1 #what direction they move in

    def checkCollision(self, sprite1, sprite2):
        col = pygame.sprite.collide_rect(sprite1, sprite2)
        if col == True:
            sys.exit()
#=====Loads in all sprite groups=====
players = pygame.sprite.Group()
bullets=pygame.sprite.Group()
bs=[]

bg = pygame.image.load(os.path.join("background.png")).convert() # loads the background
bg=pygame.transform.scale(bg,(1200,800)) # sizes the background
clock = pygame.time.Clock()# inserts a clock
score=0 # sets the initial score to 0

bullet_timer = 0 # begins the bullet timer


run = True

while run:
    # begin the game loop
    clock.tick(60) # sets the FPS
    bullet_timer += 1
    if bullet_timer == 60:
        bullet_timer = 0
        #spawn bullet
        Bullet(100, 410, 64, 64, 450)
    # make the bullets move
    for b in bullets:
        b.move()

    #collisions
    bullets.checkCollision(bullet.image, player.image)

    

    
    # shortens it so i dont have to type pygame.key.get_pressed() everytime.
    keys = pygame.key.get_pressed()

    # if the right arrow button or "d" is pressed move the x axis up 10, moving the character to the right.
    if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
        runner.move(10,0)
    # if the left arrow button or "a" is pressed move the x axis down 10, moving the character to the left.
    if keys[pygame.K_LEFT] or keys [pygame.K_a]:
        runner.move(-10,0)
 

    
    # draw in all the previously written functions.
    window.blit(bg, (0,0))
    players.draw(window)
    bullets.draw(window)

    if lives == 0:
        pygame.QUIT
       
    # if the player presses exit the game will end the cycle.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run=False
    # display what is happening.        
    pygame.display.flip()

# if the game loop ends, this will be performed, exiting the game.
pygame.quit()

1 Answers1

0

According to the official documentation on group it indeed does not have that checkCollision function. So the bullets object, which is a group of sprites, does not know about collision.

Check this answer for a great in-depth tutorial on collision detection in Pygame.

EDIT: Given that you wrote your own check, you could also indent the code to make sure it's in the loop for bullets and then run the function on each bullet instead.

JustLudo
  • 1,690
  • 12
  • 29