首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Pygame错误:属性错误:没有属性get_rect()

Pygame错误:属性错误:没有属性get_rect()
EN

Stack Overflow用户
提问于 2020-11-28 00:45:59
回答 1查看 97关注 0票数 0

尝试将船图像添加到pygame终端的背景中,所以我创建了一个船类并将其保存在一个名为ship的模块中:

代码语言:javascript
运行
复制
import pygame

class Ship():

def __init__(self, screen):
    """Initialize the ship and set its starting position."""
    self.screen = screen
    
    # Load the ship image and get its rect.
    self.image = pygame.image.load('image/ship.bmp')
    self.rect = pygame.image.get_rect()
    self.screen_rect = screen.get_rect()
    
    # Start each new ship at the bottom center of the screen.
    self.rect.centerx = self.screen_rect.bottom
    
def blitme(self):
    """Draw the ship at its current position."""
    self.screen.blit(self.image, self.rect)

然后,我将这个类导入到我的主游戏文件中,并尝试运行代码,但我总是得到一个模块:AttributeError 'pygame.image‘没有get_rect属性。

下面是我的主游戏文件中的代码:

代码语言:javascript
运行
复制
import sys

import pygame

from settings import Settings
from ship import Ship

def run_game():
# Initialize pygame, settings and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
    (ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")

# Make a ship
ship = Ship(screen)

# Start the main loop for the game.

while True:
    
    # Watch for keyboard and mouse event.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
    # Redraw the screen during each pass through the loop.
    screen.fill(ai_settings.bg_color)
    ship.blitme()
            
    # Make the most recently drawn screen visible.
    pygame.display.flip()

run_game()

请问我做错了什么?我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2020-11-29 00:03:22

你的代码

代码语言:javascript
运行
复制
self.rect = pygame.image.get_rect()

应该是

代码语言:javascript
运行
复制
self.rect = self.image.get_rect()

例如,您想要使用pygame.image.load(...调用刚刚创建的图像的.rect。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65040926

复制
相关文章

相似问题

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