0

晚上好,

我正在 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)

4

2 回答 2

0

用于pygame.time.Clock控制每秒帧数,从而控制游戏速度。

对象的方法tick()pygame.time.Clock这种方式延迟游戏,循环的每次迭代消耗相同的时间段。
这意味着循环:

clock = pygame.time.Clock()
run = True
while run:
    clock.tick(60)

    # [...]

每秒运行 60 次。


pygame.time.Clock.tick返回自上次调用以来经过的毫秒数。如果您在应用程序循环中调用它,那么这是自上一帧以来经过的毫秒数。
将玩家的速度乘以每帧经过的时间,以获得独立于 FPS 的恒定运动。

例如,以像素数定义玩家每秒应移动的距离 ( move_per_second)。然后在应用循环中计算每帧的距离:

move_per_second = 500
FPS = 60
fun = True
clock = pygame.time.Clock()
while run:
    ms_frame = clock .tick(FPS)
    move_per_frame = move_per_second * ms_frame / 1000  

    # [...]
于 2020-12-02T15:12:32.303 回答
-2

这可能会帮助您解决问题。不要删除或替换 self.screen_rect = ai_game.screen.get_rect() w/ self.settings = ai_game.settings(像我一样保留它们)

船.py

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.screen_rect = ai_game.screen.get_rect() # Keep this line
        self.settings = ai_game.settings # Add this line as require*

        # Load the ship image and get it react.
        self.image = pygame.image.load('image/ship.bmp')
        self.rect = self.image.get_rect()

        # Start each new ship at the bottom center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

        # Store a decimal value for the ship's horiontal & vertical position.
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

        # Movement flags
        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 flags."""
        # 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
        elif self.moving_left and self.rect.x > 0:
            self.x -= self.settings.ship_speed
        elif self.moving_up and self.rect.y > 0:
            self.y -= self.settings.ship_speed
        *elif self.moving_down and self.rect.bottom < self.screen_rect.bottom*: # Ship stop at bottom edge
            self.y += self.settings.ship_speed

        # Update rect object from self.x & self.y
        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)

外星人入侵.py

import sys
import pygame

from settings import Settings
from ship import Ship

class AlienInvasion:
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initialize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        # Create ship
        self.ship = Ship(self)

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()
            self.ship.update()
            self._update_screen()

    def _check_events(self):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_keydown_events(self, event):
        """Respond to keypresses."""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_UP:
            self.ship.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = True
        elif event.key == pygame.K_q:
            sys.exit()

    def _check_keyup_events(self, event):
        """Respond to key release."""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False
        elif event.key == pygame.K_UP:
            self.ship.moving_up = False
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = False

    def _update_screen(self):
        """Update images on the screen, and flip to new screen."""
        self.screen.fill(self.settings.background_color)
        self.ship.blitme()

        # Make the most recently drawn screen visible.
        pygame.display.flip()

if __name__ == '__main__':
    # Make a game instance, and run the game
    ai = AlienInvasion()
    ai.run_game()
于 2021-02-25T04:21:34.427 回答