-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
82 lines (65 loc) · 1.45 KB
/
sketch.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
var width = 700;
var height = 300;
var floorHeight = 50;
var player;
var lastSpawn;
var obstacles;
var distances = [
40,40,40,40,
50,50,
60,60,60,
70
];
var score;
var gameOver;
function setup() {
createCanvas(700, 300);
player = new Player(height, floorHeight);
score = 0;
obstacles = [];
spawn();
gameOver = false;
}
function spawn () {
var o = new Obstacle(height, width, floorHeight);
obstacles.push(o);
nextSpawn = random(distances);
}
function draw() {
background(51);
fill (204);
noStroke();
rect(0, height - floorHeight, width, height - floorHeight);
player.update();
player.show();
if (nextSpawn <= 0) {
spawn();
}
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
obstacles[i].show();
if (obstacles[i].outOfScreen()) {
obstacles.splice(i, 1);
}
else {
if (player.collided(obstacles[i])) {
console.log('COLLISION!');
noLoop();
gameOver = true;
}
}
}
nextSpawn --;
score ++;
textSize(23);
text(Math.floor(score / 10), 32, 32);
}
function keyPressed() {
if (keyCode == 0x20) { // 0x20: space
if (gameOver === true) {
setup();
loop();
}
player.jump ();
}
}