This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.html
251 lines (208 loc) · 4.81 KB
/
App.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<html>
<meta name="apple-mobile-web-app-capable"
content="yes" />
<link rel="ios.app.png"
href="ios.app.png" />
<head>
<title>run cube run</title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
</head>
<body>
<FORM>
<input align="top" type="button" value="Enter cheat code" onClick="passWord()">
</FORM>
<style>
canvas {
border: 1px solid white;
width: 100%;
}
* {
-webkit-user-select: none; /*disable text selection */
}
</style>
<script>
window.addEventListener("load",function() {
//constants
var GAME_WIDTH = 640;
var GAME_HEIGHT = 360;
//keep the game going
var gameLive = true;
//current level
var level = 1;
var life = 100;
//random color
var color = "#"+((1<<24)*Math.random()|0).toString(16);
//enemies
var enemies = [
{
x: 100, //x coordinate
y: 100, //y coordinate
speedY: 2, //speed in Y
w: 40, //width
h: 40 //heght
},
{
x: 200,
y: 0,
speedY: 2,
w: 40,
h: 40
},
{
x: 330,
y: 100,
speedY: 3,
w: 40,
h: 40
},
{
x: 450,
y: 100,
speedY: -3,
w: 40,
h: 40
}
];
//the player object
var player = {
x: 10,
y: 160,
speedX: 2,
isMoving: false, //keep track whether the player is moving or not
w: 40,
h: 40
};
//the goal object
var goal = {
x: 580,
y: 160,
w: 50,
h: 36
}
var sprites = {};
var movePlayer = function() {
player.isMoving = true;
}
var stopPlayer = function() {
player.isMoving = false;
}
//grab the canvas and context
var canvas = document.getElementById("mycanvas");
var ctx = canvas.getContext("2d");
//event listeners to move player
canvas.addEventListener('mousedown', movePlayer);
canvas.addEventListener('mouseup', stopPlayer);
canvas.addEventListener('touchstart', movePlayer);
canvas.addEventListener('touchend', stopPlayer);
//update the logic
var update = function() {
//check if you've won the game
if(checkCollision(player, goal)) {
alert('Win !');
level += 1;
life += 1;
player.speedX += 1;
player.x = 10;
player.y = 160;
player.isMoving = false;
for(var ab = 0; ab < enemies.length; ab++){
if(enemies[ab].speedY > 1){
enemies[ab].speedY += 1 ;
}
else{
enemies[ab].speedY -= 1 ;
}
}
}
//update player
if(player.isMoving) {
player.x = player.x + player.speedX;
}
//update enemies
var i = 0;
var n = enemies.length;
enemies.forEach(function(element, index){
//check for collision with player
if(checkCollision(player, element)) {
//stop the game
if(life === 0){
alert('Game Over');
for(var ab = 0; ab < enemies.length; ab++){
if(enemies[ab].speedY > 1){
enemies[ab].speedY -= (level - 1) ;
}
else{
enemies[ab].speedY += (level - 1) ;
}
}
level = 1;
life = 6;
player.speedX = 2;
color ="#"+((1<<24)*Math.random()|0).toString(16);
}
if(life > 0){
life -= 1 ;
color ="#"+((1<<24)*Math.random()|0).toString(16);
}
player.x = 10;
player.y = 160;
player.isMoving = false;
}
//move enemy
element.y += element.speedY;
//check borders
if(element.y <= 10) {
element.y = 10;
//element.speedY = element.speedY * -1;
element.speedY *= -1;
}
else if(element.y >= GAME_HEIGHT - 50) {
element.y = GAME_HEIGHT - 50;
element.speedY *= -1;
}
});
};
//show the game on the screen
var draw = function() {
//clear the canvas
ctx.clearRect(0,0,GAME_WIDTH,GAME_HEIGHT);
//draw level
ctx.font = "15px Verdana";
ctx.fillStyle = "rgb(0,0,0)";
ctx.fillText("Level : " + level , 10, 15);
ctx.fillText("Life : " + life , 10, 35);
ctx.fillText("Speed : " + player.speedX , 10, 55);
//draw player with random color
ctx.fillStyle = color;
ctx.fillRect(player.x, player.y, player.w, player.h);
//draw enemies
ctx.fillStyle = "rgb(255,120,70)";
enemies.forEach(function(element, index){
ctx.fillRect(element.x, element.y, element.w, element.h);
});
//draw goal
ctx.fillStyle = "rgb(0,255,120)";
ctx.fillRect(goal.x, goal.y, goal.w, goal.h);
ctx.fillStyle = "rgb(0,0,0)";
ctx.fillText("Goal", goal.x + 7, goal.y + 25);
};
//gets executed multiple times per second
var step = function() {
update();
draw();
if(gameLive) {
window.requestAnimationFrame(step);
}
};
//check the collision between two rectangles
var checkCollision = function(rect1, rect2) {
var closeOnWidth = Math.abs(rect1.x - rect2.x) <= Math.max(rect1.w, rect2.w);
var closeOnHeight = Math.abs(rect1.y - rect2.y) <= Math.max(rect1.h, rect2.h);
return closeOnWidth && closeOnHeight;
}
//initial kick
step();
});
</script>
</body>
</html>