还是飞机大战的问题

来源:4-2 项目作业

为学习而奋斗

2020-01-21 20:14:10

main.py:

import pygame
import sys

import constants
from game.plane import OurPlane


def main():
   """ 游戏入口,main方法 """
   # 初始化
   pygame.init()

   width, height = 480, 852

   # 屏幕对象
   screen = pygame.display.set_mode((width, height))
   # 设置窗口标题
   pygame.display.set_caption('飞机大战')

   # 加载背景图片
   bg = pygame.image.load(constants.BG_IMG)
   # 游戏的标题
   img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
   img_game_title_rect = img_game_title.get_rect()
   # 宽度和高度
   t_width, t_height = img_game_title.get_size()
   img_game_title_rect.topleft = (int((width - t_width) / 2),
                                  int(height / 2 - t_height))


   # 开始按钮
   btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
   btn_start_rect = btn_start.get_rect()
   btn_width, btn_height = btn_start.get_size()
   btn_start_rect.topleft = (int((width - btn_width) / 2),
                             int(height / 2 + btn_height))


   #加载背景音乐
   pygame.mixer.music.load(constants.BG_MUSIC)
   pygame.mixer.music.play(-1)         # 无限循环播放
   pygame.mixer.music.set_volume(0.5)  # 设置音量

   # 游戏状态
   status = 0  # 0准备中,1 游戏中,2 游戏结束

   our_plane = OurPlane(screen)

   frame = 0   # 播放帧数

   clock = pygame.time.Clock()

   while True:
       # 设置帧速率
       clock.tick(60)
       frame += 1
       if frame >= 60:
           frame = 0
       # 1. 监听事件
       for event in pygame.event.get():
           # 退出游戏
           if event.type == pygame.QUIT:
               pygame.quit()
               sys.exit()
           elif event.type == pygame.MOUSEBUTTONDOWN:
               # 鼠标点击进入游戏
               # 游戏正在准备中,点击才能进入游戏
               if status == 0:
                   status = 1
           elif event.type == pygame.KEYDOWN:
               # 键盘事件
               # 游戏正在游戏中,才需要控制键盘 ASWD
               if status == 1:
                   if event.key == pygame.K_w or event.key == pygame.K_UP:
                       our_plane.move_up()
                   elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                       our_plane.move_down()
                   elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                       our_plane.move_left()
                   elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                       our_plane.move_right()
                   elif event.key == pygame.K_SPACE:
                       # 发射子弹
                       our_plane.shoot()

       # 更新游戏的状态
       if status == 0:
           # 游戏正在准备中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 标题
           screen.blit(img_game_title, img_game_title_rect)
           # 开始按钮
           screen.blit(btn_start, btn_start_rect)
       elif status == 1:
           # 游戏进行中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 绘制飞机
           our_plane.update(frame)
           # 绘制子弹
           our_plane.bullets.update()

       pygame.display.flip()


if __name__ == '__main__':
   main()
import pygame
import sys

import constants
from game.plane import OurPlane


def main():
   """ 游戏入口,main方法 """
   # 初始化
   pygame.init()

   width, height = 480, 852

   # 屏幕对象
   screen = pygame.display.set_mode((width, height))
   # 设置窗口标题
   pygame.display.set_caption('飞机大战')

   # 加载背景图片
   bg = pygame.image.load(constants.BG_IMG)
   # 游戏的标题
   img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
   img_game_title_rect = img_game_title.get_rect()
   # 宽度和高度
   t_width, t_height = img_game_title.get_size()
   img_game_title_rect.topleft = (int((width - t_width) / 2),
                                  int(height / 2 - t_height))


   # 开始按钮
   btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
   btn_start_rect = btn_start.get_rect()
   btn_width, btn_height = btn_start.get_size()
   btn_start_rect.topleft = (int((width - btn_width) / 2),
                             int(height / 2 + btn_height))


   #加载背景音乐
   pygame.mixer.music.load(constants.BG_MUSIC)
   pygame.mixer.music.play(-1)         # 无限循环播放
   pygame.mixer.music.set_volume(0.5)  # 设置音量

   # 游戏状态
   status = 0  # 0准备中,1 游戏中,2 游戏结束

   our_plane = OurPlane(screen)

   frame = 0   # 播放帧数

   clock = pygame.time.Clock()

   while True:
       # 设置帧速率
       clock.tick(60)
       frame += 1
       if frame >= 60:
           frame = 0
       # 1. 监听事件
       for event in pygame.event.get():
           # 退出游戏
           if event.type == pygame.QUIT:
               pygame.quit()
               sys.exit()
           elif event.type == pygame.MOUSEBUTTONDOWN:
               # 鼠标点击进入游戏
               # 游戏正在准备中,点击才能进入游戏
               if status == 0:
                   status = 1
           elif event.type == pygame.KEYDOWN:
               # 键盘事件
               # 游戏正在游戏中,才需要控制键盘 ASWD
               if status == 1:
                   if event.key == pygame.K_w or event.key == pygame.K_UP:
                       our_plane.move_up()
                   elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                       our_plane.move_down()
                   elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                       our_plane.move_left()
                   elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                       our_plane.move_right()
                   elif event.key == pygame.K_SPACE:
                       # 发射子弹
                       our_plane.shoot()

       # 更新游戏的状态
       if status == 0:
           # 游戏正在准备中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 标题
           screen.blit(img_game_title, img_game_title_rect)
           # 开始按钮
           screen.blit(btn_start, btn_start_rect)
       elif status == 1:
           # 游戏进行中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 绘制飞机
           our_plane.update(frame)
           # 绘制子弹
           our_plane.bullets.update()

       pygame.display.flip()


