-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
220 lines (198 loc) · 4.04 KB
/
game.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 1212;
canvas.height = 480;
document.body.appendChild(canvas);
//Background Image
var bgimage = false;
var roadimage = new Image();
roadimage.onload = function(){
bgimage = true;
};
roadimage.src = "images/road2.jpg";
//Hidden Bgimage
var bgimage2 = false;
var road2image = new Image();
road2image.onload = function(){
bgimage2 = true;
};
road2image.src = "images/road2.jpg";
//Car Image
var carReady = false;
var carimage = new Image();
carimage.onload = function(){
carReady = true;
};
carimage.src = "images/car.png";
//defend1 Image
var defend1Ready = false;
var defend1image = new Image();
defend1image.onload = function(){
defend1Ready = true;
};
defend1image.src = "images/defend1.jpg";
//defend2 Image
var defend2Ready = false;
var defend2image = new Image();
defend2image.onload = function(){
defend2Ready = true;
};
defend2image.src = "images/defend1.jpg";
//down keys
var keydown = {};
//add key
addEventListener("keydown", function (e) {
keydown[e.keyCode] = true;
}, false);
//remove key
addEventListener("keyup", function (e) {
delete keydown[e.keyCode];
defend1.speed = 256;
defend2.speed = 256;
}, false);
//road object
var road = {
speed : 256
}
//road2 object
var road2 = {
speed : 256
}
//car object
var car = {
speed : 500
};
//defend1 object
var defend1 = {
speed : 256
}
//defend2 object
var defend2 = {
speed : 256
}
//score
var score = 0;
//reset function
var reset = function(){
car.x = 80;
car.y = 280;
defend1.x = 1000;
defend1.y = 280;
defend2.x = 1500;
defend2.y = 150;
road.x = 0;
road.y = 0;
road2.x = canvas.width;
road2.y = 0;
score = 0;
real_score = 0;
defend1.speed = 256;
defend2.speed = 256;
};
//render function
var render = function(){
if(bgimage){
ctx.drawImage(roadimage,road.x,road.y,canvas.width,canvas.height);
}
if(bgimage2){
ctx.drawImage(road2image,road2.x,road2.y,canvas.width,canvas.height);
}
if(carReady){
ctx.drawImage(carimage,car.x,car.y,100,50);
}
if(defend1Ready){
ctx.drawImage(defend1image,defend1.x,defend1.y,100,50);
}
if(defend2Ready){
ctx.drawImage(defend2image,defend2.x,defend2.y,100,50);
}
//display score
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Score : " + real_score, 32, 32);
};
//update function
var update = function(mod){
score = score + 1;
real_score = Math.round(score/200);
if(37 in keydown && 39 in keydown){
road.x = road.x;
defend1.speed = 256;
defend2.speed = 256;
}
else{
if(37 in keydown){
defend1.speed = 256;
defend2.speed = 256;
if(car.x <= 80){
car.x = 80
}
else{
car.x -= car.speed*mod;
}
}
if(39 in keydown){
if(car.x >= 1050){
car.x = 1050;
}
else{
car.x += car.speed*mod;
}
if(road2.x <= -canvas.width){
road2.x = canvas.width;
}
if(road.x <= -canvas.width){
road.x = canvas.width;
}
road.x -= road.speed*mod;
road2.x -= road2.speed*mod;
defend1.speed = 512;
defend2.speed = 512;
}
}
if(38 in keydown){
car.y = 150;
}
if(40 in keydown){
car.y = 280;
}
if(defend1.x <= 0){
defend1.x = canvas.width;
}
if(defend2.x <= 0){
defend2.x = canvas.width;
}
if(real_score >= 2){
defend1.speed = 500;
defend2.speed = 500;
}
if(real_score >= 4){
defend1.speed = 800;
defend2.speed = 800;
}
defend1.x -= defend1.speed*mod;
defend2.x -= defend2.speed*mod;
if(defend1.x <= car.x + 100 && car.x - defend1.x <=100 && car.y == defend1.y){
reset();
}
if(defend2.x <= car.x + 100 && car.x - defend2.x <=100 && car.y == defend2.y){
reset();
}
};
var main = function () {
var now = Date.now();
var delta = now - then;
update(delta / 1000);
render();
then = now;
requestAnimationFrame(main);
};
// Cross-browser support for requestAnimationFrame
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;
// Let's play this game!
var then = Date.now();
reset();
main();