我正在开发 Python Crash Course.pdf 中的 Alien Invasion 游戏
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:
check_keydown_events(event, ship)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
def check_keydown_events(event, ship):
"""respond to keypresses."""
if event.key == pygame.K_d:
ship.moving_right = True
if event.key == pygame.K_a:
ship.moving_left = True
def check_keyup_events(event, ship):
"""respond to key releases."""
if event.key == pygame.K_d:
ship.moving_right = False
if event.key == pygame.K_a:
ship.moving_left = False
# Move the ship to the right.
#ship.rect.centerx += 1
def update_screen(ai_settings, screen, ship):
"""update images on 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()
#alien invasion
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
#intialize 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)
#set the background color
#bg_color = (52, 86, 139)
# start the main loop for the game
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
#watch for keyboard and mouse events.
#redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_color)
ship.blitme()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#make the most recently drawn screen visible
pygame.display.flip()
run_game()
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 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
#store a decimal value for the ships center
self.center = float(self.rect.centerx)
#Movement flags
self.moving_right = False
self.moving_left = False
def update(self):
"""update the ships position based on the movement flag"""
# Update the ships center value, not the rect.
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_settings.ship_speed_factor
# Update rect object from self.center
self.rect.centerx = self.center
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."""
# pygame.draw.rect(self, (255,0,0), (100, 100), 36)
self.screen.blit(self.image, self.rect)
class Settings():
"""A class to store all settings for Alien Invasion"""
def __init__(self):
"""intialize the games settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (52, 86, 139)
#ship settings
self.ship_speed_factor = 1.5
这是我的 game_functions 类的代码
当我按“D”时,85% 的情况下什么也没有发生。有时它不停地向左或向右滑动,或者。按“A”时也一样。
为什么会这样?我在这段代码中做错了什么?这段代码相当基本,我只是想让它向左或向右移动。
答案 0 :(得分:0)
他们没有代码可以将 self.moving_right
和 self.moving_left
变成
false
阻止他们移动。
我会使用这样的东西:
if event.type == pygame.KEYUP :
if event.key == pygame.K_RIGHT:
self.moving_right = False
elif event.key == pygame.K_ESCAPE :
self.moving_left = False