python实现微信打飞机游戏


Posted in Python onMarch 24, 2020

本文实例为大家分享了python实现微信打飞机游戏的具体代码,供大家参考,具体内容如下

import pygame
import random
import sys
 
#初始化
pygame.init()
pygame.display.set_caption('飞机火拼')#设置窗口标题
screen= pygame.display.set_mode((320, 570), 0, 32)
pygame.mouse.set_visible(False)#隐藏光标
 
#加载图片
boom1=pygame.image.load('../Game/fly/src/boom1.png')
boom2=pygame.image.load("../Game/fly/src/boom2.png")
bullet=pygame.image.load('../Game/fly/src/bullet.png')
plane=pygame.image.load("../Game/fly/src/plane.png").convert_alpha()
enemy=pygame.image.load('../Game/fly/src/enemy.png')
#设置窗口图标
pygame.display.set_icon(plane)
background=pygame.image.load("../Game/fly/src/bg_01.png")
start=pygame.image.load('../Game/fly/src/start.png')
pause=pygame.image.load("../Game/fly/src/pause.png")
 
#pygame.mixer.init()
#Xexplosion=pygame.mixer.Sound("explosion.mp3")
#Xbullet=pygame.mixer.Sound("../Game/fly/sound/bullet.mp3")
#XgameOver=pygame.mixer.Sound("../Game/fly/sound/game_over.mp3")
#Xexplosion.set_volume(1)
 
#pygame.mixer.music.load("../Game/fly/sound/game_music.mp3")
#pygame.mixer.music.play(-1, 0.0)
#pygame.mixer.music.set_volume(0.25)
 
i=0
#分数
score=0
font=pygame.font.SysFont('微软雅黑', 36)
#子弹
bullets=[]
#敌机
enemies=[]
#记录敌机爆炸位置
boomplace=[]
#游戏结束
gameover=False
pygame.mouse.set_pos(200, 200)
while True:
 i += 1
 if i > 200:
  i =0
 screen.blit(background, (0, 0))
 #通过鼠标控制飞机
 x, y=pygame.mouse.get_pos()
 #print(x, y)
 x -= plane.get_width()/2
 y -= plane.get_height()/2
 screen.blit(plane, (x, y))
 
 #每25帧 调用一次
 if i%25 == 0:
  #返回 按下左键 中键 右键
  l,m,r=pygame.mouse.get_pressed()
  if l ==True:
   #生成子弹位置添加
   bullets.append([x+plane.get_width()/2, y])
   #Xshoot.play()
   for place in bullets:
    #子弹位置超出边界
    if place[1]<=0:
     bullets.remove(place)
  #移动子弹位置
  for place in bullets:
   place[1] -= 55
 #绘制子弹
 for place in bullets:
  screen.blit(bullet,(place[0],place[1]))
 #敌机生成 移动 消失
 if i%199 == 0:
  enemies.append([random.randint(0,320-enemy.get_width()),-enemy.get_width()/2])
  for place in enemies:
   if place[1] >= 600:
    enemies.remove(place)
  for place in enemies:
   place[1] += 35
 for place in enemies:
  screen.blit(enemy,(place[0],place[1]))
 #敌机爆炸
 for place in boomplace:
  if place[2]>0:
   screen.blit(boom1,(place[0],place[1]))
   place[2] -=1
 #子弹碰撞检测
 for bulletplace in bullets:
  for enemyplace in enemies:
   if (bulletplace[0] > enemyplace[0] and bulletplace[0] < enemyplace[0] + enemy.get_width()) and \
    (bulletplace[1] > enemyplace[1] and bulletplace[1] < enemyplace[1] + enemy.get_height()):
    boomflag = 75
    enemyplace.append(boomflag)
    boomplace.append(enemyplace)
    enemies.remove(enemyplace)
    bullets.remove(bulletplace)
    #Sexplosion.play()
    score += 1
 #飞机碰撞检测
 for enemyplace in enemies:
  if (x + 0.7*plane.get_width() > enemyplace[0]) and (x + 0.3*plane.get_width()<enemyplace[0]+ enemy.get_width())\
   and (y + 0.7*plane.get_height() > enemyplace[1]) and (y + 0.3*plane.get_height()<enemyplace[1]+ enemy.get_height()):
   enemies.remove(enemyplace)
 
   screen.blit(pause,(0,0))
   screen.blit(boom2, (x, y))
   #for place in bullets:
    #screen.blit(bullet, (place[0], place[1]))
   #for place in enemies:
    #screen.blit(enemy, (place[0], place[1]))
 
   text=font.render('Score%d'%score,True,(0,0,0))
   screen.blit(text,(200,270))
   text=font.render("Right ReStart",True,(0, 0,0))
   screen.blit(text,(30,310))
   pygame.display.update()#重新绘制窗口
 
   gameover=True
   while gameover == True and r == False :
    l, m, r = pygame.mouse.get_pressed()
    for event in pygame.event.get():
     if event.type == pygame.QUIT:
      pygame.quit()
      exit()
   #重置游戏
   i=0
   score=0
   bullets=[]
   enemies=[]
   boomplace=[]
   x, y=185-plane.get_width()/2,550 - plane.get_height()/2
 #检测 暂停 继续
 l,m,r=pygame.mouse.get_pressed()
 if r == True:
  screen.blit(background,(0,0))
 
  screen.blit(start,(0,0))
  screen.blit(plane,(x,y))
  for place in bullets:
   screen.blit(bullet,(place[0],place[1]))
  for place in enemies:
   screen.blit(enemy,(place[0],place[1]))
  for place in boomplace:
   if place[2] >0:
    screen.blit(boom1,(place[0],place[1]))
    place[2] -=1
  text=font.render(u'Score %d'%score,1,(0,0,0))
  screen.blit(text,(50,0))
 
  if gameover == True:
   x,y =185,550
   gameover =False
   text =font.render(' left to start',True,(0,0,0))
   screen.blit(text,(35,300))
  else:
   x,y=pygame.mouse.get_pos()
   text=font.render(' left to continue ',True,(0,0,0))
   screen.blit(text,(10,300))
  pygame.display.update()
 
  while True :
   for event in pygame.event.get():
    if event.type == pygame.QUIT:
     pygame.quit()
     exit()
   l, m, r = pygame.mouse.get_pressed()
   if l == True:
    pygame.mouse.set_pos(x,y)
    break
 text=font.render(u"%d"%score, True, (0, 0, 0))
 screen.blit(text,(50, 0))
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
   pygame.quit()
   exit()

