HTML5 Canvas学习笔记(5)游戏得分动画

效果图:
[img]http://dl2.iteye.com/upload/attachment/0098/5168/e0519189-e6c2-35a7-88db-cc0fc074af20.gif[/img]

点击查看效果:
[url]http://www.108js.com/article/canvas/5/exam.html[/url]

欢迎访问博主的网站:[url]http://www.108js.com[/url]

代码:
<!doctype html>
<html>
<head>
<title>
游戏得分动画
</title>
</head>
<canvas id = "canvas" style = "background-color: black;" width="800" height="650"></canvas>
<script>
var requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 60);
};
})();

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var time = 500;
var score1 = 0;
var score2 = 0;
var numHit=30;
var lose=true;

function update() {
ctx.clearRect(0,0,canvas.width,canvas.height);
if (lose) {
ctx.font = "50px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText("YOU LOSE",canvas.width / 2,canvas.height / 2);
ctx.font = "25px Verdana";
ctx.fillText("Balls hit : " + numHit,canvas.width / 2,canvas.height / 2 + 200);
ctx.fillText("Time : " + time,canvas.width / 2,canvas.height / 2 + 250);
score1 = (time + time * 0.75) + (numHit * 1000);
if (score2 < score1) {
score2 += 5;
}
ctx.fillText("Score : " + score2, canvas.width / 2, canvas.height / 2 + 300);
}
requestAnimFrame(update);
}
update();
</script>
</html>
下载源码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值