晚上好,
我正在 Pygame 中创建一个横向卷轴游戏,但在调整船速和限制航程时遇到了麻烦。我是 python 新手,仍在学习过程中。
我不断收到 AttributeError:“Ship”对象没有属性“screen_rect”
请我被卡住了,需要了解我做错了什么。
这是我到目前为止的代码:
import pygame
class Ship:
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.settings = ai_game.settings
# Load the ship image and get its rect.
self.image = pygame.image.load('images/Blue-5.bmp')
self.rect = self.image.get_rect()
# Start each new ship at center left of screen.
self.rect.midleft = self.screen_rect.midleft
# Store a decimal value for the ship's horizontal position.
self.x = float(self.rect.x)
self.y = float(self.rect.y)
# Movement Flag
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
"""Update the ship's position based on the movement flag."""
# Update the ship's x value, not the rect
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.x -= self.settings.ship_speed
if self.moving_up and self.rect.up < self.screen_rect.up:
self.y -= self.settings.ship_speed
if self.moving_down and self.rect.down > 0:
self.y += self.settings.ship_speed
# Update rect object from self.x.
self.rect.x = self.x
self.rect.y = self.y
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)