麻烦老师帮忙看一下我代码的问题,运行报错
来源:3-10 飞机大战项目游戏计分及结果展示
慕婉清5162727
2019-07-24 19:33:54
mian.py 的代码
import pygame
import sys
import constants
from game.plane import Plane, SmallEnemyPlane
from game.plane import OurPlane
from game.war import PlaneWar
def main():
"""游戏入口,main()方法"""
war = PlaneWar()
#添加6架小型敌方飞机
war.add_small_enemies(6)
war.run_game()
if __name__=='__main__':
main()
war.py的代码
import pygame
import sys
import constants
from game.plane import OurPlane, SmallEnemyPlane
from store.result import PlayRest
class PlaneWar(object):
"""飞机大战"""
# 游戏状态
READY = 0 #准备游戏
PLAYING = 1 # 游戏中
OVER = 2 #游戏结束
status = 0 # 0准备中,1 游戏中,2 游戏结束
our_plane = None
frame = 0 # 播放帧数
clock = pygame.time.Clock()
# 一架飞机可以属于多个精灵组
small_enemies = pygame.sprite.Group()
enemies = pygame.sprite.Group()
#游戏结果
rest = PlayRest()
def __init__(self):
# 初始化
pygame.init()
self.width, self.height = 480, 852
# 屏幕对象
self.screen = pygame.display.set_mode((self.width, self.height))
# 设置窗口标题
pygame.display.set_caption('飞机大战')
# 加载背景图片
self.bg = pygame.image.load(constants.BG_IMG)
self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
# 游戏的标题
self.img_game_title = pygame.image.load(constants.BG_IMG_TITLE)
self.img_game_title_rect = self.img_game_title.get_rect()
# 宽度和高度
t_width, t_height = self.img_game_title.get_size()
self.img_game_title_rect.topleft = (int((self.width - t_width) / 2), int(self.height / 2 - t_height))
# 开始按钮
self.btn_start = pygame.image.load(constants.BG_IMG_START_BIN)
self.btn_start_rect = self.btn_start.get_rect()
btn_width, btn_height = self.btn_start.get_size()
self.btn_start_rect.topleft = (int((self.width - btn_width) / 2), int(self.height / 2 + btn_height))
#游戏文字
self.score_font =pygame.font.SysFont('方正粗黑宋简体',32)
# 加载背景音乐
pygame.mixer.music.load(constants.BG_MUSIC)
pygame.mixer.music.play(-1) # 无限循环播放
pygame.mixer.music.set_volume(0.2) # 设置音量
#我方飞机对象
self.our_plane =OurPlane(self.screen,speed=20)
self.clock = pygame.time.Clock()
def bind_event(self):
"""绑定事件"""
# 1.监听事件
for event in pygame.event.get():
# 退出游戏
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠标点击进入游戏
# 游戏正则准备中
if self.status == self.READY:
self.status = self.PLAYING
elif event.type == pygame.KEYDOWN:
# 键盘事件
# 游戏正在游戏中,才需要控制键盘ASWD
if self.status == self.PLAYING:
if event.key == pygame.K_w or event.key == pygame.K_UP:
self.our_plane.move_up()
if event.key == pygame.K_s or event.key == pygame.K_DOWN:
self.our_plane.move_down()
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
self.our_plane.move_left()
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
self.our_plane.move_right()
elif event.key == pygame.K_SPACE:
# 发射子弹
self.our_plane.shoot()
def add_small_enemies(self,num):
"""
随机添加N架小型飞机
:param num: 飞机产生的数量
:return:
"""
#随机添加n架小型飞机
for i in range(num):
plane =SmallEnemyPlane(self.screen,8)
plane.add(self.small_enemies,self.enemies)
def run_game(self):
"""游戏主循环"""
while True:
#1. 设置帧速率
self.clock.tick(60)
self.frame += 1
if self.frame >= 60:
self.frame = 0
#2.绑定事件
self.bind_event()
#3.更新游戏的状态
if self.status == self.READY:
# 游戏正在准备中
# 绘制背景
self.screen.blit(self.bg, self.bg.get_rect())
# 标题
self.screen.blit(self.img_game_title, self.img_game_title_rect)
# 开始按钮
self.screen.blit(self.btn_start, self.btn_start_rect)
elif self.status == self.PLAYING:
# 游戏进行中
# 绘制背景
self.screen.blit(self.bg, self.bg.get_rect())
# 绘制飞机
self.our_plane.update(self)
# 绘制子弹
self.our_plane.bullets.update(self)
# 绘制敌方飞机
self.small_enemies.update()
#游戏分数
score_text = self.score_font.render('得分:{0}'.format(self.rest.score),
False,
constants.TEXT_SCORE_COLOR)
self.screen.blit(score_text,score_text.get_rect())
elif self.status ==self.OVER:
#游戏结束
#游戏背景
self.screen.blit(self.bg_over,self.bg_over.get_rect())
#分数统计
#1.本次总分
score_text = self.score_font.render('{0}'.format(self.rest.score),
False,
constants.TEXT_SCORE_COLOR)
score_text_rect =score_text.get_rect()
text_w,text_h =score_text.get_size()
#改变文字的位置
score_text_rect.topleft = (int((self.width-text_h)/2),int(self.height /2))
self.screen.blit(score_text, score_text_rect())
#2.历史最高分
#self.screen.blit(self.bg_over,self.bg_over.get_rect())
# 更新游戏状态
# 绘制
pygame.display.flip()
bullet.py的代码
import pygame
import constants
class Bullet(pygame.sprite.Sprite):
"""子弹类"""
#子弹状态,True,活的
active = True
def __init__(self,screen,plane,speed=None):
super().__init__()
#速度
self.speed = speed or 10
self.plane = plane
self.screen=screen
#加载子弹的图片
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,war):
"""更新子弹位置"""
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)
#碰撞检测
rest=pygame.sprite.spritecollide(self,war.enemies,False)
#print(rest,666)
for r in rest:
#子弹消失
self.kill()
# 飞机爆炸,坠毁的效果
r.broken_down()
#3.统计游戏成绩
war.rest.score += constants.SCORE_SHOOT_SMALL
constants.py 的代码
import os
import pygame
#项目的根目录
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')
BG_IMG_OVER=os.path.join(ASSETS_DIR,'images/game_over.png')
#标题图片
BG_IMG_TITLE=os.path.join(ASSETS_DIR,'images/game_title.png')
#开始游戏按钮
BG_IMG_START_BIN=os.path.join(ASSETS_DIR,'images/game_start.png')
#背景音乐
BG_MUSIC=os.path.join(ASSETS_DIR,'sounds/game_bg_music.mp3')
#游戏分数验证
TEXT_SCORE_COLOR=pygame.Color(255,255,0)
#击中小型飞机加10分
SCORE_SHOOT_SMALL = 10
#我方飞机的静态资源
OUR_PLANE_IMG_LIST=[
os.path.join(ASSETS_DIR,'images/hero1.png'),
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/bullet1.png')
BULLET_SHOOT_SOUND =os.path.join(ASSETS_DIR,'sounds/bullet.wav')
#敌方小型飞机图片及音效
SMALL_ENEMY_PLANE_IMG_LIST = [os.path.join(ASSETS_DIR,'images/enemy1.png')]
SMALL_ENEMY_DESTROY_IMG_LIST = [
os.path.join(ASSETS_DIR,'images/enemy1_down1.png'),
os.path.join(ASSETS_DIR,'images/enemy1_down2.png'),
os.path.join(ASSETS_DIR,'images/enemy1_down3.png'),
os.path.join(ASSETS_DIR,'images/enemy1_down4.png')
]
#小型飞机坠毁时播放的音乐
SMALL_ENEMY_DOWN_SOUND= os.path.join(ASSETS_DIR,'sounds/enemy1_down.wav')
result.py
class PlayRest(object):
__score = 0# 总分
__life = 3 #生命
__blood= 1000#生命值
@property
def score(self):
"""单次游戏分数"""
return self.__score
@score.setter
def core(self,value):
"""设置游戏分数"""
if value < 0:
return None
self.__score =value
1回答
同学,你好:
同学可以继续完成剩下的学习任务。
如果我解决了同学的问题,请采纳!学习愉快^_^。
相似问题