Python速成班游戏:子弹不开火

Python速成班游戏:子弹不开火,python,Python,我的外星人入侵游戏没有显示子弹被发射。我正在复制Python速成教程中的大部分代码(这里和那里都有一些小的改动),所以我猜这是由于我找不到的语法错误造成的 我已经看过书中的代码,并试图找出它们之间的差异。我把K_SPACE改为K_w,看看空格键是否没有注册或者类似的东西,但是子弹还是发不出来 外星人入侵 import pygame from pygame.sprite import Group from settings import Settings from ship import Ship

我的外星人入侵游戏没有显示子弹被发射。我正在复制Python速成教程中的大部分代码(这里和那里都有一些小的改动),所以我猜这是由于我找不到的语法错误造成的

我已经看过书中的代码,并试图找出它们之间的差异。我把K_SPACE改为K_w,看看空格键是否没有注册或者类似的东西,但是子弹还是发不出来

外星人入侵

import pygame
from pygame.sprite import Group
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')

    # set the background color
    bg_color= ai_settings.bg_color

    # make a ship
    ship = Ship(ai_settings, screen)
    # make a group to store bullets in 
    bullets = Group()

    # start the main loop for the game
    running = True
    while running:

        running = gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        bullets.update()
        gf.update_screen(ai_settings, screen, ship, bullets)

    pygame.quit()

run_game()
game_functions.py

import pygame
from bullet import Bullet

def check_events(ai_settings, screen, ship, bullets):
    """respond to keypresses and mouse events"""
    running = True
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False # different from Python Crash Course

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)

    return running

def update_screen(ai_settings, screen, ship, bullets):
    """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()

    # redraw all bullets behind ship and aliens
    for bullet in bullets.sprites():
        bullet.draw_bullet()

def check_keydown_events(event, ai_settings, screen, ship, bullets):
    """respond to keypresses"""
    if event.key == pygame.K_d:
        ship.moving_right = True
    elif event.key == pygame.K_a:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        # create a new bullet and add it to the bullets groups
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

def check_keyup_events(event, ship):
    """respond to key releases"""
    if event.key == pygame.K_d:
        ship.moving_right = False
    elif event.key == pygame.K_a:
        ship.moving_left = False
子弹头

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    """a class to manage bullets fired from the ship"""

    def __init__(self, ai_settings, screen, ship):
        """create a bullet object at the ship's current position"""
        super().__init__()
        self.screen = screen

        # create a bullet rect at (0, 0) and then set correct position
        self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top

        # store the bullet's position as a decimal value
        self.y = float(self.rect.y)

        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor

    def update(self):
        """move the bullet up the screen"""
        # update the decimal position of the bullet
        self.y -= self.speed_factor
        # update the rect position
        self.rect.y = self.y

    def draw_bullet(self):
        """draw the bullet to the screen"""
        pygame.draw.rect(self.screen, self.color, self.rect)
省略了settings.py和ship.py,但可以在必要时提供它们

预期结果:子弹头皮尤皮尤


实际结果:游戏不会崩溃,但当按下空格键时不会发生任何事情。

问题似乎出现在
更新屏幕的功能上-您首先绘制
一切,更新屏幕,然后在屏幕更新后绘制项目符号。只需将调用移动到函数末尾的
…display.flip()
,可能会显示您的项目符号(除非还有其他错误)


这是有效的,解释是完全有道理的。非常感谢。
def update_screen(ai_settings, screen, ship, bullets):
    """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()

    # take this out of here

    # pygame.display.flip()

    # redraw all bullets behind ship and aliens
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    # to here
    pygame.display.flip()