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 相关文章推荐
利用soaplib搭建webservice详细步骤和实例代码
Nov 20 Python
分析python服务器拒绝服务攻击代码
Jan 16 Python
使用rpclib进行Python网络编程时的注释问题
May 06 Python
django 解决manage.py migrate无效的问题
May 27 Python
python实现flappy bird游戏
Dec 24 Python
Python小游戏之300行代码实现俄罗斯方块
Jan 04 Python
Python实现的栈、队列、文件目录遍历操作示例
May 06 Python
Django ORM 常用字段与不常用字段汇总
Aug 09 Python
tensorflow2.0与tensorflow1.0的性能区别介绍
Feb 07 Python
Python3加密解密库Crypto的RSA加解密和签名/验签实现方法实例
Feb 11 Python
Python操作Excel工作簿的示例代码(\*.xlsx)
Mar 23 Python
Python jieba结巴分词原理及用法解析
Nov 05 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_xmlhttp 乱码问题解决方法
2009/08/07 PHP
php foreach 参数强制类型转换的问题
2010/12/10 PHP
php使用imagick模块实现图片缩放、裁剪、压缩示例
2014/04/17 PHP
PHP各种异常和错误的拦截方法及发生致命错误时进行报警
2016/01/19 PHP
PHP解压tar.gz格式文件的方法
2016/02/14 PHP
PHP并发多进程处理利器Gearman使用介绍
2016/05/16 PHP
Yii2中hasOne、hasMany及多对多关联查询的用法详解
2017/02/15 PHP
PDO::getAvailableDrivers讲解
2019/01/28 PHP
详解PHP多个进程配合redis的有序集合实现大文件去重
2019/03/06 PHP
laravel框架模型、视图与控制器简单操作示例
2019/10/10 PHP
thinkPHP框架乐观锁和悲观锁实例分析
2019/10/30 PHP
HTML IMG标签 onload 内存溢出导致浏览器CPU占用过高
2021/03/09 Javascript
javascript YUI 读码日记之 YAHOO.util.Dom - Part.4
2008/03/22 Javascript
jquery插件lazyload.js延迟加载图片的使用方法
2014/02/19 Javascript
jQuery实现下拉框功能实例代码
2016/05/06 Javascript
jstl中判断list中是否包含某个值的简单方法
2016/10/14 Javascript
在vue中安装使用vux的教程详解
2018/09/16 Javascript
浅谈一个webpack构建速度优化误区
2019/06/24 Javascript
jquery多级树形下拉菜单的实例代码
2019/07/09 jQuery
python通过索引遍历列表的方法
2015/05/04 Python
python调用fortran模块
2016/04/08 Python
Python实现的生成格雷码功能示例
2018/01/24 Python
python中的随机函数小结
2018/01/27 Python
解决PyCharm import torch包失败的问题
2018/10/13 Python
python将txt文档每行内容循环插入数据库的方法
2018/12/28 Python
Pandas之MultiIndex对象的示例详解
2019/06/25 Python
Python代理IP爬虫的新手使用教程
2019/09/05 Python
python3 中使用urllib问题以及urllib详解
2020/08/03 Python
canvas实现扭蛋机动画效果的示例代码
2018/10/17 HTML / CSS
欧舒丹美国官网:L’Occitane美国
2018/02/23 全球购物
《夹竹桃》教学反思
2014/04/20 职场文书
小学社团活动总结
2014/06/27 职场文书
村党支部群众路线教育实践活动对照检查材料
2014/09/26 职场文书
教学质量月活动总结
2015/05/11 职场文书
python实战之90行代码写个猜数字游戏
2021/04/22 Python
MySQL 使用事件(Events)完成计划任务
2021/05/24 MySQL