HTML5+JS Example-handheld game racing-with source code

Source: Internet
Author: User

Game interface, did not do anything to beautify.

Game rules: The game interface is divided into three columns, the black box is randomly set down, red squares can be moved freely in three columns (with the arrow keys, long press the direction key black box accelerated decline). The Red Square hits the black square to lose.

Score: Every normal through a black square plus 12 points, accelerated by adding 30 points.

The following direct code:

Html:

The code is as follows Copy Code
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<style>
body{
Text-align:center;
}
#mycar {
BORDER:1PX solid black;
}
</style>
<body>
<canvas id= "Mycar" width= "300px" height= "500px" ></canvas>
<div id= "scored" > Score:0</div>
<script src= "Js/mycar.js" ></script>
</body>



JS Code

The code is as follows Copy Code
function Createcar (speed,cxt,dom) {
var o = new Object ();
O.speed = speed;
O.CXT = CXT;
O.cell = 100;
O.curdir = {' x ': $, ' Y ': 400};
O.hinder = [[],[],[]];
O.scroll = 0;
o.scored = 0;
O.init = function () {
O.cxt.fillstyle = ' red ';
O.cxt.fillrect (o.curdir.x, O.curdir.y, O.cell, O.cell);
Document.onkeydown = function (e) {
if (E.keycode = = Panax Notoginseng && o.curdir.x > 0) {
O.movecar (' left ');
}
else if (E.keycode = = && O.curdir.x < 200) {
O.movecar (' right ');
}
else if (E.keycode = 40)
O.speed = SPEED/3;
};
Document.onkeyup = function () {
O.speed = speed;
};
O.sethinder ();
O.downhinder ();
};
O.sethinder = function () {
var rand1 = Math.ceil (Math.random () * 1000)% 2,
Rand2 = Math.ceil (Math.random () * 1000)% 2,
Rand3 = Math.ceil (Math.random () * 1000)% 2;
O.hinder[0].push ({' X ': 0, ' y ': 0, ' Hinder ': Rand1});
O.hinder[1].push ({' x ': +, ' y ': 0, ' Hinder ': Rand2});
O.hinder[2].push ({' X ':, ' y ': 0, ' hinder ': rand1 + rand2 = = 2?0:rand3});
for (var i = 0; i < o.hinder.length i + +) {
var last =o.hinder[i][o.hinder[i].length-1];
if (Last.hinder = = 1) {
O.cxt.fillstyle = ' black ';
O.cxt.fillrect (Last.x,last.y, O.cell, O.cell);
}
}
};
O.downhinder = function () {
settimeout (function () {
var i = 0,
j = 0,
cur = null,
old = null;
for (i = 0; i < o.hinder[0].length i + +) {
for (j = 0; J < 3; J + +) {
cur = o.hinder[j][i];
if (Cur.hinder = = 1) {
Old = O.hinder[j][i];
O.cxt.clearrect (Old.x,old.y, O.cell, O.cell);
O.HINDER[J][I].Y = o.hinder[j][i].y + 5;
cur = o.hinder[j][i];
O.cxt.fillstyle = ' black ';
O.cxt.fillrect (Cur.x,cur.y, O.cell, O.cell);
}
Else
O.HINDER[J][I].Y = o.hinder[j][i].y + 5;
}
}
for (i = 0; i < o.hinder.length i + +) {
if (o.hinder[i][0].y >= 500) {
o.scored = o.scored + Math.ceil (100/o.speed);
dom.innerhtml = ' score: ' + o.scored;
var over = O.hinder[i].shift ();
if (Over.hinder = = 1)
O.cxt.clearrect (Over.x,over.y, O.cell, O.cell);
}
}
if (O.hinder[o.curdir.x/100][0].hinder = = 1 && o.hinder[o.curdir.x/100][0].y + >= o.curdir.y) {
Alert (' You hang up ');
Return
}
O.scroll = O.scroll + 5;
if (o.scroll% 300 = 0)
O.sethinder ();
O.downhinder ();
}, O.speed);
};
O.movecar = function (dir) {
O.cxt.fillstyle = ' red ';
O.cxt.clearrect (o.curdir.x, O.curdir.y, O.cell, O.cell);
O.curdir.x = (dir = = ' Left ' o.curdir.x-o.cell:o.curdir.x + O.cell);
O.cxt.fillrect (O.curdir.x,o.curdir.y, O.cell, O.cell);
};
return o;
}

var c = document.getElementById (' Mycar ');
var scored = document.getElementById (' scored ');
var cxt = C.getcontext (' 2d ');
var car = Createcar (30,cxt,scored);
Car.init ();



The algorithm wrote a bit bad, there is a great God passed by to write guidance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.