0
我是pygame的新手,決定在我的遊戲中實現一些精靈,而不僅僅是在物體周圍移動。我想得到它,所以我在每次玩家遇到障礙物時都增加分數,但是我在碰撞方法中遇到了很多麻煩。但是,每當我運行程序時,我都會收到這個錯誤。[pygame error message]類型對象'sprite'沒有屬性'sprite'
Traceback (most recent call last):
File "rungameforryan.py", line 214, in <module>
blocks_hit_list = pygame.sprite.spritecollide(player, sprite.sprites, True)
AttributeError: type object 'sprite' has no attribute 'sprites'
建議非常感謝。 這裏是遊戲循環:
from classyforryan import ourMusic, sprites
import pygame
import os
import time
pygame.init() #short for initialize does return a tuple of successful intilizaton
colors={"black":(0,0,0), "white": (255, 255, 255), "red": (255, 0, 0), "green": (0, 255, 0), "purple": (164, 66, 244), "pink" :(252, 25, 123)}
gameDisplay= pygame.display.set_mode((800, 600))
bg=pygame.image.load('clouds_converted.jpg')
moore=pygame.image.load("baemoore_converted.png")
jump = False
fall = False
cube = pygame.image.load("cube.png")
player=sprites(moore, 50, 544)
ob = sprites(cube, 2125, 580)
ob1 = sprites(cube, 2125, 580)
ob2 = sprites(cube, 2125, 580)
pygame.display.set_caption("lets play!")
gameExit = False
clock = pygame.time.Clock()
class sprite(pygame.sprite.Sprite):
def __init__(self, ob, ob1, ob2, image, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("1136693.jpg") #edited to add self. :)
self.spritesgroup = pygame.sprite.Group() #sprite group
self.spritesgroup.add(ob)
self.spritesgroup.add(ob1)
self.spritesgroup.add(ob2)
while not gameExit:
clock.tick(40)
timer = pygame.time.get_ticks()
print(timer)
for event in pygame.event.get():
if event.type==pygame.QUIT:
gameExit=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
gameExit= True
pygame.display.update()
gameDisplay.blit(bg,[0,0])
time.sleep(.05)
player.pos()
ob.pos()
ob.left()
ob1.pos()
ob2.pos()
blocks_hit_list = pygame.sprite.spritecollide(player, sprite.spritesgroup, True)
for block in blocks_hit_list:
score +=1
print(score)
pygame.quit() #unintiliazes pygames
quit() #this will exit out of python
,這裏是中導入的所有類:
import pygame
import os
import time
gameDisplay= pygame.display.set_mode((800, 600))
class sprites(pygame.sprite.Sprite):
def __init__(self, image, xcoor, ycoor):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rectx=xcoor
self.recty=ycoor
def pos(self):
gameDisplay.blit(self.image, [self.rectx, self.recty])
def left(self):
dist = 7
self.rectx = self.rectx - dist
嗨,所以我試過這個,但即使添加了自我,我仍然得到相同的錯誤。我需要在程序中的其他地方調用該課程嗎? –
@ S.Mahmud:對不起,我可以發誓你實例化了你的類的一個實例。我會稍微調整一下我的答案。 – Gerrat
好吧,所以我修復它,以便它不會再給我這個錯誤,但現在它不會接受任何障礙打擊.. //我現在看我的代碼進一步解釋我如何修復//我也加了自己。看到你的建議後,別擔心 –