-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
143 lines (117 loc) · 3.59 KB
/
app.js
File metadata and controls
143 lines (117 loc) · 3.59 KB
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
let config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH
},
physics: {
default: "arcade",
arcade: {
gravity: { y: 300 },
debug: false,
},
},
scene: {
preload: preload,
create: create,
update: update,
},
};
let game = new Phaser.Game(config);
function preload() {
this.load.image("background", "assets/background.png");
this.load.image("road", "assets/road.png");
this.load.image("column", "assets/column.png");
this.load.spritesheet("bird", "assets/bird.png", {
frameWidth: 64,
frameHeight: 96,
});
}
var bird;
let hasLanded = false;
let cursors;
let hasBumped = false;
let isGameStarted = false;
let isGameOver = false;
let isRestartTapped = false;
let messageToPlayer;
function gameOver(scene, message) {
isGameOver = true;
messageToPlayer.text = 'Tap or press space to restart';
}
function create() {
const background = this.add.image(0, 0, "background").setOrigin(0, 0);
const roads = this.physics.add.staticGroup();
const topColumns = this.physics.add.staticGroup({
key: "column",
repeat: 1,
setXY: { x: 200, y: 0, stepX: 300 },
});
const bottomColumns = this.physics.add.staticGroup({
key: "column",
repeat: 1,
setXY: { x: 350, y: 400, stepX: 300 },
});
const road = roads.create(400, 568, "road").setScale(2).refreshBody();
bird = this.physics.add.sprite(0, 50, "bird").setScale(2);
bird.setBounce(0.2);
bird.setCollideWorldBounds(true);
this.physics.add.overlap(bird, road, () => (hasLanded = true), null, this);
this.physics.add.collider(bird, road);
cursors = this.input.keyboard.createCursorKeys();
this.physics.add.overlap(bird, topColumns, () => hasBumped = true, null, this);
this.physics.add.overlap(bird, bottomColumns, () => hasBumped = true, null, this);
this.physics.add.collider(bird, topColumns);
this.physics.add.collider(bird, bottomColumns);
messageToPlayer = this.add.text(0, 0, `Instructions: Press space bar to start`, { fontFamily: '"Comic Sans MS", Times, serif', fontSize: "20px", color: "white", backgroundColor: "black" });
Phaser.Display.Align.In.BottomCenter(messageToPlayer, background, 0, 50);
}
function update() {
if (cursors.up.isDown || cursors.space.isDown || this.input.activePointer.isDown) {
bird.setVelocityY(-160);
}
if ((cursors.up.isDown || cursors.space.isDown || this.input.activePointer.isDown) && !hasLanded) {
bird.setVelocityY(-160);
}
if (!hasLanded) {
bird.body.velocity.x = 50;
}
if (hasLanded) {
bird.body.velocity.x = 0;
}
if (cursors.up.isDown && !hasLanded && !hasBumped) {
bird.setVelocityY(-160);
}
if (!hasLanded && !hasBumped) {
bird.body.velocity.x = 50;
} else {
bird.body.velocity.x = 0;
}
if ((cursors.space.isDown || this.input.activePointer.isDown) && !isGameStarted) {
isGameStarted = true;
messageToPlayer.text = 'Instructions: Press the "^" button to stay upright\nAnd don\'t hit the columns or ground';
}
if (!isGameStarted) {
bird.setVelocityY(-160);
}
if (!hasLanded || !hasBumped) {
bird.body.velocity.x = 50;
} if (hasLanded || hasBumped || !isGameStarted) {
bird.body.velocity.x = 0;
}
if ((hasLanded || hasBumped) && !isGameOver) {
isGameOver = true;
messageToPlayer.text = 'Oh no! You crashed!';
gameOver();
}
if (bird.x > 750 && !isGameOver) {
isGameOver = true;
messageToPlayer.text = 'Congrats! You won!';
gameOver();
}
if (isGameOver && (cursors.space.isDown || this.input.activePointer.isDown)) {
location.reload();
}
}