AttributeError:'播放器'对象没有属性' sprites'

时间:2016-04-18 11:45:23

标签: python pygame python-3.4

我正在使用PyGame和Python 3.4构建游戏。我遇到的问题似乎是模块本身而不是我的实际代码,因为完整的错误是:

    Traceback (most recent call last):
  File "C:\Users\----\Desktop\Python\Game.py", line 115, in <module>
    playerhit = pygame.sprite.spritecollide(ebullet, psprite, True)
  File "C:\Python34\lib\site-packages\pygame\sprite.py", line 1514, in spritecollide
    for s in group.sprites():
AttributeError: 'Player' object has no attribute 'sprites'

我冲出了我的名字但保留了长度,但我认为这不重要。

我在网上浏览了错误的不同部分并没有出现任何问题,所以我来到这里。这里是完整的代码(它有点混乱和冗长):

import pygame, time, sys, random, Sprites, os
from pygame.locals import *

BLACK = (0, 0, 0)
MAPX = 10
MAPY = 20
TXY = 32

gameover = False
score = 0

lives = 2

playerXY = [MAPX*TXY//2, MAPY*TXY-TXY-TXY-5]

spritesheet01 = Sprites.Sheet(32, 32, 128, 32, 'spritesheet03.png')
sprites = spritesheet01.clipSheet()

psprite = Sprites.Player(sprites[0])

clock = pygame.time.Clock()
counter = int(clock.get_fps())

ebullet = Sprites.eBullet(sprites[3])

allspriteslist = pygame.sprite.Group()
enemylist = pygame.sprite.Group()
ebulletlist = pygame.sprite.Group()
pbulletlist = pygame.sprite.Group()

allspriteslist.add(psprite)

pygame.init()

enemynum = 5
enemies = []
enemytorf = [False, False, False, False, False, False, False, False, False, False]

def pause():
    os.system("pause")

def enemyCreate():
    print("enemycreate called")
    while len(enemylist) < 5:
        randomindex = random.randint(0, MAPX-1)
        if enemytorf[randomindex] == False:
            enemy = Sprites.Enemy(sprites[1])
            enemytorf[randomindex] = True
            enemy.rect.x = randomindex
            enemy.rect.y = 0
            ebullet.rect.x = randomindex
            ebullet.rect.y = 0
            enemylist.add(enemy)
            allspriteslist.add(enemy)
            ebulletlist.add(ebullet)
            allspriteslist.add(ebullet)
    print("enemycreate done")

def eBulletCreate():
    print("bulletpassed")
    for wi in range(len(ebull)):
        ebullappend = ebull[wi]
        ebullets.append(ebullappend)

print("Got through function defining")
display = pygame.display.set_mode((MAPX*TXY, MAPY*TXY))
pygame.display.set_caption("Star Smasher")
print("created screen")
pygame.key.set_repeat(10, 10)
endgame = False
create_enemy = pygame.USEREVENT + 1
shootbullet = pygame.USEREVENT + 2
print("created events")
enemyCreate()
pygame.time.set_timer(create_enemy, 7500)
pygame.time.set_timer(shootbullet, 2500)
print("Got to the while")
while endgame == False:
    print("got into while")
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_SPACE:
                pbullet = Sprites.pBullet(sprites[2])
                pbullet.rect.x = playerXY[0]
                pbullet.rect.y = playerXY[1]
                allspriteslist.add(pbullet)
                pbulletlist.add(pbullet)
            elif event.key == K_a:
                if playerXY[0] > 5:
                    playerXY[0] -= 5
            elif event.key == K_d:
                if playerXY[0] < MAPX*TXY-TXY-7:
                    playerXY[0] += 5
        if event.type == create_enemy:
            enemyCreate()
        if event.type == shootbullet:
            eBulletCreate()
    print("got through event handling")
    for bullet in pbulletlist:
        enemyhitlist = pygame.sprite.spritecollide(pbullet, enemylist, True)
        for enemy in enemyhitlist:
            pbulletlist.remove(bullet)
            allspriteslist.remove(bullet)
            score += 100
            print(score)
        if pbullet.rect.y < 0:
            pbulletlist.remove(bullet)
            allspriteslist.remove(bullet)
    for bullet in ebulletlist:
        playerhit = pygame.sprite.spritecollide(ebullet, psprite, True)
        for player in playerhit:
            ebulletlist.remove(bullet)
            allspriteslist.remove(bullet)
            gameover = True
        if ebullet.rect.y > MAPY*TXY:
            ebulletlist.remove(bullet)
            allspriteslist.remove(bullet)
    print("got through bullet handling")
    display.fill(BLACK)
    allspriteslist.draw(display)
    if gameover == True:
        font = pygame.font.Font(freesansbold.ttf, 20)
        font.render("Game Over, press any button to continue!", True, BLACK, background=None)
        display.blit(font, (160, 320, 200, 360))
        pause()
    pygame.display.update()
    clock.tick(60)
    print("Got to the display update")
pygame.quit()

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是错误的开始。 playerhit = pygame.sprite.spritecollide(ebullet, psprite, True) - 函数定义为spritecollide(sprite, group, dokill, collided = None),因此播放器应该是第一个,然后是子弹组。

另请发布Sprite.py代码。