当我按下笔记本电脑键盘上的空格键时,我的外星人入侵程序没有显示出从火箭图像顶部发射的子弹。
我遵循了Python Crash课程书中的代码示例,并准确地遵循了每个步骤,但是我的代码仍然无法正常工作。
我已经为代码编写了五个python文件。我编译和运行程序的主要代码是Alien_invasion.py文件。我将在这里列出它们。这里也是我代码的github链接,其中包含我程序的火箭图像 alien_invasion.py
来自Alien_invasion.py:
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
def run_game():
pygame.init()
ai_settings=Settings()
#Make a group that stores bullet
screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
#set the background color.
bg_color=(0,191,255)
#Start the main cloop for the game
#Make a Ship
ship=Ship(ai_settings,screen)
bullets=Group()
while True:
gf.check_events(ai_settings,screen,ship,bullets)
ship.update()
bullets.update()
gf.update_screen(ai_settings,screen,ship,bullets)
#Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
#mAKE THE MOST RECENTLY DRAWN SCREEN VISIBLE
#redraw the screen during each pass through the VISIBLE
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()
run_game()
game_function.py
import sys
import pygame
from bullet import Bullet
def check_keydown_events(event,ai_settings,screen,ship,bullets):
"""Respond to keypresses"""
if event.key==pygame.K_RIGHT:
ship.moving_right=True
elif event.key==pygame.K_LEFT:
ship.moving_left=True
elif event.key==pygame.K_SPACE:
#Create a new bullet and add it to the bullets group
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_RIGHT:
ship.moving_right=False
elif event.key==pygame.K_LEFT:
ship.moving_left=False
def check_events(ai_settings,screen,ship,bullets):
"""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,ai_settings,screen,ship,bullets)
elif event.type==pygame.KEYUP:
check_keyup_events(event,ship)
def update_screen(ai_settings,screen,ship,bullets):
"""Update images on the screen and flip to the new screen"""
#Redrwaa all bullets behind ship and aliens
for bullet in bullets.sprites():
bullet.draw_bullet()
#Redraw he 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()
settings.py
class Settings():
def __init__(self):
self.screen_width=1200
self.screen_height=800
self.bg_color=(255,255,255)
#Ship Settings
self.ship_speed_factor=1.5
#Bullet settings
self.bullet_speed_factor=10
self.bullet_width=25
self.bullet_height=75
self.bullet_color=0,0,0
bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""A class to manage bullets fired from a ship """
def __init__(self,ai_settings,screen,ship):
"""Create a bullet object at the ships current position"""
super(Bullet,self).__init__()
self.screen=screen
#Create a bullet rect at (0,0) and then set the correct posiiton
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 bullets 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 pthe screen"""
#Update the decimal position of the bullets
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)
ship.py
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 it RECENTLY
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 of the center of the screen_rect
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
#movement flag
self.moving_right=False
self.moving_left=False
#Store a decimal value for the ships centerx
self.center=float(self.rect.centerx)
def blitme(self):
""" Draw the ship at its current location"""
self.screen.blit(self.image,self.rect)
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
elif self.moving_left and self.rect.left>0:
self.center-=self.ai_settings.ship_speed_factor
#Update rect object from self.centerx
self.rect.centerx=self.center