Java实现抓娃娃_简易抓娃娃机H5的代码实现

本文介绍了如何使用2D渲染引擎PixiJs来实现一个简单的在线抓娃娃机H5应用。详细讲解了页面布局、图片资源的层级关系设置、元素移动动画以及抓杆和爪子的形态变化逻辑,最后展示了完整效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文的实现主要用PixiJs实现。PixiJS是比较著名的2D渲染引擎。可以参照教程做事前学习了解。

准备工作

第一步是图片资源。这些都是从现有的抓娃娃h5中找到的一些png资源。具体可以查看资源目录。都是F5查看资源下载下来的。

页面布局

在页面布局中,需要确定好图片资源的层级关系,可以理解为z-index,确定好图片添加的先后顺序。另外也要计算好,按照页面窗口大小,算出容器的大小,根据比例调整里面所有的图片。可以看到下面的娃娃图片已经被加了2份,这是为了从视觉上循环的时候总可以看到3个娃娃。至于为什么这么做,可以看下面的动画的解析。

setup() {

var resource = this.loader.resources["p2_top.png"]

var img = new PIXI.Sprite(resource.texture);

var ratio = this.app.view.width / img.width

// 背景

var resourceBg = this.loader.resources["bg.png"]

var imgBg = new PIXI.Sprite(resourceBg.texture)

imgBg.width = this.app.view.width

imgBg.height = this.app.view.height

imgBg.x = 0

imgBg.y = 0

this.app.stage.addChild(imgBg)

// 移动杆

var resourceHead = this.loader.resources["p2_wood_1.png"]

var imgHead = new PIXI.Sprite(resourceHead.texture)

imgHead.width = imgHead.width * ratio

imgHead.height = imgHead.height * ratio

imgHead.y = this.uplimit

imgHead.x = (this.app.view.width - imgHead.width) / 2

this.app.stage.addChild(imgHead)

this.imgHead = imgHead

//爪子

// 抓住

var resourcdPawCatch = this.loader.resources["p2_claw_0.png"]

var imgPawCatch = new PIXI.Sprite(resourcdPawCatch.texture)

imgPawCatch.width = imgPawCatch.width * ratio

imgPawCatch.height = imgPawCatch.height * ratio

imgPawCatch.x = (this.app.view.width - imgPawCatch.width) / 2

imgPawCatch.y = imgHead.height + imgHead.y

this.app.stage.addChild(imgPawCatch)

this.imgPawCatch = imgPawCatch

// 没抓住

var resourcdPawRelease = this.loader.resources["p2_claw_1.png"]

var imgPawRelease = new PIXI.Sprite(resourcdPawRelease.texture)

imgPawRelease.width = imgPawRelease.width * ratio

imgPawRelease.height = imgPawRelease.height * ratio

imgPawRelease.x = (this.app.view.width - imgPawRelease.width) / 2

imgPawRelease.y = imgHead.height + imgHead.y

imgPawRelease.visible = false

this.app.stage.addChild(imgPawRelease)

this.imgPawRelease = imgPawRelease

//移动娃娃

var resourceBaby = this.loader.resources["baby_0.png"]

var imgBaby = new PIXI.Sprite(resourceBaby.texture)

imgBaby.width = imgBaby.width * ratio

imgBaby.height = imgBaby.height * ratio

this.inter = (this.app.view.width - imgBaby.width * 3) / 2

this.babyWidth = imgBaby.width

imgBaby.x = 0

imgBaby.y = (829 - 267) * ratio

this.app.stage.addChild(imgBaby)

this.babies.push(imgBaby)

resourceBaby = this.loader.resources["baby_1.png"]

imgBaby = new PIXI.Sprite(resourceBaby.texture)

imgBaby.width = imgBaby.width * ratio

imgBaby.height = imgBaby.height * ratio

imgBaby.x = this.babyWidth + this.inter + (this.babyWidth - imgBaby.width) / 2

imgBaby.y = (829 - 267) * ratio

this.app.stage.addChild(imgBaby)

this.babies.push(imgBaby)

resourceBaby = this.loader.resources["baby_2.png"]

imgBaby = new PIXI.Sprite(resourceBaby.texture)

imgBaby.width = imgBaby.width * ratio

imgBaby.height = imgBaby.height * ratio

imgBaby.x = (this.babyWidth+ this.inter) * 2 + (this.babyWidth - imgBaby.width) / 2

imgBaby.y = (829 - 267) * ratio

this.app.stage.addChild(imgBaby)

this.babies.push(imgBaby)

resourceBaby = this.loader.resources["baby_0.png"]

imgBaby = new PIXI.Sprite(resourceBaby.texture)

imgBaby.width = imgBaby.width * ratio

imgBaby.height = imgBaby.height * ratio

imgBaby.x = (this.babyWidth + this.inter) * 3 + (this.babyWidth - imgBaby.width) / 2

imgBaby.y = (829 - 267) * ratio

this.app.stage.addChild(imgBaby)

this.babies.push(imgBaby)

resourceBaby = this.loader.resources["baby_1.png"]

imgBaby = new PIXI.Sprite(resourceBaby.texture)

imgBaby.width = imgBaby.width * ratio

imgBaby.height = imgBaby.height * ratio

imgBaby.x = (this.babyWidth + this.inter) * 4 + (this.babyWidth - imgBaby.width) / 2

imgBaby.y = (829 - 267) * ratio

this.app.stage.addChild(imgBaby)

this.babies.push(imgBaby)

resourceBaby = this.loader.resources["baby_2.png"]

imgBaby = new PIXI.Sprite(resourceBaby.texture)

imgBaby.width = imgBaby.width * ratio

imgBaby.height = imgBaby.height * ratio

imgBaby.x = (this.babyWidth + this.inter) * 5 + (this.babyWidth - imgBaby.width) / 2

imgBaby.y = (829 - 267) * ratio

this.app.stage.addChild(imgBaby)

this.babies.push(imgBaby)

// 背景图

img.width = this.app.view.width

img.height = this.app.view.height

this.app.stage.addChild(img)

// 顶部固定

resource = this.loader.resources["p2_woodhead.png"]

img = new PIXI.Sprite(resource.texture)

img.width = img.width * ratio

img.height = img.height * ratio

img.x = (this.app.view.width - img.width) / 2

img.y = 140 * ratio

this.app.stage.addChild(img)

}

