Ship won't stop moving left in python crash course alien invasion

Question:

I’m practicing python in python crash course and I am doing the first project which is alien invasion.
I’m still in the beginning of it in the part where you start to move the ship left to right and on keydown and keyup for the right key it works fine. For the left key it just keeps moving left and will not stop moving. I have written exactly what the books as said to write and i have looked on here and on google to see if anyone one else has had this error as well but can’t seem to find anything that can help me.

alien_invasion.py

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

def run_game():
  # Initialize game and create a 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(screen)
 
 # Start the main loop for the game.
 while True:
    gf.check_events(ship)
    ship.update()
    gf.update_screen(ai_settings, screen, ship)
   
   
run_game()

game_functions.py

 import sys
 import pygame


 def check_events(ship):
    """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:
             ship.moving_right = True
           elif event.key == pygame.K_LEFT:
              ship.moving_left = True
       elif event.type == pygame.KEYUP:
           if event.key == pygame.K_RIGHT:
             ship.moving_right = False
           elif event.key == pygame.K_LEFT:
             ship.moving_left == 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()

ship.py
import pygame

class Ship():
  def __init__(self, screen):
     """Initialize the ship and set its starting position."""
     self.screen = screen
    
     # Load the ship image and get its rect.
     self.image = pygame.image.load('img/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
    
     # Movement flags
     self.moving_right = False
     self.moving_left = False
    
  def update(self):
     """Update the ships's position based on the movement flag"""
     if self.moving_right:
         self.rect.centerx += 1
     if self.moving_left:
        self.rect.centerx -= 1
    
 def blitme(self):
     """Draw the ship at its current location"""
     self.screen.blit(self.image, self.rect)
    
Asked By: Johnathan Vargas

||

Answers:

in check_events

ship.moving_left = False

ship.moving_left == False would be a check for true or false.

Answered By: Finn

Just do it that way:

elif event.type == pygame.KEYUP:

         self.ship.moving_right = False

         self.ship.moving_left = False

Unfortunately, it’s not the first error in the book 🙁

Answered By: Pavel Inoz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.