在我被推荐到这里的另一个问题之前,我已经检查了它,它没有帮助我伤心; - ; Soz我开始在游戏中制作一个游戏,松散地跟随一个视频并且我试图使用groupcollide来检查子弹和暴徒的碰撞,但由于某种原因,暴徒在被击中后不会消失或被发现,任何人都可以看一看帮我?
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()
答案 0 :(得分:1)
我无法真正测试您的代码,因为我没有您的图片,但我认为我看到了问题:
当你产生子弹时,你有:
bullets.add(bullets)
bullets
是组,而bullet
是您刚刚产生的子弹。我想你的意思是:
bullets.add(bullet)
我还在这里看到另外两个问题:
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
将成为您的碰撞列表。首先,你说如果hits
不为空,则退出游戏。如果子弹击中暴徒,你真的希望游戏退出吗?
其次,for hits in hits
没有意义。我认为你的意思是for hit in hits
,它会遍历点击列表,并调用每一个hit
。