Python 3.6.2、Pygame 1.9.3 中的 Rect 错误
Rect error in Python 3.6.2, Pygame 1.9.3
我最近一直在学习 python,我刚刚发现精灵。它们看起来真的很有用,我一直在尝试制作一款游戏,你必须吃掉所有的红苹果(健康),而不是蓝苹果(发霉)。当我尝试 运行 时发生错误,它说:
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。如果你想知道这段代码还没有完全完成)
您正在尝试更改 class 的 rect
属性,而不是实例的矩形,并且由于 class 没有矩形,AttributeError
被提出。
apples.rect.y = random.randrange(displayHeight - 20)
apples.rect.x = random.randrange(displayWidth - 20)
只需将 apples
更改为 apple
(实例),它应该可以正常工作。
apple.rect.y = random.randrange(displayHeight - 20)
apple.rect.x = random.randrange(displayWidth - 20)
是的,就像skrx说的。你只需要挑出一个。
苹果中的苹果:
应该可以。
我最近一直在学习 python,我刚刚发现精灵。它们看起来真的很有用,我一直在尝试制作一款游戏,你必须吃掉所有的红苹果(健康),而不是蓝苹果(发霉)。当我尝试 运行 时发生错误,它说:
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。如果你想知道这段代码还没有完全完成)
您正在尝试更改 class 的 rect
属性,而不是实例的矩形,并且由于 class 没有矩形,AttributeError
被提出。
apples.rect.y = random.randrange(displayHeight - 20)
apples.rect.x = random.randrange(displayWidth - 20)
只需将 apples
更改为 apple
(实例),它应该可以正常工作。
apple.rect.y = random.randrange(displayHeight - 20)
apple.rect.x = random.randrange(displayWidth - 20)
是的,就像skrx说的。你只需要挑出一个。 苹果中的苹果:
应该可以。