-
Notifications
You must be signed in to change notification settings - Fork 0
/
EndRunR.html
251 lines (212 loc) · 7.88 KB
/
EndRunR.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
251
<!-- The flash! -->
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
</style>
</head>
<body>
<canvas id="game" width="1500" height="720"></canvas>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = alert("I see you click the button! Bad,Bad!!");
}
var canvas = document.getElementById("game");
var ctx = canvas.getContext("2d");
function floor(x, height) {
this.x = x;
this.width = 1500;
this.height = height;
}
var world = {
height: 720,
width: 1500,
gravity: 10,
highestFloor: 1000,
speed: 12,
distanceTravelled: 0,
points: 0,
smallestFloor: 990,
tilesPassed: 0,
autoScroll: true,
MaxfloorHeight: 150,
binu: 8,
floorTiles: [
new floor(0, 150)
],
stop: function() {
this.autoScroll = false;
},
moveFloor: function() {
for(index in this.floorTiles) {
var tile = this.floorTiles[index];
this.speed += 0.0001;
tile.x -= this.speed;
this.distanceTravelled += 0.01;
this.points += this.binu;
}
},
addFutureTiles: function() {
if(this.floorTiles.length >= 3) {
return;
}
var previousTile = this.floorTiles[this.floorTiles.length - 1];
var biggestJumpableHeight = previousTile.height + player.height * 3.5;
if(biggestJumpableHeight > this.highestFloor) {
biggestJumpableHeight = this.highestFloor;
}
var randomHeight = Math.floor(Math.random() * biggestJumpableHeight) + player.height;
var leftValue = (previousTile.x + previousTile.width);
var next = new floor(leftValue, randomHeight);
this.floorTiles.push(next);
},
cleanOldTiles: function() {
for(index in this.floorTiles) {
if(this.floorTiles[index].x <= -this.floorTiles[index].width) {
this.floorTiles.splice(index, 1);
this.tilesPassed--;
if(this.tilesPassed % 3 == 0 && this.speed < this.maxSpeed) {
}
}
}
},
getDistanceToFloor: function(playerX) {
for(index in this.floorTiles) {
var tile = this.floorTiles[index];
if(tile.x <= playerX && tile.x + tile.width >= playerX) {
return tile.height;
}
}
return -3;
},
tick: function() {
if(this.points > 100000) {
this.autoScroll = true;
this.speed += 2;
if(this.points > 25000){
sky = ctx.fillStyle = "lightblue";
ground = ctx.fillStyle = "white";
}
if(this.speed > 1600){
alert("You win! Your points are: " + this.points);
window.close();
}
}
if(!this.autoScroll) {
alert("You lost");
window.close();
}
this.cleanOldTiles();
this.addFutureTiles();
this.moveFloor();
},
draw: function() {
var sky = ctx.fillStyle = "gray";
ctx.fillRect (0, 0, this.width, this.height);
for(index in this.floorTiles) {
var tile = this.floorTiles[index];
var y = world.height - tile.height;
var ground = ctx.fillStyle = "blue";
ctx.fillRect(tile.x, y, tile.width, tile.height);
}
ctx.fillStyle = "ocean";
ctx.font = "28px Arial";
ctx.fillText("Speed: " + this.speed, 10, 40);
ctx.fillText("Distance: " + this.distanceTravelled, 10, 75);
ctx.fillText("Points: " + this.points, 10, 110);
ctx.fillText("Level 3, The rainy pacific ocean", 10, 145);
}
};
var player = {
x: 160,
y: 340,
height: 20,
width: 20,
downwardForce: world.gravity,
jumpHeight: 0,
getDistanceFor: function(x) {
var platformBelow = world.getDistanceToFloor(x);
var dis = world.height - this.y - platformBelow
return dis + dis;
},
applyGravity: function() {
this.currentDistanceAboveGround = this.getDistanceFor(this.x);
var rightHandSideDistance = this.getDistanceFor(this.x + this.width + this.width);
if(this.currentDistanceAboveGround < 0 || rightHandSideDistance < 0) {
world.stop();
}
},
processGravity: function() {
this.y += this.downwardForce;
var floorHeight = world.getDistanceToFloor(this.x, this.width);
var topYofPlatform = world.height - floorHeight;
if(this.y > topYofPlatform) {
this.y = topYofPlatform;
}
if(this.downwardForce < 0) {
this.jumpHeight += (this.downwardForce * -1);
if(this.jumpHeight >= player.height * 10) {
this.downwardForce = world.gravity;
this.jumpHeight = 0;
}
}
},
keyPress: function(keyInfo) {
var floorHeight = world.getDistanceToFloor(this.x, this.width);
var onTheFloor = floorHeight == (world.height - this.y);
if(onTheFloor) {
this.downwardForce = -7;
}
},
tick: function() {
this.processGravity();
this.applyGravity();
},
draw: function() {
var p_h = player.height - 5;
var p_w = player.width - 5;
var t_w = this.width - 5;
var t_h = this.height - 5;
var p_x = player.x + 2;
var p_y = player.y - 2;
//lighting
var l_h1 = player.height - 15;
var l_w1 = player.width + 10;
var l_w2 = this.width - 15;
var l_h2 = this.height + 10;
//The player - yellow square
var l_x = player.x - 30;
// y1 or lighting 1
var l_y1 = player.y - 2;
//y2 or lighting 2
var l_y2 = player.y - 12;
//flash body
ctx.fillStyle = "red";
var rec = ctx.fillRect(player.x, player.y - player.height, this.height, this.width);
ctx.fillRect(player.x, player.y - player.height, this.height, this.width);
ctx.fillStyle = "gold";
var rec = ctx.fillRect(p_x, p_y - p_h, t_h, t_w);
ctx.fillRect(p_x, p_y - p_h, t_h, t_w);
//The lighting 1(bottom)
ctx.fillStyle = "banana";
var rec = ctx.fillRect(l_x, l_y1 - l_h1, l_h2, l_w2);
ctx.fillRect(l_x, l_y1 - l_h1, l_h2, l_w2);
//The lighting 2(top)
ctx.fillStyle = "banana";
var rec = ctx.fillRect(l_x, l_y2 - l_h1, l_h2, l_w2);
ctx.fillRect(l_x, l_y2 - l_h1, l_h2, l_w2);
}
};
window.addEventListener("keypress", function(keyInfo) { player.keyPress(keyInfo); }, false);
function tick() {
player.tick();
world.tick();
world.draw();
player.draw();
window.setTimeout("tick()", 1000/60);
}
tick();
</script>
</body>
</html>