Skip to content
Advertisement

Python: game image changes to an undesired position after adjusting image’s moving speed

I’m making a simple python game and there are 3 py files: alien_invasion, settings, ship. I would like the image position to be at the middle bottom of screen every time the game starts. It works when code are like the following:

alien_invasion.py:
import sys
import pygame

from settings import Settings
from ship import Ship

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

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.RESIZABLE)
    pygame.display.set_caption("Alien Invasion")

    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:
            if event.key == pygame.K_RIGHT:
                self.ship.moving_right = True
            elif event.key == pygame.K_LEFT:
                self.ship.moving_left = True

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                self.ship.moving_right = False
            elif event.key == pygame.K_LEFT:
                self.ship.moving_left = False

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

    pygame.display.flip()      

if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()  

ship.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()

        # Load ship image and get its rect.
        self.image = pygame.image.load('images/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

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

    def update(self):
        """"Update the ship's position based on the movement flags."""
        if self.moving_right:
            self.rect.x += 1
        if self.moving_left:
            self.rect.x -= 1

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

settings.py:

class Settings:
    """A class to store all settings for Alien Invasion."""

    def __init__(self):
        """Initialize the game's settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)

Then I made changes to settings.py and ship.py to adjust the moving speed:

ship.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.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()

        # Load 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 new ship at the bottom center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

        # Movement flags
        self.moving_right = False
        self.moving_left = 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:
            self.x += self.settings.ship_speed
        if self.moving_left:
            self.x -= self.settings.ship_speed

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

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

settings.py:

class Settings:
    """A class to store all settings for Alien Invasion."""

    def __init__(self):
        """Initialize the game's settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)
        # Ship settings
        self.ship_speed = 1.5 

Then when I run the alien_invasion code, the image starts showing up at the left bottom of screen when game starts, which is not what I want.

My desired position: enter image description here

My undesired position: enter image description here

Advertisement

Answer

You need to set self.x after setting the location of self.rect:

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

    def __init__(self, ai_game):
        # [...]

        self.rect = self.image.get_rect()

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

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

        self.x = float(self.rect.x)                               # <--- insert

        # [...]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement