-1

I exactly copy the code from PythonCrashCourse project 1 but the effect is a little bit different comparing to statement in the book. When I press the right arrow, this ship is supposed to move continuously till I release the key. However, what I get is that the ship will not move continuously but it stops right after move 1 step. welcome your inputs!

Below is the code: 1. alien_invasion.py

import sys
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)

    #set the background color.
    bg_color=(230,230,230)
    #start the mail loop for the game
    while True:
        # watch for keyboard and mouse events.
        gf.check_events(ship)
        ship.update()
        gf.update_screen(ai_settings,screen,ship)

run_game()

2. 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.type==pygame.KEYUP:
            if event.key==pygame.K_RIGHT:
                ship.moving_right=False
        
def update_screen(ai_settings,screen,ship):
   """update images on the scren and flip to the new screen."""
   #redraw the screen during each pass through t   screen.fill(ai_settings.bg_color)
   ship.blitme()
   
   #Make the most recently drawn screen visible.
   pygame.display.flip()

3. 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('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

        #Movement flag
        self.moving_right=False

    def update(self):
        """Update the ship's position based on the movement flag."""
        if self.moving_right:
            self.rect.centerx+=1

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

4. settings.py

class Settings():
   """A class to store all settings for Alient 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)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • There is nothing wrong with your code. The problem is not reproducible. That seems to be due to your system. – Rabbid76 Dec 21 '21 at 19:32
  • 1
    thx for responding. somehow, I add another section code to enable left movement and the problem is gone...anyway, thx for your help! – Lin Victor Dec 21 '21 at 19:50

0 Answers0