复制代码

由此得到以下的效果图。aef3ed82ca2cf24c9a15eda58d0e0901.png

元素移动

娃娃移动

好了现在就要移动,以下是完成一个循环步骤中的状态变化,移动的变化以一个间隔为准。这个间隔的计算以最大的元素宽度作为基准,然后屏幕放下3个,因此间隔大小是(容器宽度 - 3 * 元素宽度)/ 2。由此可以看到一共需要6个元素才能完成循环。另外由于每个块的宽度不一致,因此计算块的x的时候要考虑居中问题。重置元素放到循环第一位的时候也要考虑居中问题来计算x。3d8a833b10733ceb84907390d660f2f8.png

由此得到的娃娃循环动画计算方法如下

// 添加循环处理

this.app.ticker.add((delta) => this.gameLoop(delta))

...

gameLoop(...args: any[]){

for (var inx = 0; inx < this.babies.length; ++inx){

//假定没有被抓取

if (!this.babiesTaken[inx]){

this.babies[inx].x = this.babies[inx].x + 1

if (this.babies[inx].x >= (this.babyWidth + this.inter) * 5 + this.inter + (this.babyWidth - this.babies[inx].width) / 2){

this.babies[inx].x = -this.babyWidth + (this.babyWidth - this.babies[inx].width) / 2

}

}

}

}

复制代码

得到效果图如下3f2f46b71aeab71efe78cd1f42e4d9a8.gif

抓杆移动

移动杆的效果除了向下移动外,还要改变爪子的形态,在快要到达下面的时候要展开爪子,到底部又要收起爪子。具体的算法如下

if (this.catching){

if (this.movingDown){

this.imgHead.y = this.imgHead.y + 1

if (this.imgHead.y >= this.downlimit){

// 开始抓娃娃

this.imgPawRelease.visible = false

this.imgPawCatch.visible = true

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

this.movingDown = false

}

else if (this.imgHead.y >= this.switchPaw){

// 开始展开爪子

this.imgPawRelease.visible = true

this.imgPawCatch.visible = false

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

}

else{

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

}

}

else {

this.imgHead.y = this.imgHead.y - 1

if (this.imgHead.y >= this.uplimit){

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

this.movingDown = false

}

else{

this.imgPawRelease.visible = false

this.imgPawCatch.visible = true

this.movingDown = true

}

}

}

复制代码

由此得到的循环效果如下,1743662d19d51dd7eccc570171d020ab.gif

如果我们判断娃娃被抓住了,那么就可以修改娃娃的y值。

以中间的娃娃为例,

if (this.catching){

if (this.movingDown){

this.imgHead.y = this.imgHead.y + 1

if (this.imgHead.y >= this.downlimit){

// 开始抓娃娃

this.imgPawRelease.visible = false

this.imgPawCatch.visible = true

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

this.babies[1].y = this.imgPawCatch.y + this.imgPawCatch.height - 20

this.movingDown = false

}

else if (this.imgHead.y >= this.switchPaw){

// 开始展开爪子

this.imgPawRelease.visible = true

this.imgPawCatch.visible = false

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

}

else{

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

}

}

else {

this.imgHead.y = this.imgHead.y - 1

if (this.imgHead.y >= this.uplimit){

this.imgPawCatch.y = this.imgHead.height + this.imgHead.y

this.babies[1].y = this.imgPawCatch.y + this.imgPawCatch.height - 20

this.imgPawRelease.y = this.imgHead.height + this.imgHead.y

this.movingDown = false

}

else{

this.imgPawRelease.visible = false

this.imgPawCatch.visible = true

this.movingDown = true

this.babies[1].y = (829 - 267) * this.ratio

}

}

}

