forked from CodingTrain/Flappy-Bird-Clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
128 lines (103 loc) · 2.66 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/cXgA1d_E-jY
// P5 exported functions (eslint flags)
/* exported preload, setup, draw, keyPressed */
// Exported sprites (eslint flags)
/* exported birdSprite, pipeBodySprite, pipePeakSprite */
var bird;
var pipes;
var parallax = 0.8;
var score = 0;
var maxScore = 0;
var birdSprite;
var pipeBodySprite;
var pipePeakSprite;
var bgImg;
var bgX;
var gameoverFrame=0;
var touched = false;
var prevTouched = touched;
function preload() {
pipeBodySprite = loadImage('./graphics/pipe_body.png');
pipePeakSprite = loadImage('./graphics/pipe_body.png');
birdSprite = loadImage('graphics/train.png');
bgImg = loadImage('graphics/background.png');
}
function setup() {
createCanvas(600, 600);
reset();
}
function draw() {
background(0);
// Draw our background image, then move it at the same speed as the pipes
image(bgImg, bgX, 0, bgImg.width, height);
bgX -= pipes[0].speed * parallax;
// this handles the "infinite loop" by checking if the right
// edge of the image would be on the screen, if it is draw a
// second copy of the image right next to it
// once the second image gets to the 0 point, we can reset bgX to
// 0 and go back to drawing just one image.
if (bgX <= -bgImg.width + width) {
image(bgImg, bgX + bgImg.width, 0, bgImg.width, height);
if (bgX <= -bgImg.width) {
bgX = 0;
}
}
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
pipes[i].show();
if (pipes[i].pass(bird)) {
score++;
}
if (pipes[i].hits(bird)) {
gameover();
}
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
}
}
bird.update();
bird.show();
if ((frameCount-gameoverFrame) % 150 == 0) {
pipes.push(new Pipe());
}
showScores();
// touches is an list that contains the positions of all
// current touch points positions and IDs
// here we check if touches' length is bigger than one
// and set it to the touched var
touched = (touches.length > 0);
// if user has touched then make bird jump
// also checks if not touched before
if (touched && !prevTouched){
bird.up();
}
// updates prevTouched
prevTouched = touched;
}
function showScores() {
textSize(32);
text('score: ' + score, 1, 32);
text('record: ' + maxScore, 1, 64);
}
function gameover() {
textSize(64);
text('HIT', width / 2, height / 2);
maxScore = max(score, maxScore);
score = 0;
reset();
}
function reset() {
bgX = 0;
pipes = [];
bird = new Bird();
pipes.push(new Pipe());
gameoverFrame=frameCount-1;
}
function keyPressed() {
if (key === ' ') {
bird.up();
}
}