-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
131 lines (115 loc) · 2.83 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
var base = [];
var theBird;
var thePipes = [];
var myWidth;
var myHeight;
let formColor = document.getElementById('color-form');
formColor.addEventListener('click', color);
let restartButton = document.getElementById('restart-button');
restartButton.addEventListener('click', restart);
let sketchHolder = document.getElementById('sketch-holder');
sketchHolder.addEventListener('click', onTouch);
function onTouch() {
if (theBird.a == 0) {
theBird.a = 0.8;
theBird.v = 10;
theBird.mobileUp();
} else {
theBird.mobileUp();
}
}
function restart(event) {
event.preventDefault();
noStroke();
cnv = createCanvas(myWidth, myHeight);
cnv.parent('sketch-holder');
// centerCanvas();
theBird = new bird(colorOfBird);
s = new score();
thePipes = [];
base = [];
base.push(new Base(0));
base.push(new Base(width));
loop();
}
var colorOfBird = '';
function color(event) {
colorOfBird = event.target.value;
noStroke();
cnv = createCanvas(myWidth, myHeight);
cnv.parent('sketch-holder');
theBird = new bird(colorOfBird);
s = new score();
thePipes = [];
base = [];
base.push(new Base(0));
base.push(new Base(width));
loop();
}
function setup() {
myHeight = displayHeight * 0.68;
myWidth = displayWidth * 0.95;
noStroke();
cnv = createCanvas(myWidth, myHeight);
// centerCanvas();
cnv.parent('sketch-holder');
theBird = new bird(colorOfBird);
s = new score();
base.push(new Base(0));
base.push(new Base(width));
}
function draw() {
image(theBird.backgroundimg, 0, 0, width, height);
//image(b.baseimg, 0, 6 * height / 8, width, height / 4);
if (frameCount % 70 == 0) {
if (theBird.a != 0) {
thePipes.push(new pipe());
}
}
for (var i = 0; i < thePipes.length; i++) {
//check whether out of frame
if (thePipes[i].outOfFrame()) {
thePipes.shift();
s.increase();
}
thePipes[i].update();
thePipes[i].show();
//check collision
if (theBird.collide(thePipes[i]) || theBird.y >= (6 * height) / 8) {
noLoop();
}
}
for (var j = 0; j < base.length; j++) {
if (base[j].outOfFrame()) {
base.push(new Base(width * 0.98));
base.shift();
}
base[j].update();
base[j].show();
}
theBird.update();
theBird.show();
s.show();
}
function keyPressed() {
if (key == 'j') {
if (theBird.a == 0) {
theBird.a = 0.7;
theBird.v = 10;
theBird.up();
} else {
theBird.up();
}
} else if (key == 'r') {
noStroke();
cnv = createCanvas(myWidth, myHeight);
cnv.parent('sketch-holder');
theBird = new bird(colorOfBird);
s = new score();
thePipes = [];
base = [];
base.push(new Base(0));
base.push(new Base(width));
loop();
}
}