1

I was writing a pygame code to make a car game. It is a basic car game where the hero sprite car is supposed to avoid contact with the enemy sprite,so if it comes in contact with the enemy car it would shut down the screen.But it immediately shuts when I just run the script saying that the sprites have collided Here is the code:

import pygame
from pygame.locals import *
import sys
import random
 
        
road = pygame.image.load(r"C:\Users\Binoy\Downloads\ezgif.com-gif-maker-0.jpg")
screen = pygame.display.set_mode((800, 600))
class Enemy(pygame.sprite.Sprite):
    def __init__(self,image):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image = pygame.image.load(image)
        self.rect=self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
        

class Hero(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image = pygame.image.load("Untitled.png")
        self.rect=self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
hero=Hero()
enemy = Enemy("enemy.png")
enemy2 = Enemy("enemy 2.0.png")
enemy3 = Enemy("enemy 3.0.png")
enemy4 = Enemy("enemy 4.0.png")
enemies = pygame.sprite.Group([enemy, enemy2,enemy3,enemy4])

velocity = 2
roadx = 0
roady = 0
x=200 
y=400
y1=50
y2=350

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type== pygame.KEYDOWN and event.key ==pygame.K_LEFT:
            x-=200
            if x==-200:
                x=0
        elif event.type== pygame.KEYDOWN and event.key ==pygame.K_RIGHT:
            x+=200
            if x==800:
                x=600
        elif pygame.sprite.spritecollide(hero, enemies, True, pygame.sprite.collide_mask):
            print("sprites have collided!")
            pygame.quit()
            sys.exit() 
              
    

    roady = roady + velocity
    if roady == 600:
        roady = 0
    y=y-2
    y1-=2
    y2-=2
    if y==0:
        y=600
    if y1==0:
        y1=600 
    if y2==0:
        y2=600
    screen.blit(road, [roadx, roady -600])
    screen.blit(road, [roadx, roady])
    screen.blit(pygame.transform.scale(enemy.image , (150, 150)), [0,y1-600])
    screen.blit(pygame.transform.scale(enemy.image , (150, 150)), [0,y1])
    screen.blit(pygame.transform.scale(enemy2.image , (150, 150)), [400,y1-600])
    screen.blit(pygame.transform.scale(enemy2.image , (150, 150)), [400,y1])
    screen.blit(pygame.transform.scale(enemy3.image, (150, 150)), [610, y2-600])
    screen.blit(pygame.transform.scale(enemy3.image , (150, 150)), [610, y2])
    screen.blit(pygame.transform.scale(enemy4.image, (150, 150)), [200, y-600])
    screen.blit(pygame.transform.scale(enemy4.image , (150, 150)), [200, y])
    screen.blit(pygame.transform.scale(hero.image , (150, 150)), [x, 250])
    pygame.display.update()

So could anyone please help us in this code and give a solution to the error. Thank you.

Edit 1:

I just tried to to display the rect formed. So when I ran the script,I saw this: Screenshot of the problem

But I am unable to change the position of the rect .So how can we change the position of the rect formed? Thanks

Code2D
  • 119
  • 1
  • 14
  • 1
    because You call the function right away and it probably retuns True: `pygame.sprite.spritecollide(hero, enemies, True, pygame.sprite.collide_mask)` – Matiiss Apr 16 '21 at 12:15
  • 1
    also I suggest using a for loop to blit images – Matiiss Apr 16 '21 at 12:16

1 Answers1

1

pygame.sprite.spritecollide uses the rect attributes of the pygame.sprite.Sprite objects for the collision test. Use the location of the rect attributes instead of the global variables. At least you have to update the rect attributes before the collision test.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174