forked from jainpawan21/Racing_Car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
199 lines (163 loc) · 5.78 KB
/
script.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
/* --------------------------- By: Pawan Jain -------------------------------- */
$(function() {
var anim_id;
//saving dom objects to variables
var container = $('#container');
var car = $('#car');
var car_1 = $('#car_1');
var car_2 = $('#car_2');
var car_3 = $('#car_3');
var line_1 = $('#line_1');
var line_2 = $('#line_2');
var line_3 = $('#line_3');
var restart_div = $('#restart_div');
var restart_btn = $('#restart');
var score = $('#score');
// var highscore = $('#highscore');
//saving some initial setup
var container_left = parseInt(container.css('left'));
var container_width = parseInt(container.width());
var container_height = parseInt(container.height());
var car_width = parseInt(car.width());
var car_height = parseInt(car.height());
//some other declarations
var game_over = false;
var score_counter = 1;
var speed = 2;
var line_speed = 5;
var move_right = false;
var move_left = false;
var move_up = false;
var move_down = false;
/* ------------------------------GAME CODE STARTS HERE------------------------------------------- */
/* Move the cars */
$(document).on('keydown', function(e) {
if (game_over === false) {
var key = e.keyCode;
if (key === 37 && move_left === false) {
move_left = requestAnimationFrame(left);
} else if (key === 39 && move_right === false) {
move_right = requestAnimationFrame(right);
} else if (key === 38 && move_up === false) {
move_up = requestAnimationFrame(up);
} else if (key === 40 && move_down === false) {
move_down = requestAnimationFrame(down);
}
}
});
$(document).on('keyup', function(e) {
if (game_over === false) {
var key = e.keyCode;
if (key === 37) {
cancelAnimationFrame(move_left);
move_left = false;
} else if (key === 39) {
cancelAnimationFrame(move_right);
move_right = false;
} else if (key === 38) {
cancelAnimationFrame(move_up);
move_up = false;
} else if (key === 40) {
cancelAnimationFrame(move_down);
move_down = false;
}
}
});
function left() {
if (game_over === false && parseInt(car.css('left')) > 0) {
car.css('left', parseInt(car.css('left')) - 5);
move_left = requestAnimationFrame(left);
}
}
function right() {
if (game_over === false && parseInt(car.css('left')) < container_width - car_width) {
car.css('left', parseInt(car.css('left')) + 5);
move_right = requestAnimationFrame(right);
}
}
function up() {
if (game_over === false && parseInt(car.css('top')) > 0) {
car.css('top', parseInt(car.css('top')) - 3);
move_up = requestAnimationFrame(up);
}
}
function down() {
if (game_over === false && parseInt(car.css('top')) < container_height - car_height) {
car.css('top', parseInt(car.css('top')) + 3);
move_down = requestAnimationFrame(down);
}
}
/* Move the cars and lines */
anim_id = requestAnimationFrame(repeat);
function repeat() {
if (collision(car, car_1) || collision(car, car_2) || collision(car, car_3)) {
stop_the_game();
return;
}
score_counter++;
if (score_counter % 20 == 0) {
score.text(parseInt(score.text()) + 1);
}
if (score_counter % 500 == 0) {
speed = speed + 1.5;
line_speed++;
}
car_down(car_1);
car_down(car_2);
car_down(car_3);
line_down(line_1);
line_down(line_2);
line_down(line_3);
anim_id = requestAnimationFrame(repeat);
}
function car_down(car) {
var car_current_top = parseInt(car.css('top'));
if (car_current_top > container_height) {
car_current_top = -200;
var car_left = parseInt(Math.random() * (container_width - car_width));
car.css('left', car_left);
}
car.css('top', car_current_top + speed);
}
function line_down(line) {
var line_current_top = parseInt(line.css('top'));
if (line_current_top > container_height) {
line_current_top = -300;
}
line.css('top', line_current_top + line_speed);
}
restart_btn.click(function() {
location.reload();
});
function stop_the_game() {
// if(parseInt(highscore.text) < parseInt(score.text)) // not working
// {
// highscore.text(parseInt(score.text));
// }
game_over = true;
cancelAnimationFrame(anim_id);
cancelAnimationFrame(move_right);
cancelAnimationFrame(move_left);
cancelAnimationFrame(move_up);
cancelAnimationFrame(move_down);
restart_div.slideDown();
restart_btn.focus();
}
/* ------------------------------GAME CODE ENDS HERE------------------------------------------- */
function collision($div1, $div2) {
var x1 = $div1.offset().left;
var y1 = $div1.offset().top;
var h1 = $div1.outerHeight(true);
var w1 = $div1.outerWidth(true);
var b1 = y1 + h1;
var r1 = x1 + w1;
var x2 = $div2.offset().left;
var y2 = $div2.offset().top;
var h2 = $div2.outerHeight(true);
var w2 = $div2.outerWidth(true);
var b2 = y2 + h2;
var r2 = x2 + w2;
if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
return true;
}
});