我目前正在制作一款游戏,并且具有mainDisplay
,sprite
,testSprite
类和函数spriteAnimate
:
class mainDisplay:
def __init__(self):
self.screenDisplay = pygame.display.set_mode(
[1200, 720]) # create game window SURFACE object (screen display is a SURFACE)
pygame.display.set_caption("Blue") # set window name
class sprite(pygame.sprite.Sprite):
def __init__(self, colWidth, colHeight,framesNo, spriteGroup='', spawnX=0, spawnY=0,):
super().__init__()
self.xSpawn = spawnX
self.ySpawn = spawnY
self.spawnCoords = (spawnX, spawnY)
self.surface = pygame.Surface((colWidth, colHeight)) # make sprite surface
self.rect = pygame.Rect(self.xSpawn, self.ySpawn, colWidth, colHeight)
self.masks = [None]*framesNo
self.index = 0
self.image = self.masks[self.index]
if spriteGroup != '':
spriteGroup.add(self) # if there is a sprite group, add it
def next(self):
self.index += 1
if self.index >= len(self.masks):
self.index = 0
print(self.index)
self.image = self.masks[self.index]
class testSprite(sprite):
def __init__(self, mainDisplay):
self.mask1 = pygame.image.load(
os.path.join('D:\BLUE\code\spriteIMG', 'testmask1.png')) # sets the images to png images
self.mask2 = pygame.image.load(os.path.join('D:\BLUE\code\spriteIMG', 'testmask2.png'))
self.mask3 = pygame.image.load(os.path.join('D:\BLUE\code\spriteIMG', 'testmask3.png'))
super().__init__(640, 640, 4, testGroup)
self.masks[0] = self.mask1
self.masks[1] = self.mask2
self.masks[2] = self.mask3
self.masks[3] = self.mask2
def spriteAnimate(sprite1, spriteGroup1, clock,display):
framerate = 100
while sprite1.alive():
spriteGroup1.draw(mainDisplay.screenDisplay)
sprite1.next()
pygame.display.flip()
currentTime = pygame.time.get_ticks()
clock.tick(5)
它在功能mainGame
中实现:
def mainGame(display):
display.screenDisplay.fill(colours.buttonBlue)
errorMessage = text((515), ((720 / 20) * 2), 'Candara', 38, colours.buttonDarkC, 'main game is under construction',
display)
test1 = testSprite(display)
spriteClock = pygame.time.Clock()
spriteAnimate(test1,testGroup,spriteClock,display)
当我尝试运行此代码时,相对于spriteAnimate()中的行,它显示错误'AttributeError:类型对象'mainDisplay'没有属性'screenDisplay': spriteGroup1.draw(mainDisplay.screenDisplay)
我最初是通过传递参数来完成此操作的(错误有所不同:
TypeError:参数1必须为pygame.Surface,而不是None
但是,然后继续尝试直接引用mainDisplay
类。我不确定我是否一直在盯着代码看太久,找不到面对我的东西。
当前回溯:
Traceback (most recent call last):
File "D:/BLUE/code/testing.py", line 200, in <module>
main()
File "D:/BLUE/code/testing.py", line 193, in main
firstScreen(display, screen1, maingame) # starts first screen
File "D:/BLUE/code/testing.py", line 113, in firstScreen
mainMenu(quitButton, startButton, continueButton, mainDisplay)
File "D:/BLUE/code/testing.py", line 93, in mainMenu
mainGame(mainDisplay)
File "D:/BLUE/code/testing.py", line 174, in mainGame
spriteAnimate(test1,testGroup,spriteClock,display)
File "D:/BLUE/code/testing.py", line 126, in spriteAnimate
spriteGroup1.draw(mainDisplay.screenDisplay)
AttributeError: type object 'mainDisplay' has no attribute 'screenDisplay'
Process finished with exit code 1