-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
100 lines (88 loc) · 2.15 KB
/
main.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
var player, gaps;
var State = { STARTSCREEN: 0, RUNNNING: 1, LOST: 2 };
var gameState = State.STARTSCREEN;
var score;
var slowMo = false;
var gameSpeed = 1.0;
var obstacleSpeed;
var acceleration = 0.0;
function setup() {
createCanvas(WIDTH, HEIGHT);
player = new Player();
gapStream = new GapStream();
this.focus();
score = 0;
obstacleSpeed = OBSTACLESTARTSPEED;
}
function userMovement() {
if (keyIsDown(32) && player.canSlowMo()) {
slowMo = true;
} else {
slowMo = false;
}
if (slowMo) {
gameSpeed = SLOWMOSPEED;
} else {
gameSpeed = NORMALSPEED;
}
if (keyIsDown(37) || keyIsDown(65)) {
acceleration -= PLAYERACCELERATION;
acceleration = constrain(acceleration, -PLAYERSPEED, PLAYERSPEED);
player.move(acceleration * gameSpeed * deltaTime);
} else if (keyIsDown(39) || keyIsDown(68)) {
acceleration += PLAYERACCELERATION;
acceleration = constrain(acceleration, -PLAYERSPEED, PLAYERSPEED);
player.move(acceleration * gameSpeed * deltaTime);
} else {
if (acceleration < - (2 * PLAYERACCELERATION)) {
acceleration += PLAYERACCELERATION;
} else if (acceleration > (2 * PLAYERACCELERATION)) {
acceleration -= PLAYERACCELERATION;
} else {
acceleration = 0;
}
}
}
function keyTyped() {
if (key === ' ') {
if (gameState === State.STARTSCREEN) {
gameState = State.RUNNNING;
}
if (gameState === State.LOST) {
gameState = State.RUNNNING;
setup();
}
}
}
function draw() {
background(220);
if (gameState === State.STARTSCREEN) {
rectMode(CENTER);
rect(WIDTH / 2, HEIGHT / 2, WIDTH / 4);
textFont("monospace", 40);
textAlign(CENTER, CENTER);
text('SPACE TO START', WIDTH / 2, HEIGHT / 2);
}
if (gameState === State.RUNNNING) {
userMovement();
gapStream.update();
gapStream.checkCollition(player);
gapStream.draw();
player.draw();
drawScore();
obstacleSpeed += 0.00001;
}
if (gameState === State.LOST) {
rectMode(CENTER);
rect(WIDTH / 2, HEIGHT / 2, WIDTH / 4);
textFont("monospace", 40);
textAlign(CENTER, CENTER);
text('SCORE: ' + score, WIDTH / 2, HEIGHT / 2);
}
}
function drawScore() {
fill('black');
textFont("monospace", 20);
textAlign(LEFT, TOP);
text('SCORE: ' + score, 0, 0);
}