-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcar.html
64 lines (55 loc) · 1.27 KB
/
car.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE HTML>
<html lang = "en">
<head>
<title>car using simpleGame engine</title>
<meta charset = "UTF-8" />
<style type = "text/css">
</style>
<script type = "text/javascript"
src = "simpleGame.js"></script>
<script type = "text/javascript">
//<![CDATA[
var canvas;
var context;
var car;
function init(){
//initialize variables
canvas = document.getElementById("game");
context = canvas.getContext("2d");
//set up car
car = new Sprite(canvas, "car.gif", 50, 30);
car.setPosition(100, 100);
car.setSpeed(0);
car.setAngle(20);
onkeydown = checkKeys;
setInterval(update, 100);
} // end init
function update(){
context.clearRect(0, 0, 200, 200);
car.update();
//car.report();
} // end update
function checkKeys(e){
if (e.keyCode == 37){
//left
car.changeAngleBy(-5);
} else if (e.keyCode == 39){
//right
car.changeAngleBy(5);
} else if (e.keyCode == 38){
//up
car.changeSpeedBy(1);
} else if (e.keyCode == 40){
car.changeSpeedBy(-1);
} // end if
} // end checkKeys
//]]>
</script>
</head>
<body onload = "init()">
<h1>car game with simpleGame</h1>
<canvas id = "game"
height = "200"
width = "200"></canvas>
</body>
</html>