canvas 实现赛车小游戏
一:样式
<style>
#btn{
width: 60px;
height: 30px;
line-height: 30px;
background: #7EC0EE;
border: 2px solid #8A2BE2;
font-size: 20px;
text-align: center;
font-weight: bold;
border-radius: 5px;
box-shadow: 0 2px 2px #8A2BE2;
display: block;
position: absolute;
top:30px;
left: 140px;
cursor: pointer;
}
#sd{
position: absolute;
top:30px;
left: 30px;
border: 2px solid #8A2BE2;
width: 100px;
height: 30px;
line-height: 30px;
border-radius: 5px;
font-size: 20px;
text-align: center;
}
</style>
二: 画布
<canvas width="200" height="500" id="canvas" style="border:10px solid #A2CD5A;"></canvas>
<input type="text" placeholder="难度(1-5)" id="sd">
<div id="btn">开始</div>
三:实现
var canvas = document.getElementById('canvas');
var cxt = canvas.getContext('2d');
// 定时器
var timer;
// 游戏是否结束
var iStop;
// 赛车
var car;
// 障碍物
var blocks;
// 障碍物速度
var speed;
// 清除画布
function erase(){
cxt.clearRect(0, 0, canvas.width, canvas.height);
}
function init(){
iStop = false;
// 赛车
car = {'x':0,'y':450,'width':50, 'height':50};
// 障碍物
blocks = [
{'x':-0,'y':-50,'width':50, 'height':50},
{'x':50,'y':-50,'width':50, 'height':50},
{'x':100,'y':-50,'width':50, 'height':50},
{'x':0,'y':250,'width':50, 'height':50},
{'x':50,'y':250,'width':50, 'height':50},
{'x':150,'y':250,'width':50, 'height':50}
];
speed = 2;
}
// 绘图
function draw(){
cxt.save();
cxt.fillRect(car.x, car.y, car.width, car.height);
for(var i=0;i<blocks.length;i++){
cxt.fillRect(blocks[i].x, blocks[i].y, blocks[i].width, blocks[i].height);
if(blocks[i].y > 400 && blocks[i].x == car.x){
iStop = true;
}
}
cxt.restore();
}
// 障碍物前进
function step(){
var _blocks = [];
for(var i=0;i<blocks.length;i++){
blocks[i].y += speed;
if(blocks[i].y < 500){
_blocks.push(blocks[i]);
}
}
if(_blocks.length == 3){
var out = Math.round(Math.random()*3);
for(var j=0; j<4; j++){
if(j != out){
_blocks.push({'x':(50*j),'y':-50,'width':50, 'height':50});
}
}
}
blocks = _blocks;
}
function drawOver() {
cxt.save();
cxt.font="20px Verdana";
cxt.fillStyle = 'yellow';
cxt.fillText('游戏结束!', 75, 200);
cxt.restore();
}
// 键盘控制赛车左右(<-、->)运动
var last = new Date();
document.onkeydown = (function(e){
var now = new Date();
if(now.getTime() - last.getTime() < 100){
return;
}
last = now;
switch(e.which){
case 39:
if(car.x < 150){
car.x += 50;
}
break;
case 37:
if(car.x > 0){
car.x -= 50;
}
break;
}
});
window.requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
window.cancelRequestAnimationFrame =
window.cancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame;
function animate() {
erase();
draw();
step();
if(iStop){
cancelRequestAnimationFrame(timer);
drawOver();
}else{
timer = requestAnimationFrame(animate);
}
}
//animate();
document.querySelector('#btn').onclick = function(){
if(this.innerHTML == '开始'){
init();
var s = document.querySelector('#sd').value;
if(s != ''){
speed = parseInt(s);
}
animate();
this.innerHTML = '结束';
}else{
cancelRequestAnimationFrame(timer);
this.innerHTML = '开始';
}
}
canvas 实现赛车小游戏的更多相关文章
- java 图形化小工具Abstract Window Toolit :画笔Graphics,画布Canvas(),弹球小游戏
画笔Graphics Java中提供了Graphics类,他是一个抽象的画笔,可以在Canvas组件(画布)上绘制丰富多彩的几何图和位图. Graphics常用的画图方法如下: drawLine(): ...
- Canvas进阶——制作小游戏【贪吃蛇】
今天呢,主要和小伙伴们分享一下一个贪吃蛇游戏从构思到实现的过程~因为我不是很喜欢直接PO代码,所以只copy代码的童鞋们请出门左转不谢. 按理说canvas与其应用是老生常谈了,可我在准备阶段却搜索不 ...
- canvas 实现微信小游戏
var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); var timer; var iS ...
- [Canvas]首个小游戏告成
英雄在地图上射箭杀怪兽,杀完了就胜利了. 点此下载程序试玩. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> < ...
- 【JavaScript】canvas实现一个小游戏
参考: 1.image onload事件:http://www.runoob.com/jsref/event-img-onload.html(赞) 2.canvas的drawImage无法显示图像:h ...
- 如何开发一个简单的HTML5 Canvas 小游戏
原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...
- html5+Canvas实现酷炫的小游戏
最近除了做业务,也在尝试学习h5和移动端,在这个过程中,学到了很多,利用h5和canvas做了一个爱心鱼的小游戏.点这里去玩一下 PS: 貌似有点闪屏,亲测多刷新两下就好了==.代码在本地跑都不会闪, ...
- Canvas小游戏里,删除过期或者死亡元素技巧
在许多canvas游戏,canvas效果中,经常会有过期的元素需要删除 例如现在需要制作一个笨鸟先飞(flappy bird)小游戏,游戏中障碍物(且称为柱子),此时会有一个全局变量保存所有柱子的实例 ...
- canvas drag 实现拖拽拼图小游戏
博主一直心心念念想做一个小游戏- 前端时间终于做了一个小游戏,直到现在才来总结,哈哈- 以后要勤奋点更新博客! 实现原理 1.如何切图? 用之前的方法就是使用photoshop将图片切成相应大小的图 ...
随机推荐
- C# winform 支持html5的 控件
OpenWebKitSharp WebKit.net c#winform中使用WebKit传递js对象实现与网页交互 分类: .NET开发2013-08-18 23:55 2496人阅读 评论(1) ...
- STL-stack和顺序栈实现括号匹配
2018-11-11-14:28:31 1.顺序栈 下面是我用数组实现的顺序栈,包含的函数有出入栈,查看栈顶元素,栈的大小,栈是否空等函数,当栈空间不够用时,对应的数组会自动增长. /******** ...
- Camera插件推荐,解锁电影大师级视角控制
相机在游戏中的重要性是不言而喻的,尤其是一些MMORPG或FPS等类型的游戏,相机不仅需要跟随游戏主角进行移动,可能还要随时准备切换焦点,这就要求开发者将游戏相机管理得井井有条,能顺应游戏中可能瞬息发 ...
- JavaScript各种继承方式(六):寄生组合继承
一 原理 用寄生继承来改造组合继承. function Fruit(name){ this.name = name; } Fruit.prototype.eat = function(){ conso ...
- UVa 10129 Play on Words(有向图欧拉路径)
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to s ...
- f5售后查询
登录: https://secure.f5.com/validate/validate.jsp http://boochem.blog.51cto.com/628505/633907
- 项目总结09:select标签下封装option标签
项目中经常用到Select标签,用封装好的方法获取option,可以避免冗赘的代码: 1.JSP--标签 <select class="width_md" name=&quo ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- OnActionExecuting验证用户登录
代码 using Common; using Service; using Service.IService; using System; using System.Collections.Gener ...
- 搭建FTP服务器 window7
1.安装IIS组件,打开控制面板-->程序和功能,点击打开或关闭windows功能 找到Internet信息服务,勾选FTP服务器和Web管理工具下的IIS管理控制台进行安装ftp,如图所示 2 ...