Python Forum
Getting a ship to move in pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a ship to move in pygame
#1
I'm trying to make a basic alien invasion game but at the moment my ship won't move left to right. I'm not sure why as it was moving before. Can anyone tell me what I'm doing wrong? Here is my code for the ship:

import pygame 

from settings import Settings

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

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

        # Load the ship image and get its rect.
        self.image = pygame.image.load(r'C:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\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

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

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

    def update(self):
        """Update ships position based on movement flags."""
        # Update the ships x value not the rect.
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.rect.x += self.settings.ship_speed 
        if self.moving_left and self.rect.left > 0:
            self.rect.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)
Here is my main code:

import sys 

import pygame  # contains functionality to make a game

from settings import Settings
from ship import Ship
#from alien import Alien

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

    def __init__(self):
        """Initialise the game and create game resources."""
        pygame.init() # initialise the background settings 
        # which the game needs to work properly
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height)) 
        # creates display window with these dimensions
        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self)
        #self.alien = Alien(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):
        # Watch for keyboard 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:
                        # Move ship 1 to the 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 screenand flip to the new screen."""
        # redraw the screen during each pass through the loop 
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()
        #self.alien.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()
and here are the settings I'm using:

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

    def __init__(self):
        """Initialise the games settings."""
        self.screen_width = 1000
        self.screen_height = 600
        self.bg_color = (230, 230, 230)

        # Ship settings
        self.ship_speed = 1.5
Reply
#2
   # Update rect object from self.x
        self.rect.x = self.x
here. You change the rect.x with the move, but then set it back to self.x which you never change. I think if you take that line out you'll be golden.
Reply
#3
Thank you that solved it. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] my pygame car wont move Erio85 1 1,048 Apr-24-2023, 04:52 PM
Last Post: Erio85
  [PyGame] pygame-manu : using controller buttons to move around menu mlw19mlw91 2 1,543 Mar-12-2023, 01:55 PM
Last Post: deanhystad
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,280 Nov-10-2021, 03:38 PM
Last Post: metulburr
  Window Color/Adding a ship kleeklee 2 4,561 May-29-2020, 01:30 AM
Last Post: kleeklee
  Python BattleShips Random Ship placement FnaticPutin 2 7,692 Oct-13-2017, 11:33 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020