用pygame做飞机大战为什么子弹不出来?
子弹模块
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""子弹管理类"""
def __init__(self, ai_settings, screen, ship):
"""在飞船所处的位置发射子弹"""
super(Bullet, self).__init__()
self.screen = screen
#在(0,0)处创建一个子弹,再设置位置
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
self.rect.x = 600
self.rect.y = 400
#存储子弹小数位置
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed = ai_settings.bullet_speed
def update(self):
"""移动子弹"""
self.y -= self.speed
self.rect.y = self.y
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.screen, self.color, self.rect)
主要函数模块
import sys
import pygame
from bullet import Bullet
def check_events(ai_settings, screen, ship, bullets):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown(event, ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
check_up(event, ship)
def check_keydown(event, ai_settings, screen, ship, bullets):
"""响应按键"""
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:
#创建一颗子弹
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
def check_up(event, ship):
"""响应松开"""
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def update_screen(ai_settings, screen, ship, bullets):
#重绘子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
"""更新屏幕图像"""
screen.fill(ai_settings.bg_color)
ship.blitme()
# 让最近绘制的屏幕可见
pygame.display.flip()
飞船模块
import pygame
class Ship():
def __init__(self, ai_settings, screen):
"""初始化飞船"""
self.screen = screen
self.ai_settings = ai_settings
#加载飞船图像
self.image = pygame.image.load('images/ship.jpeg')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#将每艘飞船放在屏幕底部
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
#在飞船的属性中存储小数值
self.center = float(self.rect.centerx)
#移动标志
self.moving_right = False
self.moving_left = False
def update(self):
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed
if self.moving_left and self.rect.left > self.screen_rect.left:
self.center -= self.ai_settings.ship_speed
self.rect.centerx = self.center
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
信息存储模块
class Settings():
"""保存设置类"""
def __init__(self):
"""初始化游戏设置"""
#屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
#飞船设置
self.ship_speed = 1.5
#子弹设置
self.bullet_speed = 1
self.bullet_width = 10
self.bullet_height = 150
self.bullet_color = (60, 60, 60)
游戏运行主模块
import sys
import pygame
from bullet import Bullet
from settings import Settings
from ship import Ship
import game_function as gf
from pygame.sprite import Group
def run_game():
# 初始化游戏并创建一个屏幕对象
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Aircraft War Game")
# 创建一艘飞船
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)