-
Notifications
You must be signed in to change notification settings - Fork 83
/
sketch.js
139 lines (113 loc) · 2.98 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
129
130
131
132
133
134
135
136
137
138
139
// 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 isOver = false;
var touched = false;
var prevTouched = touched;
function preload() {
pipeBodySprite = loadImage('graphics/pipe_marshmallow_fix.png');
pipePeakSprite = loadImage('graphics/pipe_marshmallow_fix.png');
birdSprite = loadImage('graphics/train.png');
bgImg = loadImage('graphics/background.png');
}
function setup() {
createCanvas(800, 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);
textAlign(CENTER, CENTER);
text('GAMEOVER', width / 2, height / 2);
textAlign(LEFT, BASELINE);
maxScore = max(score, maxScore);
isOver = true;
noLoop();
}
function reset() {
isOver = false;
score = 0;
bgX = 0;
pipes = [];
bird = new Bird();
pipes.push(new Pipe());
gameoverFrame = frameCount - 1;
loop();
}
function keyPressed() {
if (key === ' ') {
bird.up();
if (isOver) reset(); //you can just call reset() in Machinelearning if you die, because you cant simulate keyPress with code.
}
}
function touchStarted() {
if (isOver) reset();
}