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将图片切成相应大小的图 ...
随机推荐
- 【转】修复关于apache-xampp的问题:Port 443 in use by “vmware-hostd.exe”!
在电脑里装了VMware后,再要装xampp,十有八九就会出现这个问题: 11:23:37 [Apache] Problem detected! 11:23:37 [Apache] ...
- kafka集群压力测试--基础。
1.生产者测试 kafka-producer-perf-test.bat --num-records 1000000 --topic test --record-size 200 --throughp ...
- 第十章 优先级队列 (c)堆排序
- Hudson 打包部署到Was上特别慢
一.找问题点 1.打包很快,到部署很慢 2.部署到其他was一样很慢 二.解决 经过寻找,网上找出以下一段话: 问题出在web.xml,web.xml中的版本信息不对,要根据你的servlet版本和运 ...
- c# tcp协议发送数据
private void tcp_send(string data)//tcp协议转发数据 { TcpClient tcpClient = new TcpClient(); tcpClient.Con ...
- 伪异步IO
针对传统的BIO编程,当客户端数量一直增加的情况下,可能会导致服务器直接奔溃掉,进而出现了一种伪异步IO的线程方式. 先看一下代码: 看一下server端的代码: 其中使用了自定义的一个线程池Hand ...
- 支付宝小程序开发之与微信小程序不同的地方
前言: 本文仅汇总微信小程序移植支付宝小程序过程中遇到的一些不同的地方,详细请参考官方开发文档. 网络请求: 对于网络请求,基本上改动不大,也就支付宝小程序没有responseType属性及响应码字段 ...
- TZOJ 1937 Hie with the Pie(floyd+状压dp)
描述 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...
- Intellij创建简单Springboot项目
Intellij创建简单Springboot项目 第一步:选择创建新项目——file-new-project 第二步:选择项目类型——Spring Initializr-next 第三步:输入项目信息 ...
- auth 认证模块
. auth认证模块: http://www.cnblogs.com/liwenzhou/p/9030211.html auth模块的知识点: . 创建超级用户 python manage.py cr ...