This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
/
runner.html
161 lines (161 loc) · 4.97 KB
/
runner.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
` <a type="button" class="btn btn-primary" href="Games.html" >
Back</a>
<a type="button" class="btn btn-primary" href="runner.html" >
Restart</a>
<!DOCTYPE html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.min.js"></script>
<html>
<head>
<title>Run!</title>
</head>
<body>
<canvas id="game" width="640" height="480"></canvas>
<script>
var canvas = document.getElementById("game");
var ctx = canvas.getContext("2d");
function floor(x, height) {
this.x = x;
this.width = 700;
this.height = height;
}
var world = {
height: 480,
width: 640,
gravity: 9,
highestFloor: 240,
speed: 5,
distanceTravelled: 0,
maxSpeed: 40,
tilesPassed: 0,
autoScroll: true,
floorTiles: [
new floor(0, 140)
],
stop: function() {
this.autoScroll = false;
},
moveFloor: function() {
for(index in this.floorTiles) {
var tile = this.floorTiles[index];
tile.x -= this.speed;
this.distanceTravelled += this.speed;
}
},
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) {
this.speed++;
}
}
}
},
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 -1;
},
tick: function() {
if(!this.autoScroll) {
return;
}
this.cleanOldTiles();
this.addFutureTiles();
this.moveFloor();
},
draw: function() {
ctx.fillStyle = "cyan";
ctx.fillRect (0, 0, this.width, this.height);
for(index in this.floorTiles) {
var tile = this.floorTiles[index];
var y = world.height - tile.height;
ctx.fillStyle = "indigo";
ctx.fillRect(tile.x, y, tile.width, tile.height);
}
ctx.fillStyle = "white";
ctx.font = "28px Arial";
ctx.fillText("Speed: " + this.speed, 10, 40);
ctx.fillText("Travelled: " + this.distanceTravelled, 10, 75);
}
};
var player = {
x: 160,
y: 340,
height: 20,
width: 20,
downwardForce: world.gravity,
jumpHeight: 3,
getDistanceFor: function(x) {
var platformBelow = world.getDistanceToFloor(x);
return world.height - this.y - platformBelow;
},
applyGravity: function() {
this.currentDistanceAboveGround = this.getDistanceFor(this.x);
var rightHandSideDistance = this.getDistanceFor(this.x + 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 * 6) {
this.downwardForce = world.gravity;
this.jumpHeight = 3;
}
}
},
keyPress: function(keyInfo) {
var floorHeight = world.getDistanceToFloor(this.x, this.width);
var onTheFloor = floorHeight == (world.height - this.y);
if(onTheFloor) {
this.downwardForce = -5;
}
},
tick: function() {
this.processGravity();
this.applyGravity();
},
draw: function() {
ctx.fillStyle = "mediumvioletred";
ctx.fillRect(player.x, player.y - player.height, this.height, this.width);
}
};
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>