Python速成课程游戏:项目符号不触发

时间:2019-05-10 21:26:32

标签: python

我的外星人入侵游戏没有显示被发射的子弹。我正在从Python Crash课程书中复制大部分代码(在这里和那里进行了微小的更改),所以我猜测这是由于找不到语法错误引起的。

我查看了本书中的代码,并试图找出它们之间的差异。我将K_SPACE更改为K_w,以查看空格键是否未注册或类似内容,但子弹仍然不会发射。

alien_invasion.py

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

bullet.py

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,但可以根据需要提供它们

预期结果:子弹走去

实际结果:游戏不会崩溃,但是在按下空格键时什么也不会发生。

1 个答案:

答案 0 :(得分:1)

问题似乎出在update_screen函数上-您首先绘制 一切,更新屏幕,然后在更新屏幕后绘制项目符号。只需将对...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()