尝试将船图像添加到pygame终端的背景中,所以我创建了一个船类并将其保存在一个名为ship的模块中:
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属性。
下面是我的主游戏文件中的代码:
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()
请问我做错了什么?我该如何解决这个问题?
发布于 2020-11-29 00:03:22
你的代码
self.rect = pygame.image.get_rect()
应该是
self.rect = self.image.get_rect()
例如,您想要使用pygame.image.load(...
调用刚刚创建的图像的.rect。
https://stackoverflow.com/questions/65040926
复制相似问题