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的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
Jun 14 Python
Python爬虫实现百度图片自动下载
Feb 04 Python
Python3自动签到 定时任务 判断节假日的实例
Nov 13 Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 Python
Python及Pycharm安装方法图文教程
Aug 05 Python
python logging日志模块原理及操作解析
Oct 12 Python
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
Feb 24 Python
python微信公众号开发简单流程实现
Mar 09 Python
python可以用哪些数据库
Jun 22 Python
Python如何使用vars返回对象的属性列表
Oct 17 Python
Python self用法详解
Nov 28 Python
FP-growth算法发现频繁项集——发现频繁项集
Jun 24 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
这东西价格,可以买几台TECSUN S-2000
2021/03/02 无线电
如何获知PHP程序占用多少内存(memory_get_usage)
2012/09/23 PHP
php的XML文件解释类应用实例
2014/09/22 PHP
WordPress中用于获取搜索表单的PHP函数使用解析
2016/01/05 PHP
laravel5创建service provider和facade的方法详解
2016/07/26 PHP
Redis使用Eval多个键值自增的操作实例
2016/11/04 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
2017/04/27 PHP
PHP Ajax跨域问题解决方案代码实例
2020/08/01 PHP
jquery load()在firefox(火狐)下显示不正常的解决方法
2011/04/05 Javascript
jqGrid增加时--判断开始日期与结束日期(实例解析)
2013/11/08 Javascript
Javascript模仿淘宝信用评价实例(附源码)
2015/11/26 Javascript
神奇!js+CSS+DIV实现文字颜色渐变效果
2016/03/16 Javascript
功能强大的Bootstrap使用手册(一)
2016/08/02 Javascript
Bootstrap选项卡学习笔记分享
2017/02/13 Javascript
关于js中的鼠标事件总结
2017/07/11 Javascript
vue学习之mintui picker选择器实现省市二级联动示例
2017/10/12 Javascript
如何编写一个d.ts文件的步骤详解
2018/04/13 Javascript
vue实现简单loading进度条
2018/06/06 Javascript
vue 搭建后台系统模块化开发详解
2019/05/01 Javascript
Angular如何由模板生成DOM树的方法
2019/12/23 Javascript
Vue中关闭弹窗组件时销毁并隐藏操作
2020/09/01 Javascript
python爬虫入门教程之点点美女图片爬虫代码分享
2014/09/02 Python
简单介绍Python下自己编写web框架的一些要点
2015/04/29 Python
Anaconda多环境多版本python配置操作方法
2017/09/12 Python
Python高级特性切片(Slice)操作详解
2018/09/27 Python
不知道这5种下划线的含义,你就不算真的会Python!
2018/10/09 Python
python判断一个数是否能被另一个整数整除的实例
2018/12/12 Python
对python3 Serial 串口助手的接收读取数据方法详解
2019/06/12 Python
python的几种矩阵相乘的公式详解
2019/07/10 Python
python实现爬虫抓取小说功能示例【抓取金庸小说】
2019/08/09 Python
解决Atom安装Hydrogen无法运行python3的问题
2019/08/28 Python
Python集合操作方法详解
2020/02/09 Python
python 爬取百度文库并下载(免费文章限定)
2020/12/04 Python
销售代表求职自荐信
2013/10/01 职场文书
大学文艺委员竞选稿
2015/11/19 职场文书
MySQL的全局锁和表级锁的具体使用
2021/08/23 MySQL