当我按下空格键时,alien_invasion程序中没有发射多发子弹

时间:2019-05-08 15:46:18

标签: python pygame

当我尝试运行我的Alien_invasion python程序时,子弹没有从我的飞船中发射出来

我创建了6个文件,但由于它们与我的问题有关,因此我仅列出其中的四个文件。

来自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)

来自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"""
#Redraw 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()

来自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():
#Initialize game and create  a screen object
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()

来自settings.py:

class Settings():
"""A class to store all settings for Alien Invasion"""
    def __init__(self):
        #Initialize the games Settings
        #screen Settings
        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=1
        self.bullet_width=3
        self.bullet_height=15
        self.bullet_color=0,0,0

0 个答案:

没有答案