【发布时间】:2017-09-01 14:11:39
【问题描述】:
我最近一直在学习 python,我刚刚发现了 sprites。它们看起来真的很有用,我一直在尝试制作一个游戏,你必须吃掉所有的红苹果(健康),而不是蓝苹果(发霉)。当我尝试运行它时出现错误,它说:
line 32, in <module>
apples.rect.y = random.randrange(displayHeight - 20)
AttributeError: type object 'apples' has no attribute 'rect'
对不起,如果我犯了一个非常愚蠢的错误,但我一直在其他地方寻找答案,但我找不到。这是我的整个主要代码:
import pygame
import random
pygame.init()
displayWidth = 800
displayHeight = 600
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
gameCaption = pygame.display.set_caption("Eat The Apples")
gameClock = pygame.time.Clock()
class apples(pygame.sprite.Sprite):
def __init__(self, colour, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20, 10])
self.image.fill(red)
self.rect = self.image.get_rect()
applesList = pygame.sprite.Group()
allSpriteList = pygame.sprite.Group()
for i in range(50):
apple = apples(red, 20 , 20)
apples.rect.y = random.randrange(displayHeight - 20)
apples.rect.x = random.randrange(displayWidth - 20)
applesList.add(apple)
allSpriteList.add(apple)
player = apples(green, 20, 20)
def gameLoop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.display.update()
gameClock.tick(60)
gameLoop()
pygame.quit()
quit()
感谢您的阅读,期待您的回复! (P.S. 如果您想知道,这段代码还没有完全完成)
【问题讨论】:
标签: python-3.x pygame rect