帮助中心
获取牛币
联系我们
关于我们
版权声明
素材牛首页
微博登录
QQ登录
微信扫码登录
注册
登录
新浪微博
QQ登陆
微信登录
热门搜索:
小程序
支付
Java
后台模板
上传
商城模板
jQuery
手机
Thinkphp
微信
Vue.js
首 页
网页特效
整站源码
PHP实例
网站模版
工具箱
常用代码
论坛
游戏源码
资源分享
当前位置:
首页
»
精选常用代码
» 2D版跳一跳小游戏代码
2D版跳一跳小游戏代码
浏览:1105次
-
评论:1次
-
发布时间:2018-01-23
Html
Css
Js
var canvas = document.getElementById('stage'); var context = canvas.getContext('2d'); Math.randomRange = function(min, max) { return Math.floor(Math.random() * (max - min) + min); } var player = { x: 0, y: 300, width: 30, jumping: false, //标识是否跳跃中 power: 0, //标识蓄力程度,决定跳多远 speedY: 10 //标识Y轴上的速度,模拟重力加速度 } var gameover = false; var cameraMoving = false; //镜头是否移动中 var timerPowerUp; //蓄力操作用的计时器 var platforms = []; //存储所有生成的平台 //初始化平台 platforms.push({ x: 0, width: 100 }, { x: 200, width: 100 }); var lastPlatform = platforms[platforms.length - 1]; //最新生成的平台 var currentPlatform = platforms[0]; //当前落脚的平台 var drawPlayer = function() { context.fillStyle = "blue"; context.save(); context.fillRect(player.x, player.y, player.width, player.width); context.restore(); } var drawPlatform = function() { context.fillStyle = "#A3A3A3"; context.save(); platforms.forEach(function(platform) { context.fillRect(platform.x, 300 + player.width, platform.width, 300); }); context.restore(); } var drawTextFailed = function() { if (gameover) { context.fillStyle = "#000"; context.save(); context.font = "normal 30px sans-serif"; context.textBaseline = 'top'; var text = "FAILED"; context.fillText(text, canvas.clientWidth / 2 - text.length * 30 / 2, 300); context.restore(); } } //玩家跳跃中 var playerJumping = function() { if (player.jumping) { player.y -= player.speedY; player.speedY -= 0.7; player.x += player.power; if (player.y >= 300) { player.jumping = false; player.y = 300; player.power = 0; if (!iscurrentplatform()) { if (isfailed()) { gameover = true; } else { currentPlatform = lastPlatform; generateNextPlatform(); cameraMoving = true; } } } } } //生成下一个平台 var generateNextPlatform = function() { var x = Math.randomRange(lastPlatform.x + 100, 500); var width = Math.randomRange(player.width, 100); lastPlatform = { x: x, width: width } platforms.push(lastPlatform); } //判断玩家位置是否在当前平台 var iscurrentplatform = function() { return player.x > currentPlatform.x - player.width && player.x < currentPlatform.x + currentPlatform.width; } //判断是否失败 var isfailed = function() { return player.x <= (lastPlatform.x - player.width) || player.x >= (lastPlatform.x + lastPlatform.width); } //移动镜头(场景整体移动) var moveCamera = function() { if (cameraMoving) { player.x -= 10; platforms.forEach(function(platform) { platform.x -= 10; }); if (player.x <= 0 || currentPlatform.x <= 0) { cameraMoving = false; if (player.x < 0) { player.x = 0; } else if (currentPlatform.x < 0) { currentPlatform.x = 0; } } } } //蓄力中 var PowerUping = function() { player.power += 1; } //开始蓄力 var PowerUp = function() { if (!gameover && !player.jumping) { timerPowerUp = setInterval(PowerUping, 100); } } //停止蓄力 var PowerUpStop = function() { if (!gameover && !player.jumping) { clearInterval(timerPowerUp); player.jumping = true; player.speedY = 10; } } //帧刷新时执行的其他操作 var withFrameUpdate = function() { playerJumping(); moveCamera(); } //帧绘制 var drawFrame = function() { window.requestAnimationFrame(drawFrame, canvas); context.clearRect(0, 0, canvas.width, canvas.height); drawPlayer(); drawPlatform(); drawTextFailed(); withFrameUpdate(); } document.addEventListener("mousedown", PowerUp); document.addEventListener("mouseup", PowerUpStop); drawFrame();
↑上面代码改变,会自动显示代码结果
jQuery调用版本:
1.11.3
<!doctype html><html><head><meta charset="utf-8"><title>2D版跳一跳小游戏代码-www.sucainiu.com</title><script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script><style>
</style></head><body>
<script>
</script>
</body></html>
立即下载
收藏
积分说明:注册即送10牛币,每日签到可获得5牛币,成为VIP会员可永久免牛币下载!
充值积分
充值会员
更多说明»
代码描述:2D版跳一跳小游戏
获取、判断浏览器类型
点击TAB切换
讨论这个常用代码(1)
回答他人问题或分享心得会奖励牛币
Lv10 CTO
华***n
2018年01月24日
O(∩_∩)O哈哈~
回复
文明上网,理性发言! 😉 阿里云幸运券,
戳我领取
发表评论
提交回复
我的积分余额:
0
已下载次数:
1105
所需牛币:
5
开始下载
牛币获取:
签到、评论、充值
» 在线充值
(10牛币=1元)
成为VIP可永久免积分下载全部常用代码
» 查看详情
VIP
客服
签到
充值