-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
237 lines (189 loc) · 6.83 KB
/
game.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var game = new Phaser.Game(800, 600, Phaser.AUTO, "gameDiv");
var mainState = {
preload: function() {
if (!game.device.desktop) {
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
// to fit landscape on mobile
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
}
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
game.stage.backgroundColor = '#185edc';
game.load.image('bird', 'assets/blossom.png');
game.load.image('pipe', 'assets/fishy.png');
game.load.image('floor', 'assets/floor.gif');
game.load.image('flipy', 'assets/flipy.png');
game.load.image('wave', 'assets/wave.png');
},
create: function() {
game.physics.startSystem(Phaser.Physics.ARCADE);
this.pipes = game.add.group();
this.floors = game.add.group();
this.waves = game.add.group();
this.timer = game.time.events.loop(1500, this.addRowOfPipes, this);
this.timer = game.time.events.loop(1000, this.addRowOfSeaweed, this);
this.timer = game.time.events.loop(1000, this.addRowOfWaves, this);
this.bird = game.add.sprite(10, 245, 'bird');
this.bird.scale.setTo(3.0, 3.0);
game.physics.arcade.enable(this.bird);
this.bird.body.gravity.y = 250;
for (var i = 0; i < 6; i++) {
this.addOneSeaweed(i * 190, 400);
}
for (var i = 0; i < 5; i++) {
this.addOneWave(i * 170, 0);
}
this.addOneWave(800, 0);
// New anchor position
this.bird.anchor.setTo(-0.2, 0.5);
var spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
spaceKey.onDown.add(this.jump, this);
game.input.onDown.add(this.jump, this);
var downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
downKey.onDown.add(this.dive, this);
game.input.onDown.add(this.dive, this);
this.score = 0;
this.labelScore = game.add.text(20, 30, "0", {
font: "30px Arial",
fill: "#ffffff"
});
game.input.onUp.add(this.mouseUp, this);
game.input.onDown.add(this.mouseDown, this);
//this.text1 = game.add.text(game.world.centerX, game.world.centerY, "swipe up or down!");
//this.text1.fill = "#ffffff";
//this.text1.anchor.set(0.5, 0.5);
},
update: function() {
if (this.bird.y < 0 || this.bird.y > game.world.height)
this.restartGame();
game.physics.arcade.overlap(this.bird, this.pipes, this.hitPipe, null, this);
// Slowly rotate the bird downward, up to a certain point.
if (this.bird.angle < 20)
this.bird.angle += 1;
if (this.mouseIsDown == true) {
//get the distance between the start and end point
var distY = Math.abs(game.input.y - this.startY);
//if the distance is greater than 50 pixels then a swipe has happened
if (distY > 50) {
this.swipeDone();
}
}
},
jump: function() {
// If the bird is dead, he can't jump
if (this.bird.alive == false)
return;
this.bird.body.velocity.y = -200;
// Jump animation
game.add.tween(this.bird).to({
angle: -20
}, 100).start();
},
dive: function() {
// If the bird is dead, he can't dive
if (this.bird.alive == false)
return;
this.bird.body.velocity.y = +100;
// Jump animation
game.add.tween(this.bird).to({
angle: -20
}, 100).start();
},
hitPipe: function() {
// If the bird has already hit a pipe, we have nothing to do
if (this.bird.alive == false)
return;
// Set the alive property of the bird to false
this.bird.alive = false;
// Prevent new pipes from appearing
game.time.events.remove(this.timer);
// Go through all the pipes, and stop their movement
this.pipes.forEach(function(p) {
p.body.velocity.x = 0;
}, this);
// Go through all the floors, and stop their movement
this.floors.forEach(function(p) {
p.body.velocity.x = 0;
}, this);
// Go through all the waves and stop their movement
this.waves.forEach(function(p) {
p.body.velocity.x = 0;
}, this);
},
restartGame: function() {
game.state.start('main');
},
addOnePipe: function(x, y) {
var pipe;
if (Math.round(y) % 2 == 0) {
pipe = game.add.sprite(x, y, 'flipy');
pipe.scale.setTo(1.7, 1.7);
} else {
pipe = game.add.sprite(x, y, 'pipe');
pipe.scale.setTo(0.6, 0.6);
}
this.pipes.add(pipe);
game.physics.arcade.enable(pipe);
pipe.body.velocity.x = -200;
pipe.checkWorldBounds = true;
pipe.outOfBoundsKill = true;
},
addRowOfPipes: function() {
this.addOnePipe(800, 0 + (Math.random() * (400 - 100) + 100));
this.labelScore.text = this.score;
this.score += 1;
},
addOneSeaweed: function(x, y) {
var floor = game.add.sprite(x, y, 'floor');
floor.scale.setTo(6.5, 6.5);
this.floors.add(floor);
game.physics.arcade.enable(floor);
// Make the seaweed a little slower than the fish.
floor.body.velocity.x = -170;
floor.checkWorldBounds = true;
floor.outOfBoundsKill = true;
},
addRowOfSeaweed: function() {
this.addOneSeaweed(800, 400);
},
addOneWave: function(x, y) {
var wave = game.add.sprite(x, y, 'wave');
//wave.scale.setTo(6.5, 6.5);
wave.scale.setTo(5.5, 5.5);
this.waves.add(wave);
game.physics.arcade.enable(wave);
// Make the wave a little slower than the fish.
wave.body.velocity.x = -170;
wave.checkWorldBounds = true;
wave.outOfBoundsKill = true;
},
addRowOfWaves: function() {
//this.addOneWave(800, 0);
this.addOneWave(800, 0);
},
mouseDown: function() {
//set the mouseIsDown to true
this.mouseIsDown = true;
//record the place the mouse started
this.startY = game.input.y;
},
mouseUp: function() {
this.mouseIsDown = false;
},
swipeDone: function() {
//get the ending point
var endY = game.input.y;
//check the start point against the end point
if (endY < this.startY) {
//this.text1.text = "Swiped up";
this.jump();
} else {
//this.text1.text = "Swiped down";
//downKey.onDown.add(this.dive, this);
this.dive();
}
},
};
game.state.add('main', mainState);
game.state.start('main');