首页 > 解决方案 > 嘿,我不能在我的 pygame 程序中使用 midbottom 函数。每次我运行我的程序时,我都会在左下角看到我的 rect 对象

问题描述

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 是主类的实例的代码。每次我运行我的代码时,我都会在屏幕的左下角看到我的 rect 对象,而 midbottom 函数应该将我的 rect 对象(即此处的船)放在屏幕的中下部分

标签: python-3.xpygamepython-3.8game-developmentrect

解决方案


设置是不够的self.rect.midbottom = self.screen_rect.midbottom。您还需要self.x设置self.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改用。

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)

推荐阅读