报错,老师帮忙看下哪里有问题,谢谢
来源:3-4 飞机大战项目飞机类的封装
qq__8359
2019-12-17 00:12:20
'''
T----------------T------飞机的基类---T-----------------T
我方飞机 敌方小型飞机 敌方中型飞机 敌方大型飞机
'''
import pygame
import constants
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()
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.down += self.speed
def move_left(self):
'''向左移动'''
self.rect.left -= self.speed
def move_right(self):
'''向右移动'''
self.rect.right += 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
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
import sys
import pygame
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_img = pygame.image.load(constants.BG_IMG)
# 加载游戏的大标题
img_title = pygame.image.load(constants.GAME_TITLE)
img_title_rect = img_title.get_rect()
print(img_title_rect)
# 标题的宽高 计算标题的坐标
t_width, t_height = img_title.get_size()
img_title_rect.topleft = (int((width - t_width) / 2),
int(height / 2 - t_height))
# 加载游戏的开始按钮
game_start = pygame.image.load(constants.GAME_START)
game_start_rect = game_start.get_rect()
print(game_start_rect)
# 按钮的宽高 计算按钮的坐标
s_width, s_height = game_start.get_size()
game_start_rect.topleft = (int((width - s_width) / 2),
int(height / 2 + s_height))
# 加载背景音乐
pygame.mixer.music.load(constants.BG_MUSIC) # 加载音乐
pygame.mixer.music.play(-1) # 无限循环播放
pygame.mixer.music.set_volume(0.4) # 设置音量
# 游戏状态
status = 0 # 0 准备状态, 1 游戏中, -1 游戏结束。
our_plane = OurPlane(screen)
while True:
'''游戏的循环'''
# 1,监听事件
for event in pygame.event.get():
# 退出游戏
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠标点击进入游戏
# 游戏处于准备状态0,鼠标点击才能进入游戏
if status == 0:
status = 1
# 更新游戏状态
if status == 0:
# 游戏处于准备状态0
# 游戏背景图片
screen.blit(bg_img, bg_img.get_rect())
# 游戏标题 (图片,位置坐标)
screen.blit(img_title, img_title_rect)
# 游戏开始按钮 (图片,位置坐标)
screen.blit(game_start, game_start_rect)
elif status == 1:
# 进入游戏
screen.blit(bg_img, bg_img.get_rect())
# 绘制飞机
our_plane.blit_me() # 调用plane里的方法绘制
# 绘制游戏屏幕里的图像
#screen.blit(bg_img, bg_img.get_rect())
pygame.display.flip()
if __name__ == '__main__':
main()
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")
# 标题提示
GAME_TITLE = os.path.join(ASSETS_DIR, "images/game_title.png")
# 开始按钮
GAME_START = os.path.join(ASSETS_DIR, "images/game_start.png")
# 背景音乐
BG_MUSIC = os.path.join(ASSETS_DIR, "sounds/game_bg_music.mp3")
'''我方飞机的静态资源'''
# 我方飞机图片
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"),
]
老师帮忙看下哪里有问题3回答
同学,你好。OUR_DESTROY_IMG_LIST为列表类型的数据,在使用时直接赋值即可,不需要再添加一层方括号。


同学可根据上述方法进行修改,同学可在提问时把报错信息一起提供,这样可以更好的为您解决问题。
如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~
qq__8359
提问者
2019-12-17
引入不了,是虚拟环境的问题吗?

qq__8359
提问者
2019-12-17
老师,我换了台电脑,运行报错怎么回事?

相似问题