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 ORM框架SQLAlchemy学习笔记之安装和简单查询实例
Jun 10 Python
python中sys.argv参数用法实例分析
May 20 Python
python Django模板的使用方法
Jan 14 Python
python 时间戳与格式化时间的转化实现代码
Mar 23 Python
python中的turtle库函数简单使用教程
Jul 23 Python
Python实现的字典排序操作示例【按键名key与键值value排序】
Dec 21 Python
python读取几个G的csv文件方法
Jan 07 Python
对Python实现累加函数的方法详解
Jan 23 Python
python3 map函数和filter函数详解
Aug 26 Python
Django项目使用ckeditor详解(不使用admin)
Dec 17 Python
python实现俄罗斯方块游戏(改进版)
Mar 13 Python
用Python简陋模拟n阶魔方
Apr 17 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 字符串编码截取函数(兼容utf-8和gb2312)
2009/05/02 PHP
第4章 数据处理-php正则表达式-郑阿奇(续)
2011/07/04 PHP
PHP以指定字段为索引返回数据库所取的数据数组
2013/06/30 PHP
php中的filesystem文件系统函数介绍及使用示例
2014/02/13 PHP
PHP Static延迟静态绑定用法分析
2016/03/16 PHP
关于ThinkPhp 框架表单验证及ajax验证问题
2017/07/19 PHP
jQuery Validation插件remote验证方式的Bug解决
2010/07/01 Javascript
jQuery jqgrid 对含特殊字符json 数据的 Java 处理方法
2011/01/01 Javascript
如何用ajax来创建一个XMLHttpRequest对象
2012/12/10 Javascript
Jquery实现点击切换图片并隐藏显示内容(2种方法实现)
2013/04/11 Javascript
浅析JavaScript事件和方法
2015/02/28 Javascript
Jquery实现地铁线路指示灯提示牌效果的方法
2015/03/02 Javascript
Javascript 是你的高阶函数(高级应用)
2015/06/15 Javascript
jQuery获取单击节点对象的方法
2016/06/02 Javascript
js 动态给元素添加、移除事件的实现方法
2016/07/19 Javascript
js接收并转化Java中的数组对象的方法
2016/08/11 Javascript
Html中 IFrame的用法及注意点
2016/12/22 Javascript
JS 实现获取验证码 倒计时功能
2018/10/29 Javascript
webpack配置proxyTable时pathRewrite无效的解决方法
2018/12/13 Javascript
详解js中的几种常用设计模式
2020/07/16 Javascript
javascript利用canvas实现鼠标拖拽功能
2020/07/23 Javascript
小程序实现简单语音聊天的示例代码
2020/07/24 Javascript
vue绑定数字类型 value为数字的实例
2020/08/31 Javascript
Python pandas.DataFrame调整列顺序及修改index名的方法
2019/06/21 Python
Pandas DataFrame数据的更改、插入新增的列和行的方法
2019/06/25 Python
python对常见数据类型的遍历解析
2019/08/27 Python
Python requests获取网页常用方法解析
2020/02/20 Python
浅析Python面向对象编程
2020/07/10 Python
Python Pillow(PIL)库的用法详解
2020/09/19 Python
python可视化分析的实现(matplotlib、seaborn、ggplot2)
2021/02/03 Python
中东地区最大的奢侈品市场:The Luxury Closet
2019/04/09 全球购物
物流管理专业应届生求职信
2013/11/21 职场文书
交通安全演讲稿
2014/01/07 职场文书
班级团队活动方案
2014/08/14 职场文书
爱国教育主题班会
2015/08/14 职场文书
CSS中calc(100%-100px)不加空格不生效
2023/05/07 HTML / CSS