pygame模块错误与collsions

时间:2017-08-02 22:33:55

标签: python python-3.x pygame

我已经做了很长一段时间的shmup游戏了。我经常遇到的一个问题是碰撞。问题是,我的代码似乎完全正确,但我从pygame模块本身得到错误!

示例:

Traceback (most recent call last):
File "/home/pi/shooter_modified(1).py", line 101, in <module>
  hitregistry = pygame.sprite.groupcollide(enemies, player, True, True)
File "/usr/lib/python3/dist-packages/pygame/sprite.py", line 1549, in groupcollide
  for s in groupa.sprites():
AttributeError: 'list' object has no attribute 'sprites'

这是我自己的代码的某种问题,还是pygame模块本身的问题? 这是我的代码:

import pygame,random,os
from pygame.locals import *
'initialize pygame'
pygame.init()
'set variables'
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
white = (255,255,255)
width = 1280
height = 720
'create window'
screen =  pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
'sprite groups'
all_sprites = pygame.sprite.Group()
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
pygame.display.set_caption("kamikaze base defense")
'classes'

class Player(pygame.sprite.Sprite):
    def __init__(self):
        self.x, self.y = pygame.mouse.get_pos()
        pygame.sprite.Sprite.__init__(self)
        #self.image = pygame.Surface((32,32))++--3
        #$self.image.fill((green))
        self.image = pygame.image.load("vehicle.png")
        self.image.set_colorkey(white)
        self.rect = self.image.get_rect()
        self.rect.center = (width/2,700)
        self.speed = 0
    def update(self):
        self.keypress = pygame.key.get_pressed()
        if self.keypress[pygame.K_a]:
            self.speed = 3
            self.rect.x -= self.speed
        if self.keypress[pygame.K_d]:
            self.speed = 3
            self.rect.x += self.speed

        #self.rect.x += 1
        if self.rect.left > width:
            self.rect.right = 0
        if self.rect.right < 0:
            self.rect.left = 1280
    def shoot(self):
        bullet = Bullet(self.rect.centerx,self.rect.top)
        all_sprites.add(bullet)
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("missile.png")
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(50,width)
        self.rect.y = random.randrange(-100,-40)
        self.speedy = random.randrange(1,5)
        self.speedx = random.randrange(0,3)
    def update(self):
        self.rect.y = self.rect.y + self.speedy
        self.rect.x = self.rect.x + self.speedx
        if self.rect.top > height:
            self.rect.x = random.randrange(50,width)
            self.rect.y = random.randrange(-200,-100) 
            self.speedy = random.randrange(1,6)            
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((5,10))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.speedy = -5
    def update(self):
        self.rect.y -= 5
        if self.rect.bottom < 0:
            self.kill()


enemies = []
player = Player()
enemy1 = Enemy()
enemy2 = Enemy()
enemy3 = Enemy()
enemy4 = Enemy()
all_sprites.add(enemy1)
all_sprites.add(enemy2)
all_sprites.add(enemy3)
all_sprites.add(enemy4)
enemies.append(enemy1)
enemies.append(enemy2)
enemies.append(enemy3)
enemies.append(enemy4)
pygame.init()
while True:
    pygame.event.pump()
    'main loop'   
    all_sprites.add(player)
    screen.fill(black)
    all_sprites.draw(screen)
    hitregistry = pygame.sprite.groupcollide(enemies, player, True, True)
    if hitregistry:
        print("hit!")
        pygame.quit()
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullet = Bullet()
            bullet.rect.x = player.rect.x +50
            bullet.rect.y = player.rect.y
            all_sprites.add(bullet)
            bullets.add(bullet)
    bullets.draw(screen)
    bullethits = pygame.sprite.groupcollide(enemies,bullets,True,True)
    if bullethits:
       print("hit!")

2 个答案:

答案 0 :(得分:4)

你的错误发生在这里:

hitregistry = pygame.sprite.groupcollide(enemies, player, True, True)

groupcollide()需要两个Sprite Groups作为参数。您在执行enemiesbullets时正确使用它,但在此您传递的是enemies(一个组)和player(不是一个组)。您可能希望使用spritecollide()来测试Sprite与Group:

hitregistry = pygame.sprite.spritecollide(player, enemies, True)

另外,为什么all_sprites.add(player)循环中有while?您正在每帧添加播放器到该组...

答案 1 :(得分:1)

pygame.sprite.groupcollide前两个参数的类型为pygame.sprite.Group,但您要给它listPlayer

因此,您应该尝试enemies = []enemies.append(enemy1),而不是enemies = Group()enemies.add(enemy1)。可能需要进行其他更改。同样适用于玩家。尝试查看pygame documentation