嘿,我不能在我的 pygame 程序中使用 middottom 函数。每次我 运行 我的程序都会在左下角看到我的矩形对象

hey,I coudn't use the midbottom function in my pygame program. Every time I run my program I get to see my rect object on the bottom left

import pygame

class Ship:
    """A class to manage the ship."""

    def __init__(self, ai_game):
        """Initialize the ship and see its starting position."""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings

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

        # Store a decimal value for the ship's horizontal position
        self.x = float(self.rect.x)

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

        # Movement flag
        self.moving_right = False
        self.moving_left = False

    def update(self):
        """"Update the ship's position based on movement flag."""
        # Update the ship's x value and 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

        # Update rect object from self.x
        self.rect.x = self.x

    def blitme(self):
        """Draw the ship at the current location."""
        self.screen.blit(self.image, self.rect)

这是代码,其中 ai_game 是来自主 class 的实例。 每次我 运行 我的代码都会在屏幕的左下角看到我的 rect 对象,而 midbottom 函数应该将我的 rect 对象(即这里的船)放在屏幕的 midbottom 部分

设置self.rect.midbottom = self.screen_rect.midbottom不够。您还需要设置 self.xself.screen_rect.centerx

self.rect.midbottom = self.screen_rect.midbottom
self.x = self.screen_rect.centerx

注意在update方法中, self.rect.x是由self.x设置的。因此 self.x 属性需要正确初始化

如果您不需要浮点精度,您可以完全删除 self.x 属性并改用 slef.rect.centerx

Class Ship:

class Ship:
    """A class to manage the ship."""

    def __init__(self, ai_game):
        """Initialize the ship and see its starting position."""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings

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

        # Start each ship at the bottom centre of the screen
        self.rect.midbottom = self.screen_rect.midbottom
        
        # Movement flag
        self.moving_right = False
        self.moving_left = False

    def update(self):
        """"Update the ship's position based on movement flag."""
        # Update the ship's x value and not the rect.
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.centerx += self.settings.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.centerx -= self.settings.ship_speed

    def blitme(self):
        """Draw the ship at the current location."""
        self.screen.blit(self.image, self.rect)