weixin_48907160 2022-10-15 20:40 采纳率: 100%
浏览 76
已结题

用pygame做飞机大战为什么子弹不出来?

用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)


  • 写回答

4条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-10-15 21:09
    关注

    在game_function.py这个主要函数里,在重绘子弹之后,屏幕又被screen.fill给覆盖了,所以看不到子弹。
    重绘子弹的命令应该放在screen.fill后面

    def update_screen(ai_settings, screen, ship, bullets):
        #重绘子弹
        # for bullet in bullets.sprites():
        #     bullet.draw_bullet()
     
        """更新屏幕图像"""
        screen.fill(ai_settings.bg_color)
        for bullet in bullets.sprites():
             bullet.draw_bullet()
             
        ship.blitme()
    
        # 让最近绘制的屏幕可见
        pygame.display.flip()
    

    另外你的子弹位置固定在600,400,所以虽然飞机移动了,子弹也只会从屏幕中间发射。需要改成飞机所在的位置,才会随着飞机移动而改变子弹发射位置:

            # self.rect.x = 600
            # self.rect.y = 400
            self.rect.centerx = ship.rect.centerx
            self.rect.top = ship.rect.top
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 10月15日

悬赏问题

  • ¥15 数学建模——参会安排怎么做
  • ¥15 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据