首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘鸡’对象没有属性'rect‘

‘鸡’对象没有属性'rect‘
EN

Stack Overflow用户
提问于 2016-01-14 12:49:31
回答 1查看 4K关注 0票数 3

我目前正在用python编写一个更大的程序。这是一个简单的游戏,但我有一个错误。有人能帮我吗?

误差

代码语言:javascript
复制
          Traceback (most recent call last):
  File "C:/Users/kkuja/Desktop/game.py", line 36, in <module>
    MainWindow.MainLoop()
  File "C:/Users/kkuja/Desktop/game.py", line 17, in MainLoop
    self.chicken_sprites.draw(self.screen)
  File "C:\Users\kkuja\AppData\Local\Programs\Python\Python35\lib\site-packages\pygame\sprite.py", line 475, in draw
    self.spritedict[spr] = surface_blit(spr.image, spr.rect)
AttributeError: 'Chicken' object has no attribute 'rect'

代码语言:javascript
复制
import os, sys
import pygame

class Game:
    def __init__(self, width=640, height=480):
        pygame.init()
        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode([self.width, self.height])
    def MainLoop(self):
        self.ChickenLoad();

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        self.chicken_sprites.draw(self.screen)
        pygame.display.flip()

    def ChickenLoad(self):
        self.chicken = Chicken()
        self.chicken_sprites = pygame.sprite.Group(self.chicken)

class Chicken(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("chic.jpg")


 if __name__ == "__main__":
    MainWindow = Game()
    MainWindow.MainLoop()

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-14 13:20:56

在您的代码中的函数self.chicken_sprites.draw(self.screen)中,chicken.rect试图被访问,但您没有定义它。

如果您引用正式文件,您可以找到以下代码:

代码语言:javascript
复制
class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
       self.image = pygame.Surface([width, height])
       self.image.fill(color)

       # Fetch the rectangle object that has the dimensions of the image
       # Update the position of this object by setting the values of rect.x and rect.y
       self.rect = self.image.get_rect()

如果不在您的self.rect中设置Chicken,它应该如下所示。

代码语言:javascript
复制
class Chicken(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("chic.jpg")
        self.rect = self.image.get_rect(); #here rect is created
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34790063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档