一:样式

<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 实现赛车小游戏的更多相关文章

  1. java 图形化小工具Abstract Window Toolit :画笔Graphics,画布Canvas(),弹球小游戏

    画笔Graphics Java中提供了Graphics类,他是一个抽象的画笔,可以在Canvas组件(画布)上绘制丰富多彩的几何图和位图. Graphics常用的画图方法如下: drawLine(): ...

  2. Canvas进阶——制作小游戏【贪吃蛇】

    今天呢,主要和小伙伴们分享一下一个贪吃蛇游戏从构思到实现的过程~因为我不是很喜欢直接PO代码,所以只copy代码的童鞋们请出门左转不谢. 按理说canvas与其应用是老生常谈了,可我在准备阶段却搜索不 ...

  3. canvas 实现微信小游戏

    var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); var timer; var iS ...

  4. [Canvas]首个小游戏告成

    英雄在地图上射箭杀怪兽,杀完了就胜利了. 点此下载程序试玩. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> < ...

  5. 【JavaScript】canvas实现一个小游戏

    参考: 1.image onload事件:http://www.runoob.com/jsref/event-img-onload.html(赞) 2.canvas的drawImage无法显示图像:h ...

  6. 如何开发一个简单的HTML5 Canvas 小游戏

    原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...

  7. html5+Canvas实现酷炫的小游戏

    最近除了做业务,也在尝试学习h5和移动端,在这个过程中,学到了很多,利用h5和canvas做了一个爱心鱼的小游戏.点这里去玩一下 PS: 貌似有点闪屏,亲测多刷新两下就好了==.代码在本地跑都不会闪, ...

  8. Canvas小游戏里,删除过期或者死亡元素技巧

    在许多canvas游戏,canvas效果中,经常会有过期的元素需要删除 例如现在需要制作一个笨鸟先飞(flappy bird)小游戏,游戏中障碍物(且称为柱子),此时会有一个全局变量保存所有柱子的实例 ...

  9. canvas drag 实现拖拽拼图小游戏

    博主一直心心念念想做一个小游戏-  前端时间终于做了一个小游戏,直到现在才来总结,哈哈- 以后要勤奋点更新博客! 实现原理 1.如何切图? 用之前的方法就是使用photoshop将图片切成相应大小的图 ...

随机推荐

  1. jggrid应用,后台c#

    参考网址: 1.https://www.cnblogs.com/miro/p/jqGrid.html 2.https://blog.csdn.net/ainuser/article/details/6 ...

  2. Git之清除已保存的账户

    Git会自动保存输入过的用户名.密码. Git的配置文件是-/.gitconfig.可在windows下的GIt Bash.Mac的命令行中,用vim ~/.gitconfig打开. Windows ...

  3. queue模拟

    BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面两个简单的方法put和take. put(anOb ...

  4. POJ 2230 Watchcow(有向图欧拉回路)

    Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...

  5. f5创建VS

    1.1) 2) 3) 4) 5) 2.Availability 1)Pool 中的monitor保障服务高可用 2)Pool 失败机制一 Fallback Host 最后的host( 使用于HTTP ...

  6. ajax中的contendType和dataType知识点梳理

    在ajax中有2个参数比较重要,之前一直没有搞清楚,下面我们开始梳理一下 1.contentType字段:这个字段的意思,ajax发送给后端的数据是什么类型 如果在ajax中不指定这个属性,则默认是u ...

  7. 自学之jQuery

    最近在公司做项目的过程中,涉及到写前端部分,因为之前一直很少写前端,所以不是很熟悉,所以,在此写一下自学jQuery的过程,以及中途遇到的坑. 首先,要想使用jQuery必须先引入jQuery < ...

  8. swift - 百度地图API集成

    1.百度搜索  百度地图api 2. 选中之后选择, 看功能需求下载 API 3. 下载的API拖入项目,此处有坑, 如果只用地图或者 定位,这中写着 是 导航的SDK 别拖进去, 不然报错 怕搞错到 ...

  9. windows核心编程

    第一章 函数返回值: void:不可能失败.极少数会返回VOID BOOL:失败返回0 HANDLE:失败会返回NULL 或INVALID_HANDLE_VALUE PVOID:失败返回NULL wa ...

  10. Windows服务安装、卸载、启动和关闭的管理器

    最近在重构公司的系统,把一些需要独立执行.并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了.但是每次都要建立Windows服务项目 ...