if __name__ == '__main__':
   main()
import pygame
import sys

import constants
from game.plane import OurPlane


def main():
   """ 游戏入口,main方法 """
   # 初始化
   pygame.init()

   width, height = 480, 852

   # 屏幕对象
   screen = pygame.display.set_mode((width, height))
   # 设置窗口标题
   pygame.display.set_caption('飞机大战')

   # 加载背景图片
   bg = pygame.image.load(constants.BG_IMG)
   # 游戏的标题
   img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
   img_game_title_rect = img_game_title.get_rect()
   # 宽度和高度
   t_width, t_height = img_game_title.get_size()
   img_game_title_rect.topleft = (int((width - t_width) / 2),
                                  int(height / 2 - t_height))


   # 开始按钮
   btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
   btn_start_rect = btn_start.get_rect()
   btn_width, btn_height = btn_start.get_size()
   btn_start_rect.topleft = (int((width - btn_width) / 2),
                             int(height / 2 + btn_height))


   #加载背景音乐
   pygame.mixer.music.load(constants.BG_MUSIC)
   pygame.mixer.music.play(-1)         # 无限循环播放
   pygame.mixer.music.set_volume(0.5)  # 设置音量

   # 游戏状态
   status = 0  # 0准备中,1 游戏中,2 游戏结束

   our_plane = OurPlane(screen)

   frame = 0   # 播放帧数

   clock = pygame.time.Clock()

   while True:
       # 设置帧速率
       clock.tick(60)
       frame += 1
       if frame >= 60:
           frame = 0
       # 1. 监听事件
       for event in pygame.event.get():
           # 退出游戏
           if event.type == pygame.QUIT:
               pygame.quit()
               sys.exit()
           elif event.type == pygame.MOUSEBUTTONDOWN:
               # 鼠标点击进入游戏
               # 游戏正在准备中,点击才能进入游戏
               if status == 0:
                   status = 1
           elif event.type == pygame.KEYDOWN:
               # 键盘事件
               # 游戏正在游戏中,才需要控制键盘 ASWD
               if status == 1:
                   if event.key == pygame.K_w or event.key == pygame.K_UP:
                       our_plane.move_up()
                   elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                       our_plane.move_down()
                   elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                       our_plane.move_left()
                   elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                       our_plane.move_right()
                   elif event.key == pygame.K_SPACE:
                       # 发射子弹
                       our_plane.shoot()

       # 更新游戏的状态
       if status == 0:
           # 游戏正在准备中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 标题
           screen.blit(img_game_title, img_game_title_rect)
           # 开始按钮
           screen.blit(btn_start, btn_start_rect)
       elif status == 1:
           # 游戏进行中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 绘制飞机
           our_plane.update(frame)
           # 绘制子弹
           our_plane.bullets.update()

       pygame.display.flip()


if __name__ == '__main__':
   main()
import pygame
import sys

import constants
from game.plane import OurPlane