效果图:

python实现微信打飞机游戏

更多关于python游戏的精彩文章请点击查看以下专题:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中文编码问题小结
Sep 28 Python
在Python中操作字符串之replace()方法的使用
May 19 Python
Python爬虫包 BeautifulSoup  递归抓取实例详解
Jan 28 Python
Windows下的Jupyter Notebook 安装与自定义启动(图文详解)
Feb 21 Python
解决python升级引起的pip执行错误的问题
Jun 12 Python
python实现抖音视频批量下载
Jun 20 Python
docker django无法访问redis容器的解决方法
Aug 21 Python
实现Python与STM32通信方式
Dec 18 Python
详解pycharm连接不上mysql数据库的解决办法
Jan 10 Python
使用python创建生成动态链接库dll的方法
May 09 Python
Pycharm 解决自动格式化冲突的设置操作
Jan 15 Python
python实现简单的井字棋游戏(gui界面)
Jan 22 Python
Python类的动态绑定实现原理
Mar 21 #Python
Python类和实例的属性机制原理详解
Mar 21 #Python
Python生成器常见问题及解决方案
Mar 21 #Python
Python作用域与名字空间原理详解
Mar 21 #Python
Python小整数对象池和字符串intern实例解析
Mar 21 #Python
Python描述符descriptor使用原理解析
Mar 21 #Python
Python如何省略括号方法详解
Mar 21 #Python
You might like
php异常处理方法实例汇总
2015/06/24 PHP
php防止网站被攻击的应急代码
2015/10/21 PHP
PHP错误机制知识汇总
2016/03/24 PHP
ThinkPHP下表单令牌错误与解决方法分析
2017/05/20 PHP
Laravel中的Blade模板引擎示例详解
2017/10/10 PHP
返回对象在当前级别中是第几个元素的实现代码
2011/01/20 Javascript
javascript中window.event事件用法详解
2012/12/11 Javascript
jquery实现的一个导航滚动效果具体代码
2013/05/27 Javascript
基于canvas实现的钟摆效果完整实例
2016/01/26 Javascript
JS获取checkbox的个数简单实例
2016/08/19 Javascript
Vue学习笔记进阶篇之单元素过度
2017/07/19 Javascript
jQueryMobile之窗体长内容的缺陷与解决方法实例分析
2017/09/20 jQuery
vue项目中用cdn优化的方法
2018/01/03 Javascript
Node.js引入UIBootstrap的方法示例
2018/05/11 Javascript
微信小程序实现顶部下拉菜单栏
2018/11/04 Javascript
javascript合并两个数组最简单的实现方法
2019/09/14 Javascript
Python实现保证只能运行一个脚本实例
2015/06/24 Python
Python2.7简单连接与操作MySQL的方法
2016/04/27 Python
详解Python3操作Mongodb简明易懂教程
2017/05/25 Python
Python2.X/Python3.X中urllib库区别讲解
2017/12/19 Python
Python实现决策树C4.5算法的示例
2018/05/30 Python
Python中GIL的使用详解
2018/10/03 Python
对python实时得到鼠标位置的示例讲解
2018/10/14 Python
利用python脚本如何简化jar操作命令
2019/02/24 Python
解决Django no such table: django_session的问题
2020/04/07 Python
关于Python错误重试方法总结
2021/01/03 Python
悦木之源美国官网:Origins美国
2016/08/01 全球购物
FILA斐乐中国官方商城:意大利运动品牌
2017/01/25 全球购物
高二物理教学反思
2014/02/08 职场文书
人力资源管理专业自荐信
2014/06/24 职场文书
2015年药店店长工作总结
2015/04/29 职场文书
小学班主任心得体会
2016/01/07 职场文书
2016年度师德标兵先进事迹材料
2016/02/26 职场文书
七年级作文之《我和我的祖国》观后感作文
2019/10/18 职场文书
springboot中的pom文件 project报错问题
2022/01/18 Java/Android
Django框架中视图的用法
2022/06/10 Python