版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么用Python写个微信飞机大战
/upload/information/20210522/379/529539.png先看效果图/upload/information/20210522/379/529540.gif操作环境具体实现import
os
#
得到当前文件夹下面的material_images目录的路径
source_dir
=
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'material_images')import
pygame
class
Game():
def
__init__(self,
background_image_path,
size=(480,
700),
title='飞机大战',
font_name='方正舒体',
font_size=30,
speed=2000):
'''
:param
background_image_path:
背景图片的路径地址
:param
size:
游戏窗口的大小
:param
title:
游戏窗口的标题
:param
font_name:
指定字体
:param
font_size:
指定字体大小
:param
speed:
背景图滚动整个窗口一次所用时间,单位为ms
'''
self.size
=
size
self.screen
=
pygame.display.set_mode(size)
self.title
=
title
self.background_image_path
=
background_image_path
self.background
=
pygame.image.load(self.background_image_path).convert()
#
设置字体对象,得到系统中自带的字体
self.font
=
pygame.font.SysFont(font_name,
font_size)
#
得到Clock对象,我们可以使用它来获取距离上次绘制图像的时间
self.clock
=
pygame.time.Clock()
#
背景图初始位置
self.height
=
0
#
使用窗口的高度处于滚动的时间,就能得到每ms滚动的距离
self.every_ms_move_distance
=
self.size[1]
/
speed
#
2秒
#
分数
self.score
=
0
#
存放所有的敌机
self.enemies
=
[]
def
show_score(self):
'''
显示分数,
在窗口的的最上方距离上边距10px,
左右居中
'''
pass
def
set_time_passed(self):
#
控制画
的帧,
越大越快
#
得到上一次绘制图像到到现在的时间,
ms
self.time_passed
=
self.clock.tick()
def
draw_background(self):
'''
绘制背景图片,一直向下滚动,营造飞机一直往上面飞的感觉
'''
#
每次移动的距离
=
每ms移动的距离
*
上次到现在的时间(ms)
pass
def
create_enemy(self,
image_path=os.path.join(source_dir,'enemy1.png'),
enemy_number=5):
'''
创建敌机
:param
image_path:
敌机的图片地址
:param
enemy_number:
最多有几个敌机在屏幕上
'''
pass
def
draw_enemies(self,
time_passed,
screen):
'''
绘制敌机到屏幕上,清理跑出窗口的敌机,
:param
time_passed:
上次绘制导向现在经过的时间
:param
screen:
绘制的窗口对象
'''
pass
def
bullet_and_enemy_crash_detection(self,
bullets):
'''
检测子弹是否击中敌机
:param
bullets:
飞机的所有子弹
'''
pass
def
plan_and_enemy_crash_detection(self,
plan,
allow_crash_size=None):
'''
检测敌机与飞机是否相撞
:param
plan:
飞机对象
:param
allow_crash_size:
允许飞机碰撞的大小,只有左右有效
'''
pass
def
draw_plan(self,
plan,
time_passed):
'''
绘制飞机
:param
plan:
飞机对象
:param
time_passed:
距离上次绘制的时间
:return:
'''
pass
def
game_over(self):
'''
游戏结束
'''
while
True:
#
绘制背景图
pass
def
run(self):
'''
游戏入口函数,开始函数,主体函数
:return:
'''
#
设置游戏窗口的大小
pygame.display.set_caption(self.title)
#
初始化一个飞机对象
plan
=
Plan()
while
True:
#
如果飞机自毁完成,
游戏结束,
调用game_over函数
pass
#
检测监听事件
pass
#
检测上下左右的移动案件.
#
w,a,s,d
和
上,下,左,右键都可以
#
然后执行plan.update函数,改变飞机的位置
pass
#
子弹和敌机的碰撞检测
self.bullet_and_enemy_crash_detection(plan.bullets)
#
飞机与敌机的碰撞检测
self.plan_and_enemy_crash_detection(plan)
#
设置属性time_passed的值,
距离上次的时间,方便后面使用
self.set_time_passed()
#
绘制背景图片
self.draw_background()
#
显示分数
self.show_score()
#
生成敌机
self.create_enemy()
#
绘制敌机
self.draw_enemies(time_passed=self.time_passed,
screen=self.screen)
#
绘制飞机
self.draw_plan(plan=plan,
time_passed=self.time_passed)
#
绘制子弹
plan.draw_bullets(time_passed=self.time_passed,
screen=self.screen)
#
显示我们的图像
pygame.display.update()
这里说以下怎样查看自己的系统中有哪些自带的字体.
pygame.font.get_fonts(),这个函数就能够得到系统中所有的自带字体文件。不过,当我们游戏中有中文的时候,我们也得选择支持中文的字体,否则的话是显示不出中文的。
import
pygame
class
Game():
def
__init__(self,
background_image_path,
size=(480,
700),
title='飞机大战',
font_name='方正舒体',
font_size=30,
speed=2000):
'''
:param
background_image_path:
背景图片的路径地址
:param
size:
游戏窗口的大小
:param
title:
游戏窗口的标题
:param
font_name:
指定字体
:param
font_size:
指定字体大小
:param
speed:
背景图滚动整个窗口一次所用时间,单位为ms
'''
self.size
=
size
self.screen
=
pygame.display.set_mode(size)
self.title
=
title
self.background_image_path
=
background_image_path
self.background
=
pygame.image.load(self.background_image_path).convert()
#
设置字体对象,得到系统中自带的字体
self.font
=
pygame.font.SysFont(font_name,
font_size)
#
得到Clock对象,我们可以使用它来获取距离上次绘制图像的时间
self.clock
=
pygame.time.Clock()
#
背景图初始位置
self.height
=
0
#
使用窗口的高度处于滚动的时间,就能得到每ms滚动的距离
self.every_ms_move_distance
=
self.size[1]
/
speed
#
2秒
#
分数
self.score
=
0
#
存放所有的敌机
self.enemies
=
[]
def
show_score(self):
'''
显示分数,
在窗口的的最上方距离上边距10px,
左右居中
'''
pass
def
set_time_passed(self):
#
控制画
的帧,
越大越快
#
得到上一次绘制图像到到现在的时间,
ms
self.time_passed
=
self.clock.tick()
def
draw_background(self):
'''
绘制背景图片,一直向下滚动,营造飞机一直往上面飞的感觉
'''
#
每次移动的距离
=
每ms移动的距离
*
上次到现在的时间(ms)
pass
def
create_enemy(self,
image_path=os.path.join(source_dir,'enemy1.png'),
enemy_number=5):
'''
创建敌机
:param
image_path:
敌机的图片地址
:param
enemy_number:
最多有几个敌机在屏幕上
'''
pass
def
draw_enemies(self,
time_passed,
screen):
'''
绘制敌机到屏幕上,清理跑出窗口的敌机,
:param
time_passed:
上次绘制导向现在经过的时间
:param
screen:
绘制的窗口对象
'''
pass
def
bullet_and_enemy_crash_detection(self,
bullets):
'''
检测子弹是否击中敌机
:param
bullets:
飞机的所有子弹
'''
pass
def
plan_and_enemy_crash_detection(self,
plan,
allow_crash_size=None):
'''
检测敌机与飞机是否相撞
:param
plan:
飞机对象
:param
allow_crash_size:
允许飞机碰撞的大小,只有左右有效
'''
pass
def
draw_plan(self,
plan,
time_passed):
'''
绘制飞机
:param
plan:
飞机对象
:param
time_passed:
距离上次绘制的时间
:return:
'''
pass
def
game_over(self):
'''
游戏结束
'''
while
True:
#
绘制背景图
pass
def
run(self):
'''
游戏入口函数,开始函数,主体函数
:return:
'''
#
设置游戏窗口的大小
pygame.display.set_caption(self.title)
#
初始化一个飞机对象
plan
=
Plan()
while
True:
#
如果飞机自毁完成,
游戏结束,
调用game_over函数
pass
#
检测监听事件
pass
#
检测上下左右的移动案件.
#
w,a,s,d
和
上,下,左,右键都可以
#
然后执行plan.update函数,改变飞机的位置
pass
#
子弹和敌机的碰撞检测
self.bullet_and_enemy_crash_detection(plan.bullets)
#
飞机与敌机的碰撞检测
self.plan_and_enemy_crash_detection(plan)
#
设置属性time_passed的值,
距离上次的时间,方便后面使用
self.set_time_passed()
#
绘制背景图片
self.draw_background()
#
显示分数
self.show_score()
#
生成敌机
self.create_enemy()
#
绘制敌机
self.draw_enemies(time_passed=self.time_passed,
screen=self.screen)
#
绘制飞机
self.draw_plan(plan=plan,
time_passed=self.time_passed)
#
绘制子弹
plan.draw_bullets(time_passed=self.time_passed,
screen=self.screen)
#
显示我们的图像
pygame.display.update()
这里说以下怎样查看自己的系统中有哪些自带的字体.
pygame.font.get_fonts(),这个函数就能够得到系统中所有的自带字体文件。不过,当我们游戏中有中文的时候,我们也得选择支持中文的字体,否则的话是显示不出中文的。
/upload/information/20210522/379/529541.jpg#
显示飞机自毁动画的Mixin类,
可用于飞机和敌机的自毁动画显示
class
DestroyAnimationMixin():
def
show_destroy_animation(self,
time_passed,
destroy_time=200):
'''
显示自毁动画
动画其实就是几张图片切换的比较快,我们的眼睛识别不出来,所以认为他是动态的,也就是动画
:param
time_passed:
距离上次绘制图像到现在的时间,单位ms
:param
destroy_time:
自毁动画总共显示时间,单位ms
'''
#
因为我们的自毁图片有四张,需要依次显示,首先动画的效果
#
self.destroy_image_position
表示第几章自毁图片,从零开始
#
如果大于等于4了,说明自毁动画显示完成,设置self.destroyed变量为True,
方便别处调用
if
self.destroy_image_position
>=
4:
self.destroyed
=
True
return
#
依次加载自毁图片
if
self.time_passed
>=
destroy_time
/
4:
self.image
=
pygame.image.load(os.path.join(source_dir,
self.destroy_images[self.destroy_image_position])).convert_alpha()
self.destroy_image_position
+=
1
self.time_passed
=
0
else:
self.time_passed
+=
time_passed/upload/information/20210522/379/529542.jpg#
飞机类,继承DestroyAnimationMixin,
方便使用显示自毁动画的函数
class
Plan(DestroyAnimationMixin):
def
__init__(self,
image_path=os.path.join(source_dir,'plan.png'),
background_size=(480,
700)):
'''
:param
image_path:
飞机图片地址
:param
background_size:
游戏窗口大小
'''
self.background_size
=
background_size
self.image
=
pygame.image.load(image_path).convert_alpha()
self.image_size
=
self.image.get_size()
self.position
=
[(background_size[0]-self.image_size[0])
/
2,
500]
#
飞机每次移动的距离
self.every_time_move_distance
=
0.5
#
飞机的子弹
self.bullets
=
[]
#
destroy
association
attributes,
自毁相关属性
#
开始自毁
self.start_destroy
=
False
#
自毁结束
self.destroyed
=
False
#
自毁图片
self.destroy_images
=
['me_destroy_1.png',
'me_destroy_2.png',
'me_destroy_3.png',
'me_destroy_4.png']
#
自毁图片位置
self.destroy_image_position
=
0
#
距离上次绘制图像到现在的时间
self.time_passed
=
0
def
update(self,
direction):
'''
更新飞机位置
:param
direction:
飞机移动方向
'''
pass
def
shut(self,
image_path=os.path.join(source_dir,'bullet.png')):
'''
飞机发射子弹
:param
image_path:
子弹图片
'''
pass
def
draw_bullets(self,
time_passed,
screen):
'''
绘制飞机的所有子弹
:param
time_passed:
距离上次绘制图像到现在的时间
:param
screen:
绘制到哪一个窗口中
'''
pass/upload/information/20210522/379/529543.jpg#
敌机类,继承DestroyAnimationMixin,
方便使用显示自毁动画的函数
class
Enemy(DestroyAnimationMixin):
def
__init__(self,
image_path=os.path.join(source_dir,
'enemy1.png'),
speed=2000,
background_size=(480,
700)):
'''
:param
image_path:
敌机图片地址
:param
speed:
敌机移动整个窗口需要的时间,单位ms,也就是速度
:param
background_size:
游戏窗口的尺寸
'''
self.image
=
pygame.image.load(image_path).convert_alpha()
self.speed
=
background_size[1]
/
speed
self.background_size
=
background_size
self.position
=
[random.randint(0,
background_size[0]-self.image.get_size()[0]),
-self.image.get_size()[1]]
#
开始自毁
self.start_destroy
=
False
#
自毁完成
self.destroyed
=
False
#
自毁图片路径
self.destroy_images
=
['enemy1_down1.png',
'enemy1_down2.png',
'enemy1_down3.png',
'enemy1_down3.png']
#
距离上次绘制图像到现在的时间
self.time_passed
=
0
#
自毁图片在self.destroy_images的位置
self.destroy_image_position
=
0
def
update(self,
time_passed):
'''
更新敌机的位置
:param
time_passed:
距离上次绘制图像到现在的时间
:return:
'''
pass#
飞机子弹类
class
Bullet()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Unit 2 Colours and Clothes Lesson 12 公开课教学课件【七年级英语上册(冀教版)】
- 2024年火电电力职业技能鉴定考试-配电值班员笔试参考题库含答案
- 2024年火电电力职业技能鉴定考试-工厂供电笔试参考题库含答案
- 敲开自立的门作文600字
- 《城市和社区可持续发展+潜力评估方法GBT+40757-2021》详细解读
- 2024年江苏住院医师-江苏住院医师肾脏内科笔试参考题库含答案
- 2024-2030全球及中国工业热熔胶枪行业研究及十五五规划分析报告
- 2024年水利工程行业技能考试-防水工笔试参考题库含答案
- LED工业照明产品项目可行性研究报告及运营方案|瑞克咨询|2024年编|
- 2024年机械制造行业技能考试-机械设备安装工笔试参考题库含答案
- (高清版)WST 360-2024 流式细胞术检测外周血淋巴细胞亚群指南
- 山东经贸职业学院辅导员考试试题2024
- 飞行影院项目商业计划书
- 【劳动教育一等奖教学案例】-二年级劳动教育-《三股辫儿我会编》活动方案
- 2024年广西中考数学调研试卷(3月份)(含解析)
- 4月24中国航天日主题宣传教育课件
- 2024-2030年中国稀土永磁电机市场运行态势及行业发展前景预测报告
- 店开业活动方案策划
- 社会隔离对老年人认知功能的影响:有调节的链式中介效应
- 新能源物流车推广方案
- 临床肺功能检查2014年指南解读
评论
0/150
提交评论