复制代码

效果如下,f39f546bd40747fcc7aea1eefb782f82.gif

碰撞集成

目前每个元素都有了,我们需要把抓捕的条件判断加进去。这里面主要是依据爪子的位置和娃娃的位置判定的,可以再具体调整,为了降低难度,放宽了条件

catch() {

for (var inx = 0; inx < this.babies.length; ++inx){

if (Math.abs(this.babies[inx].y - this.imgPawCatch.y) < 100

&& Math.abs((this.babies[inx].x + this.babies[inx].width / 2) - (this.imgPawCatch.x + this.imgPawCatch.width / 2)) < 100 ) {

this.babiesTaken[inx] = true

this.catchedBaby = this.babies[inx]

break;

}

}

}

复制代码

最终效果和代码

由于素材资源有限,简单用按钮来操作。得到最终效果图如下:5c498c355afcbfaae2ca2531bf00b128.gif

代码请戳github

Thinkphp大灌篮游戏源码 微信投篮源码 适用范围: Thinkphp内核 微信大灌篮游戏源码 微信投篮源码下载 免公众号+个人免签支付接口 运行环境: php+mysql 源码详细: Thinkphp大灌篮游戏源码 微信投篮源码 运行环境:PHP+MYSQL 程序内核:Thinkphp 内含详细安装教程,请严格按照文档来安装,顺序错了也会安装不起来。 大灌篮游戏源码 投篮源码微信精彩源码手动提现 【免公众号+个人免签支付接口】 用户提现三种方式 1.手动(用户提交收款二维码,后台可看见用户提交的信息) 2.加客服微信(用户加客服微信手动下分) 3.自动(需要您有微信企业付款到零钱的公众号) 不需要公众号也能玩,支持个人免签支付接口+企业自动提现,新增个人免签支付接口,用户提现有三种:手动提现,客服,企业微信自动提现,喜欢的朋友下载吧。 1.下载222111大灌篮目录里的 大灌篮完整运营版 2.打开这个目录,除了这两个红色两个文件其他缓存文件都删除不然网站打不开: 3.修改数据库信息config.php文件,路径是gznet\Cache\config.php: 4.打开数据库:找到qz_user这个表,里面的admin 是管理员用户名 旁边的是密码md5加密的,密码你自己在网上生成md5加密的密码粘贴就行了。密码默认 111111 5.后台地址: http://域名/?g=Admin&m=Login 前台地址: http://域名/index.php/User/Ball/index.html 先进去输入手号,登录以后生成二维码用微信扫就可以了; 更换这个客服二维码成你的, D:\phpStudy\WWW\ball\resource\assets\images\er_wma 系统设置里面的个人收款,和提现那里都不要改,改的话就改不回来了,只能重新安装 免签软件注册地址 http://sk.wapwg.cn/ 软件配置:shoukuanla\config.php 修改地址:gznet\Lib\Action\User\PayAction.class.php 玩这个都是微信,建议支付宝和微信的收款图片都用微信的 收款图片修改:shoukuanla\images 微信夹娃娃红包游戏源码 **精彩互换娃娃源码+安装教程 整套源码使用ThinkPHP框架开发,mysql数据,傻瓜化自定安装。附件中包含安装配置说明文档 1、运行环境:windows+php5.4+mysql2、具备条件:认证服务号带支付、备案域名、vps服务器 1、拼手气红包,用户先支付固定金额的钱,然后拆一个红包,大小是随的。2、可添加无数个不同支付面值的红包,如果只有一个,直接进入支付页面。3、代理资格、可自动或手动设置、根据充值可自动或手动升级。4、代理佣金可自动或人工结算,佣金比例后台可调,可分等级,可扣量。5、红包刷新频率可设置,设置多少分钟刷新一次,每次抽的都不同。6、数据统计非常详细、明确、图表化显示。7、抽中红包信息可滚动显示。9、震撼功能:更换公众号用户数据不会丢失,可以多个公众号轮流发红包。10、可根据抽红包次数,控制抽中红包大小,比如可以设置前两次抽的大,以后抽的小。11、可限制用户每天抽100次12、用户首页集成个人中心功能、用户可查看自己详细的数据13、用户可充值、抢红包更爽,再也不用支付一次,抢一次了14、灵活强大的大转盘功能,独家开发,自己可完全个性定制。15、双佣金模式,三级分销单局佣金和充值百分比佣金16、自动黑名单功能,判断用户行为,自动加入黑名单17、刮刮乐游戏,让用户一次刮个够!18、域名防封化处理,自动短链接,域名使用寿命大幅度提高
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值