python实现微信小游戏“飞机大战”

简介: python实现微信小游戏“飞机大战”

以前版本的微信小游戏有一个飞机大战的游戏,学完python之后我试着写了下程序去基本实现这个游戏的部分功能,下面是小游戏飞机大战的python代码

注:python中部分代码转自crossin编程教室

importpygameimportrandomfromsysimportexit#向sys模块借用个exit函数来退出程序#定义一个敌机类classEnemy:
defrestar(self):
#重置敌机位置与速度self.x=random.randint(100,600)
self.y=random.randint(-200,-80)
self.speed=random.random()+0.4def__init__(self):
self.restar()
self.image=pygame.image.load('enermy.jpg')
defmove(self):
ifself.y<800:
self.y+=self.speed#向下移动else:
self.restar()
#定义一个bullet类,封装子弹相关的数据和方法classBullet:
def__init__(self):
self.x=0;
self.y=-1;
self.image=pygame.image.load('bulet.jpg')
self.active=False#默认不激活子弹defmove(self):
#激活状态下,向上移动ifself.active:
self.y-=0.8#挡飞出屏幕,设为不激活ifself.y<0:
self.active=Falsedefrestar(self):
#重置子弹位置子弹mouseX,mouseY=pygame.mouse.get_pos()
self.x=mouseX-self.image.get_width()/2;
self.y=mouseY-self.image.get_height()/2;
self.active=True;
defcheckhit(enemy,bullet):
#如果子弹在敌机的图片范围之内if (bullet.x>enemy.xandbullet.x<enemy.x+enemy.image.get_width()) and (
bullet.y>enemy.yandbullet.y<enemy.y+enemy.image.get_height()
        ):
enemy.restar();
bullet.active=FalsereturnTruereturnFalseclassPlane:
defrestar(self):
self.x=200self.y=600def__init__(self):
self.restar()
self.image=pygame.image.load('plane.jpg')
defmove(self):
x, y=pygame.mouse.get_pos();  # 跟随鼠标移动x-=self.image.get_width() /2;
y-=self.image.get_height() /2;
self.x=x;
self.y=ydefcheckcrash(enemy, plane):
if (plane.x+0.7*plane.image.get_width() >enemy.x) and (
plane.x+0.3*plane.image.get_width() <enemy.x+enemy.image.get_width()) and (
plane.y+0.7*plane.image.get_height() >enemy.y) and (
plane.y+0.3*plane.image.get_width() <enemy.y+enemy.image.get_height()
    ):
returnTruereturnFalsepygame.init()
screen=pygame.display.set_mode((1000,700),0,32)  #创建一个窗口pygame.display.set_caption('xwr!')#设置窗口标题background=pygame.image.load('pygame.jpg')  #加载并转换图像plane=Plane()#加载飞机图像bullets=[]#创建子弹的listforiinrange(5):
bullets.append(Bullet())  #向list添加5发子弹count_b=len(bullets)#子弹总数index_b=0#即将激活的子弹序号interval_b=0#发射子弹的间隔enemies=[]
foreinrange(5):
enemies.append(Enemy())
gameover=False#分数score=0#用以显示文字的font变量font=pygame.font.Font(None, 32)
whileTrue:  #游戏主循环函数foreventinpygame.event.get():
ifevent.type==pygame.QUIT:  #接收到退出事件后退出程序pygame.quit()
exit()
ifgameoverandevent.type==pygame.MOUSEBUTTONUP:
plane.restar()
foreinenemies:
e.restar()
forbinbullets:
b.restar()
score=0;
gameover=Falsescreen.blit(background, (0, 0))  # 将背景图画上去ifnotgameover:
interval_b-=1#发射间隔递减ifinterval_b<0:   #激活一发子弹bullets[index_b].restar()
interval_b=80#重置间隔时间index_b=(index_b+1)%count_b#子弹序号周期性递增forbinbullets:  #处于激活状态的子弹,移动位置并绘制ifb.active:
foreinenemies:
ifcheckhit(e,b):
score+=100b.move()
screen.blit(b.image,(b.x,b.y))
foreinenemies:
ifcheckcrash(e, plane):
gameover=Truee.move()
screen.blit(e.image,(e.x,e.y))
plane.move()
screen.blit(plane.image, (plane.x, plane.y))
# 在屏幕左上角显示分数text=font.render("Socre: %d"%score, 1, (0, 0, 0))
screen.blit(text, (0, 0))
else:
# 在屏幕中央显示分数text=font.render("Socre: %d"%score, 1, (0, 0, 0))
screen.blit(text, (190, 400))
passpygame.display.update()  #刷新一下界面

效果图如下图所示:

6aadf8f8fdd7d4a8d988a1e7cff8e331.jpg

飞机可以发出子弹,只要子弹碰到敌机,敌机就会消失不见。背景及子弹发射的速度还有大小等都可以更换和控制。

8ac2d2a46700461c19d3b65b0ea3b12c.jpg

这是游戏结束的图片,只要飞机撞到敌机就失败了,左上方显示得到的分数Score。

不足之处:消灭敌机还有发射子弹没有加入声音效果,后续版本可以加入爆炸的声音,游戏的沉浸式体验才会更好。

相关文章
|
1月前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
2月前
|
Python
python小游戏7
python小游戏7
|
2月前
|
Python
python小游戏6
python小游戏6
|
2月前
|
Python
python小游戏5
python小游戏5
|
2月前
|
Python
python小游戏4
python小游戏4
|
2月前
|
Python
python小游戏1
python小游戏1
|
2月前
|
安全 C++ Python
小游戏实战-Python实现石头剪刀布+扫雷小游戏
小游戏实战-Python实现石头剪刀布+扫雷小游戏
38 0
|
2月前
|
数据可视化 安全 数据安全/隐私保护
使用Python做个可视化的“剪刀石头布”小游戏
使用Python做个可视化的“剪刀石头布”小游戏
35 0
|
2月前
|
Python
python小游戏3
python小游戏3
|
2月前
|
Python
python小游戏
python小游戏