船赢了';不要停止在python速成班中向左移动外星人入侵

船赢了';不要停止在python速成班中向左移动外星人入侵,python,pygame,Python,Pygame,我正在python速成班上练习python,我正在做第一个项目,那就是外星人入侵。 我仍然在它的开始部分,你开始把船从左到右移动,按向下键和向上键,按向右键,它工作得很好。对于左键,它只会一直向左移动,不会停止移动。我写的正是书中所说的内容,我在这里和谷歌上查看了一下,看看是否有其他人也犯了这个错误,但似乎找不到任何可以帮助我的东西 外星人入侵 import pygame from settings import Settings from ship import Ship import gam

我正在python速成班上练习python,我正在做第一个项目,那就是外星人入侵。 我仍然在它的开始部分,你开始把船从左到右移动,按向下键和向上键,按向右键,它工作得很好。对于左键,它只会一直向左移动,不会停止移动。我写的正是书中所说的内容,我在这里和谷歌上查看了一下,看看是否有其他人也犯了这个错误,但似乎找不到任何可以帮助我的东西

外星人入侵

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 导入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)
    

中检查事件

ship.moving_left = False

ship.moving_left==False
将检查是真是假。

只有拼写错误的问题不应回答,但应投票决定是否结束。关于一个简单的打字错误引起的问题是离题的。原因是这些问题对其他读者没有用处。看见但我相信你知道这一点。你为什么要回答这个问题?哈哈,谢谢!我甚至没有意识到我放了两个等号。我看着书,看着代码,我可以发誓我一切都对。有时候你只需要第二双眼睛就能看到你的代码lol。@rabbi76我不知道。