-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
111 lines (97 loc) · 2.41 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
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
let starterBird;
let starterSwitcher;
let bird;
let pipes;
let backgroundImg;
let botPipeImg;
let topPipImg;
let gameOverImg;
let base;
let gameOver;
let input;
function preload() {
backgroundImg = loadImage('images/background-day.png');
base = loadImage('images/base.png');
botPipeImg = loadImage('images/pipe-green.png');
topPipImg = loadImage('images/pipe-green-top.png');
birdImgMidFlap = loadImage('images/yellowbird-midflap.png');
birdImgDownFlap = loadImage('images/yellowbird-downflap.png');
birdImgUpFlap = loadImage('images/yellowbird-upflap.png');
gameOverImg = loadImage('images/gameover.png')
}
function setup() {
angleMode(DEGREES);
createCanvas(450, 550);
background(backgroundImg);
drawBase();
bird = new Bird();
pipes = new Pipes();
starterBird = new Bird();
frameRate(60);
gameOver = false;
input = false;
starterSwitcher = -1;
}
function draw() {
if (input) {
background(backgroundImg);
bird.update();
bird.draw();
pipes.update();
pipes.draw();
drawBase();
let fps = frameRate();
fill(0);
stroke(0);
text("FPS: " + fps.toFixed(2), 10, height - 10);
if (bird.collide()) {
console.log("lose");
image(gameOverImg, width / 2 - (gameOverImg.width / 2), height * 0.25);
setTimeout(noLoop(),5000);
noLoop();
gameOver = true;
}
} else {
background(backgroundImg);
starterBird.velocity = 7;
starterBird.y += starterSwitcher * 0.3;
starterBird.draw();
if (starterBird.y > height / 2 + 6) {
starterSwitcher = -1;
} else if (starterBird.y < height / 2 - 6) {
starterSwitcher = 1;
}
drawBase();
}
}
function drawBase() {
return image(base, 0, height - base.height + 30, width, base.height);
}
function reset() {
frameRate(60);
input = false;
gameOver = false;
bird = new Bird();
pipes = new Pipes();
starterBird = new Bird();
starterSwitcher = -1;
loop();
}
function keyPressed() {
if (!gameOver) {
if (keyCode === UP_ARROW || key === ' ') {
input = true;
bird.up();
} else {
reset();
}
}
}
function mousePressed() {
if (!gameOver) {
input = true;
bird.up();
} else {
reset();
}
}