Search code examples
pythonpygamepycharmkeypress

I am having trouble getting this ship in pygame to move right and left with KEYUP and KEYDOWN presses


At the moment, I am learning from the Python Crash Course Intro Book By Eric Matthes. I am on the section about making a game with Pygame. The second they added keyup and keydown my code stopped letting me move the ship right and left. Beforehand I was able to move the ship without the additional functions.


I have tried looking on the web. I have tried messing around with the code to fit the situation. I, however, have not tried another IDE. I apologize in advance if this is not properly formatted, I am a noob to this website. Thanks!

===================

alien_invaion.py

import pygame

from settings import Settings
from ship import Ship
import game_functions as gf


def run_game():
# Initialize pygame, settings, and screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings. screen_height))
    pygame.display.set_caption("Alien Invasion")

    # Make a ship.
    ship = Ship(ai_settings, screen)

    # Start the main loop of the game.
    while True:
        gf.check_events(ship)
        ship.update
        gf.update_screen(ai_settings, screen, ship)


run_game()

ship.py

import pygame


class Ship():

    def __init__(self, ai_settings, screen):
        """Initialize the ship and set its starting position."""
        self.screen = screen
        self.ai_settings = ai_settings

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

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

        # Store a decimal value for the ship's center.


        # Movement flags
        self.moving_right = False

    def update(self):
        """Update the ship's position based on the movement flag."""
        # Update the ship's center value, not rect.
        if self.moving_right:
            self.rect.centerx += 1
        # Update rect object from self.center.

    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 = (0, 23, 233)

     # Ship settings

game_functions.py

import sys

import pygame



def check_events(ship):
    """respond to keypresss's 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:
                ship.moving_right = True

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


def update_screen(ai_settings, screen, ship):
    """Update images on the screen and flip to the new screen."""
    # Redraw the screen during each pass through the loop.
    screen.fill(ai_settings.bg_color)
    ship.blitme()
    


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

No error messages... I expect the ship to be able to move when I hold the left and right keys down and the ship to stop moving when I release said keys.


Solution

  • You missed the parentheses when the update method is called:

    ship.update

    ship.update()