def main():
   """ 游戏入口,main方法 """
   # 初始化
   pygame.init()

   width, height = 480, 852

   # 屏幕对象
   screen = pygame.display.set_mode((width, height))
   # 设置窗口标题
   pygame.display.set_caption('飞机大战')

   # 加载背景图片
   bg = pygame.image.load(constants.BG_IMG)
   # 游戏的标题
   img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
   img_game_title_rect = img_game_title.get_rect()
   # 宽度和高度
   t_width, t_height = img_game_title.get_size()
   img_game_title_rect.topleft = (int((width - t_width) / 2),
                                  int(height / 2 - t_height))


   # 开始按钮
   btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
   btn_start_rect = btn_start.get_rect()
   btn_width, btn_height = btn_start.get_size()
   btn_start_rect.topleft = (int((width - btn_width) / 2),
                             int(height / 2 + btn_height))


   #加载背景音乐
   pygame.mixer.music.load(constants.BG_MUSIC)
   pygame.mixer.music.play(-1)         # 无限循环播放
   pygame.mixer.music.set_volume(0.5)  # 设置音量

   # 游戏状态
   status = 0  # 0准备中,1 游戏中,2 游戏结束

   our_plane = OurPlane(screen)

   frame = 0   # 播放帧数

   clock = pygame.time.Clock()

   while True:
       # 设置帧速率
       clock.tick(60)
       frame += 1
       if frame >= 60:
           frame = 0
       # 1. 监听事件
       for event in pygame.event.get():
           # 退出游戏
           if event.type == pygame.QUIT:
               pygame.quit()
               sys.exit()
           elif event.type == pygame.MOUSEBUTTONDOWN:
               # 鼠标点击进入游戏
               # 游戏正在准备中,点击才能进入游戏
               if status == 0:
                   status = 1
           elif event.type == pygame.KEYDOWN:
               # 键盘事件
               # 游戏正在游戏中,才需要控制键盘 ASWD
               if status == 1:
                   if event.key == pygame.K_w or event.key == pygame.K_UP:
                       our_plane.move_up()
                   elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                       our_plane.move_down()
                   elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                       our_plane.move_left()
                   elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                       our_plane.move_right()
                   elif event.key == pygame.K_SPACE:
                       # 发射子弹
                       our_plane.shoot()

       # 更新游戏的状态
       if status == 0:
           # 游戏正在准备中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 标题
           screen.blit(img_game_title, img_game_title_rect)
           # 开始按钮
           screen.blit(btn_start, btn_start_rect)
       elif status == 1:
           # 游戏进行中
           # 绘制背景
           screen.blit(bg, bg.get_rect())
           # 绘制飞机
           our_plane.update(frame)
           # 绘制子弹
           our_plane.bullets.update()

       pygame.display.flip()


if __name__ == '__main__':
   main()


constants.py:

import os

#项目文件路径
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

#静态资源文件夹路径
ASSETS_DIR = os.path.join(BASE_DIR,'assets')

#游戏背景图片路径
BG_IMG = os.path.join(ASSETS_DIR,'images\\background.png')

#飞机大战标题图片路径
IMG_GAME_TITLE = os.path.join(ASSETS_DIR,'images\\game_title.png')

#游戏开始图片路径(开始按钮)
IMG_GAME_START_BTN = os.path.join(ASSETS_DIR,'images\\game_start.png')

#获取游戏运行时背景音乐路径
BG_MUSIC = os.path.join(ASSETS_DIR,'sounds\\game_bg_music.mp3')

#发射子弹时背景音乐路径
BULLET_SHOOT_SOUND = os.path.join(ASSETS_DIR,'sounds\\bullet.wav')

#获取我方飞机图片资源路径

#飞机一图
OUR_PLANE_IMG_1 = [os.path.join(ASSETS_DIR,'images\\hero1.png')]

#飞机二图
OUR_PLANE_IMG_2 = [os.path.join(ASSETS_DIR,'images\\hero2.png')]

#飞机列表静态图片
OUR_DESTROY_IMG_LIST = [
   os.path.join(ASSETS_DIR,'images/hero_broken_n1.png'),
   os.path.join(ASSETS_DIR, 'images/hero_broken_n2.png'),
   os.path.join(ASSETS_DIR, 'images/hero_broken_n3.png'),
   os.path.join(ASSETS_DIR, 'images/hero_broken_n4.png')
]

#子弹图片资源路径
BULLET_IMG = os.path.join(ASSETS_DIR,'images\\bullet2.png')



plane.py:

"""
                          飞机的基类
    我方的飞机     敌方的小型飞机   敌方的中型飞机  敌方的大型飞机
"""
import pygame

import constants
from game.bullet import Bullet


