Reputation: 103
Before I get recommended to another question on here, I already checked it out, it didnt help me sadly ;-;. Soz i started making a game in pygame, loosely following a video and im trying to use groupcollide to check the collide of a bullet and a mob, but for some reason the mobs wont disappear or detect after being hit, can anyone take a look and help me?
import pygame, random, os
width = 400
height = 600
fps = 60
# usefule images
rocket_img = pygame.image.load("rocket_ship.png")
astroid_img = pygame.image.load("astroid.png")
nebulous = pygame.image.load("nebulous.jpg")
# usefull colors RGB
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
yellow = (255, 255, 0)
class Player(pygame.sprite.Sprite):
# player
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = rocket_img
self.image.set_colorkey(black)
self.rect = self.image.get_rect()
self.rect.center = (width / 2, height - 80)
self.speedx = 0
def update(self):
self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -5
if keystate[pygame.K_RIGHT]:
self.speedx = 5
self.rect.x += self.speedx
if self.rect.right > width:
self.rect.right = width
elif self.rect.left < 0:
self.rect.left = 0
def shoot(self):
bullet = Bullets(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullets)
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 50))
self.image.fill(red)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(width - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
self.speedx = random.randrange(-3, 3)
def update(self):
self.rect.x += self.speedx
self.rect.y += self.speedy
if self.rect.top > height +10 or self.rect.left < -25 or self.rect.right > width + 20:
self.rect.x = random.randrange(width -self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
class Bullets(pygame.sprite.Sprite):
def __init__(self, x , y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill(yellow)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
# starts our pygame
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((width, height)) # things to run the game
pygame.display.set_caption("PyGame Shmup!")
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
bullets = pygame.sprite.Group()
mobs = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
m = Mob()
all_sprites.add(m)
mobs.add(m)
running = True
while running:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
# update
all_sprites.update()
# check or collisions
hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
if hits:
pygame.quit()
for hits in hits:
m = Mob()
all_sprites.add(m)
mobs.add(m)
hits = pygame.sprite.spritecollide(player, mobs, False)
if hits:
running = False
pygame.quit()
# render
screen.blit(nebulous, (0, 0))
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
Upvotes: 0
Views: 240
Reputation: 898
I can't really test your code, since I don't have your images, but I think I see the problem:
When you spawn the bullet you have:
bullets.add(bullets)
bullets
is the group and bullet
is the bullet you just spawned. I think you meant:
bullets.add(bullet)
I also see two other problems here:
hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
if hits:
pygame.quit()
for hits in hits:
m = Mob()
all_sprites.add(m)
mobs.add(m)
hits
will be your list of collisions. First, you're saying that if hits
is not empty, quit the game. Do you really want the game to quit if a bullet hits a mob?
Second, for hits in hits
doesn't make sense. I think you meant for hit in hits
which would loop through the list of hits, calling each one hit
.
Upvotes: 1