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实现字符串反转的常用方法分析【4种方法】
Sep 30 Python
python使用筛选法计算小于给定数字的所有素数
Mar 19 Python
解决Python一行输出不显示的问题
Dec 03 Python
在win10和linux上分别安装Python虚拟环境的方法步骤
May 09 Python
pytorch使用 to 进行类型转换方式
Jan 08 Python
Python django框架开发发布会签到系统(web开发)
Feb 12 Python
Django使用Celery加redis执行异步任务的实例内容
Feb 20 Python
Numpy中ndim、shape、dtype、astype的用法详解
Jun 14 Python
解决python和pycharm安装gmpy2 出现ERROR的问题
Aug 28 Python
Django如何批量创建Model
Sep 01 Python
Python基础之hashlib模块详解
May 06 Python
python数字图像处理数据类型及颜色空间转换
Jun 28 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
safari下载文件自动加了html后缀问题
2018/11/09 PHP
Js如何判断客户端是PC还是手持设备简单分析
2012/11/22 Javascript
用js格式化金额可设置保留的小数位数
2014/05/09 Javascript
Jquery性能优化详解
2014/05/15 Javascript
jQuery实现统计输入文字个数的方法
2015/03/11 Javascript
如何解决ligerUI布局时Center中的Tab高度大小
2015/11/24 Javascript
jQuery判断元素是否显示 是否隐藏的简单实现代码
2016/05/19 Javascript
用js制作淘宝放大镜效果
2020/10/28 Javascript
vue+vuex+axio从后台获取数据存入vuex实现组件之间共享数据
2017/04/22 Javascript
如何重置vue打印变量的显示方式
2017/12/06 Javascript
vue.js 微信支付前端代码分享
2018/02/10 Javascript
js运算符的一些特殊用法
2018/07/29 Javascript
[38:30]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场2
2014/05/24 DOTA
python的迭代器与生成器实例详解
2014/07/16 Python
python字符串过滤性能比较5种方法
2017/06/22 Python
浅析使用Python操作文件
2017/07/31 Python
Python 实现12306登录功能实例代码
2018/02/09 Python
对pandas写入读取h5文件的方法详解
2018/12/28 Python
快速解决pyqt5窗体关闭后子线程不同时退出的问题
2019/06/19 Python
Python中面向对象你应该知道的一下知识
2019/07/10 Python
Django的models模型的具体使用
2019/07/15 Python
python如何通过twisted搭建socket服务
2020/02/03 Python
关于Theano和Tensorflow多GPU使用问题
2020/06/19 Python
编译 pycaffe时报错:fatal error: numpy/arrayobject.h没有那个文件或目录
2020/11/29 Python
pytorch Dataset,DataLoader产生自定义的训练数据案例
2021/03/03 Python
美国著名的户外用品品牌:L.L.Bean
2018/01/05 全球购物
墨西哥巴士车票在线购买:ClickBus
2018/03/27 全球购物
外贸采购员求职的自我评价
2013/11/26 职场文书
专营店会计助理岗位职责
2013/11/29 职场文书
挂牌仪式主持词
2014/03/20 职场文书
城市创卫标语
2014/06/17 职场文书
学校百日安全生产活动总结
2014/07/05 职场文书
2014年教师党员自我评价范文
2014/09/22 职场文书
同学会邀请函模板
2015/01/30 职场文书
成绩单家长意见
2015/06/03 职场文书
家长对孩子的寒假评语
2015/10/09 职场文书