python实现微信小游戏打飞机代码
各位小伙伴们想要跟我交流的话可以微信扫一扫下面的公众号二维码,可以在公众号联系我哦~
python中部分代码转自crossin编程教室的代码
import pygame
import random
from sys import exit #向sys模块借用个exit函数来退出程序
#定义一个敌机类
class Enemy:def restar(self):#重置敌机位置与速度self.x=random.randint(100,600)self.y=random.randint(-200,-80)self.speed=random.random()+0.4def __init__(self):self.restar()self.image=pygame.image.load('enermy.jpg')def move(self):if self.y < 800:self.y+=self.speed #向下移动else:self.restar()#定义一个bullet类,封装子弹相关的数据和方法
class Bullet:def __init__(self):self.x=0;self.y=-1;self.image=pygame.image.load('bulet.jpg')self.active=False #默认不激活子弹def move(self):#激活状态下,向上移动if self.active:self.y-=0.8#挡飞出屏幕,设为不激活if self.y<0:self.active=Falsedef restar(self):#重置子弹位置子弹mouseX,mouseY=pygame.mouse.get_pos()self.x=mouseX-self.image.get_width()/2;self.y=mouseY-self.image.get_height()/2;self.active=True;
def checkhit(enemy,bullet):#如果子弹在敌机的图片范围之内if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and (bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()):enemy.restar();bullet.active=Falsereturn Truereturn False
class Plane:def restar(self):self.x=200self.y=600def __init__(self):self.restar()self.image=pygame.image.load('plane.jpg')def move(self):x, y = pygame.mouse.get_pos(); # 跟随鼠标移动x -= self.image.get_width() / 2;y -= self.image.get_height() / 2;self.x=x;self.y=y
def checkcrash(enemy, plane):if (plane.x + 0.7*plane.image.get_width() > enemy.x) and (plane.x + 0.3*plane.image.get_width() < enemy.x + enemy.image.get_width()) and (plane.y + 0.7*plane.image.get_height() > enemy.y) and (plane.y + 0.3*plane.image.get_width() < enemy.y + enemy.image.get_height()):return Truereturn False
pygame.init()
screen=pygame.display.set_mode((1000,700),0,32) #创建一个窗口
pygame.display.set_caption('xwr!')#设置窗口标题
background=pygame.image.load('pygame.jpg') #加载并转换图像
plane=Plane()#加载飞机图像
bullets=[]#创建子弹的list
for i in range(5):bullets.append(Bullet()) #向list添加5发子弹
count_b=len(bullets)#子弹总数
index_b=0 #即将激活的子弹序号
interval_b=0#发射子弹的间隔
enemies=[]
for e in range(5):enemies.append(Enemy())
gameover = False
#分数
score = 0
#用以显示文字的font变量
font = pygame.font.Font(None, 32)
while True: #游戏主循环函数for event in pygame.event.get():if event.type==pygame.QUIT: #接收到退出事件后退出程序pygame.quit()exit()if gameover and event.type==pygame.MOUSEBUTTONUP:plane.restar()for e in enemies:e.restar()for b in bullets:b.restar()score=0;gameover=Falsescreen.blit(background, (0, 0)) # 将背景图画上去if not gameover:interval_b-=1 #发射间隔递减if interval_b<0: #激活一发子弹bullets[index_b].restar()interval_b=80 #重置间隔时间index_b=(index_b+1)%count_b#子弹序号周期性递增for b in bullets: #处于激活状态的子弹,移动位置并绘制if b.active:for e in enemies:if checkhit(e,b):score+=100b.move()screen.blit(b.image,(b.x,b.y))for e in enemies:if checkcrash(e, plane):gameover = Truee.move()screen.blit(e.image,(e.x,e.y))plane.move()screen.blit(plane.image, (plane.x, plane.y))# 在屏幕左上角显示分数text = font.render("Socre: %d" % score, 1, (0, 0, 0))screen.blit(text, (0, 0))else:# 在屏幕中央显示分数text = font.render("Socre: %d" % score, 1, (0, 0, 0))screen.blit(text, (190, 400))passpygame.display.update() #刷新一下界面
python实现微信小游戏打飞机代码相关推荐
- python小游戏代码大全打枪-python实现微信小游戏打飞机代码
以前版本的微信小游戏有一个打飞机的游戏,学完python之后我试着写了下程序去基本实现打飞机的功能,下面是小游戏打飞机的python代码 注:python中部分代码转自crossin编程教室 impo ...
- 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)
微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...
- 微信小游戏《飞机打方块》源码分享
微信小游戏<飞机打方块>源码分享 游戏使用CocosCreator v2.4.2开发 源码:https://gitee.com/propertygame/cocos-creator3.x- ...
- python学法用法 自动刷分_使用python对微信小游戏跳一跳刷分
最近微信游戏很火,所以想着看能不能抓包,刷个分... 抓包 抓包还是使用的fiddle,不会的可以参考抓取手机app的数据(摩拜单车) 但是会发现一打开,抓不了包... 解决无法抓包的问题 所以需要有 ...
- 用Python编写微信小游戏“跳一跳”的运行脚本
前言 更新了微信后发现了一款小游戏跳一跳,但是玩了一下午最高才达到200,每次差点破纪录后总是手抖就挂掉了,气的想要砸手机..闲来无事刷微博的时候正好看到有人分析如何编写脚本自动运行游戏破了3000多 ...
- python开发微信小游戏大全_【python】用python玩微信跳一跳小游戏
Python微信跳一跳实验报告 Step 1 准备工具:一台安卓手机,Windows10,python3,adb驱动,依赖的各种python库 Step 2 PiP安装:下面列出需要安装的库: bac ...
- 微信小游戏实战——飞机大战demo笔记完整篇(函数实现)
1. 目录结构: 2. game.js:入口文件 //game.js文件完整代码: import Main from "./src/mian.js" new Main() 3. g ...
- 微信小游戏之飞机大战解析
一.从抄官方代码开始 1.1 首先是game.js,具体代码如下: import './js/libs/weapp-adapter' import './js/libs/symbol'import M ...
- c 语言500行小游戏代码,500行代码使用python写个微信小游戏飞机大战游戏.pdf
500行行代代码码使使用用python写写个个微微信信小小游游戏戏飞飞机机大大战战游游戏戏 这篇文章主要介绍了500行代码使用python写个微信小游戏飞机大战游戏,本文通过实例代码给大家介绍的非常详 ...
最新文章
- 统计学权威盘点过去50年最重要的统计学思想,因果推理、bootstrap等上榜,Judea Pearl点赞...
- FileUpload控件
- JavaScript实现squareRoot平方根算法(附完整源码)
- apache默认网站
- C语言 函数声明和定义 - C语言零基础入门教程
- bundle文件解压_通过sourcemap解压缩webpack 实战
- hadoop jar
- 初步认识泊松重建(比较全的综合教程)
- uniapp debug能打开release不行_盘点在 Release 下由循环体优化所产生的不确定性 Bug...
- 软件构架 课堂练习一
- HTML5期末大作业:大学生个人网站设计——我们的班级(7页) HTML+CSS+JavaScript 学生DW网页设计作业成品 html网页制作代码大全 html5网页设计作业代码
- 浙江移动无线dns服务器地址,首选dns_浙江省宁波市(中国移动)首选dns是什么,备选dns是什么...
- 量化交易:金融算法交易的前沿发展
- 对腾讯云qcloud音视频通信SDK的调研(优缺点)
- Ant Design Pro学习记录—默认主题配色修改
- (8个方法)解决windows11/10/8/7卡在准备就绪一直转圈
- A - Chess Placing
- Excel2013打印时怎么固定表头及表尾让打印后的每页都可以看得到
- 视觉数据集是基于物体和风景标记的巨大的图像库
- 巧用剪贴蒙版制作西瓜图标
热门文章
- 运算符--位移运算符和一些其他运算符
- 第三章 续:时间控件(TimePicker)
- 有关锁和内存使用的DMV
- 数据结构—链表-建立单链表
- linux hibernate suspend 区别,实现Linux休眠(sleep/hibernate)和挂起(suspend)[转]
- 本周四直播预告(内含福利)丨 经典知识库:MGR原理介绍与案例分享
- 最新课程 | openGauss 快速上手指南课程即将开课
- 5场直播丨PostgreSQL、openGauss、Oracle、GoldenDB、EsgynDB
- 实战课堂:为什么更换存储之后一切正常但RAC集群启动不了?
- 设备接入服务,看完这篇给你整的明明白白