class Plane(pygame.sprite.Sprite):
   """"
   飞机的基础类
   """
   # 飞机的图片
   plane_images = []
   # 飞机爆炸的图片
   destroy_images = []
   # 坠毁的音乐地址
   down_sound_src = None
   # 飞机的状态: True,活的,False,死的
   active = True
   # 该飞机发射的子弹精灵组
   bullets = pygame.sprite.Group()

   def __init__(self, screen, speed=None):
       super().__init__()
       self.screen = screen
       # 加载静态资源
       self.img_list = []
       self._destroy_img_list = []
       self.down_sound = None
       self.load_src()

       # 飞行的速度
       self.speed = speed or 10
       # 获取飞机的位置
       self.rect = self.img_list[0].get_rect()

       # 飞机的宽度和高度
       self.plane_w, self.plane_h = self.img_list[0].get_size()

       # 游戏窗口的宽度和高度
       self.width, self.height = self.screen.get_size()

       # 改变飞机的初始化位置, 放在屏幕的下方
       self.rect.left = int((self.width - self.plane_w) / 2)
       self.rect.top = int(self.height / 2)

   def load_src(self):
       """ 加载静态资源 """
       # 飞机图像
       for img in self.plane_images:
           self.img_list.append(pygame.image.load(img))
       # 飞机坠毁的图像
       for img in self.destroy_images:
           self._destroy_img_list.append(pygame.image.load(img))
       # 坠毁的音乐
       if self.down_sound_src:
           self.down_sound = pygame.mixer.Sound(self.down_sound_src)

   @property
   def image(self):
       return self.img_list[0]

   def blit_me(self):
       self.screen.blit(self.image, self.rect)

   def move_up(self):
       """ 飞机向上移动 """
       self.rect.top -= self.speed

   def move_down(self):
       """ 飞机向下移动 """
       self.rect.top += self.speed

   def move_left(self):
       """ 飞机向左移动 """
       self.rect.left -= self.speed

   def move_right(self):
       """ 飞机向右移动 """
       self.rect.left += self.speed

   def broken_down(self):
       """ 飞机坠毁效果 """
       # 1. 播放坠毁音乐
       if self.down_sound:
           self.down_sound.play()
       # 2. 播放坠毁的动画
       for img in self._destroy_img_list:
           self.screen.blit(img, self.rect)
       # 3. 坠毁后..
       self.active = False

   def shoot(self):
       """ 飞机发射子弹 """
       bullet = Bullet(self.screen, self, 15)
       self.bullets.add(bullet)


class OurPlane(Plane):
   """ 我方的飞机 """
   # 飞机的图片
   plane_images = [constants.OUR_PLANE_IMG_1, constants.OUR_PLANE_IMG_2]
   # 飞机爆炸的图片
   destroy_images = constants.OUR_DESTROY_IMG_LIST
   # 坠毁的音乐地址
   down_sound_src = None

   def update(self, frame):
       """ 更新飞机的动画效果 """
       if frame % 5:
           self.screen.blit(self.img_list[0], self.rect)
       else:
           self.screen.blit(self.img_list[1], self.rect)

   def move_up(self):
       """ 向上移动,超出范围之后,拉回来 """
       super().move_up()
       if self.rect.top <= 0:
           self.rect.top = 0

   def move_down(self):
       super().move_down()
       if self.rect.top >= self.height - self.plane_h:
           self.rect.top = self.height - self.plane_h

   def move_left(self):
       super().move_left()
       if self.rect.left <= 0:
           self.rect.left = 0

   def move_right(self):
       super().move_right()
       if self.rect.left >= self.width - self.plane_w:
           self.rect.left = self.width - self.plane_w

bullet.py:

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

import pygame
import constants


class Bullet(pygame.sprite.Sprite):
   """ 子弹类 """

   # 子弹状态,True: 活着
   active = True

   def __init__(self, screen, plane, speed=None):
       super().__init__()
       self.screen = screen
       # 速度
       self.speed = speed or 10
       self.plane = plane

       # 加载子弹的图片
       self.image = pygame.image.load(constants.BULLET_IMG)

       # 改变子弹的位置
       self.rect = self.image.get_rect()
       self.rect.centerx = plane.rect.centerx
       self.rect.top = plane.rect.top

       # 发射的音乐效果
       self.shoot_sound = pygame.mixer.Sound(constants.BULLET_SHOOT_SOUND)
       self.shoot_sound.set_volume(0.3)
       self.shoot_sound.play()

   def update(self, *args):
       """ 更新子弹的位置 """
       self.rect.top -= self.speed
       # 超出屏幕的范围
       if self.rect.top < 0:
           self.remove(self.plane.bullets)
           print(self.plane.bullets)
       # 绘制子弹
       self.screen.blit(self.image, self.rect)

老师,您好,我明明认认真真的 去核对,甚至去看了四五遍,还去反复核对敲代码,他还是一样报错???

为什么!

http://img.mukewang.com/climg/5e26eb010929b36b10620459.jpg一样的错误,为什么?老师,您那边执行会不会啊???

写回答

1回答

时间,

2020-01-22

同学,你好。运行这次提供的代码是会报错的,之前学生提供的代码没有报该错误。

在加载我方飞机图片时,同学将静态资源中的值放到列表中,则plane_images为列表嵌套列表的形式,在load_src()的for循环时得到的img为列表,因此会报错。

去掉静态资源中的路径外边的中括号即可

http://img1.sycdn.imooc.com/climg/5e27b005094869a908130146.jpg

http://img1.sycdn.imooc.com/climg/5e27b02c09b3435e07420163.jpg

如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~

0

0 学习 · 8160 问题

查看课程