-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
237 lines (206 loc) · 4.99 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Phaser 3 - Anadia</title>
<script src="./phaser.js"></script>
<style type="text/css">
body {
margin: 0;
text-align: center;
}
</style>
</head>
<body>
<script type="text/javascript">
var config = {
type: Phaser.AUTO,
width: 800,
height: 800,
physics: {
default: "arcade",
arcade: {
debug: false,
},
},
scene: {
preload: preload,
create: create,
update: update,
},
}
var game = new Phaser.Game(config)
var player
var milks
var cows
var cursors
var score = 0
var scoreText = ""
var gameOver = false
function preload() {
this.load.image("background", "assets/background.png")
this.load.spritesheet("player", "assets/rato.png", {
frameWidth: 32,
frameHeight: 32,
})
// 16 x 16 px
this.load.image("milk", "assets/milk.png")
this.load.spritesheet("cow", "assets/cow.png", {
frameWidth: 27,
frameHeight: 17,
})
}
function create() {
this.add.image(400, 400, "background")
player = this.physics.add.sprite(400, 400, "player")
player.setCollideWorldBounds(true)
// Our player animations, walking left, right, up and down.
this.anims.create({
key: "idle",
frames: [{ key: "player", frame: 0 }],
frameRate: 20,
})
this.anims.create({
key: "down",
frames: this.anims.generateFrameNumbers("player", {
start: 0,
end: 3,
}),
frameRate: 10,
repeat: -1,
})
this.anims.create({
key: "up",
frames: this.anims.generateFrameNumbers("player", {
start: 4,
end: 7,
}),
frameRate: 10,
repeat: -1,
})
this.anims.create({
key: "left",
frames: this.anims.generateFrameNumbers("player", {
start: 8,
end: 11,
}),
frameRate: 10,
repeat: -1,
})
this.anims.create({
key: "right",
frames: this.anims.generateFrameNumbers("player", {
start: 12,
end: 15,
}),
frameRate: 10,
repeat: -1,
})
// Create um group object with physics and start position
milks = this.physics.add.group({
key: "milk",
setXY: {
x: Phaser.Math.Between(11, 789),
y: Phaser.Math.Between(11, 789),
},
setScale: { x: 1.3, y: 1.3 },
})
// Create um group object with physics (Ex: collide, overlap, velocity)
cows = this.physics.add.group({
setScale: { x: 1.5, y: 1.5 },
})
// Animation for the cows
this.anims.create({
key: "run",
frames: this.anims.generateFrameNumbers("cow", {
start: 0,
end: 1,
}),
frameRate: 10,
repeat: -1,
})
// Create the input's handler
cursors = this.input.keyboard.createCursorKeys()
// Checks to see if the player overlaps with any of the milk
// if he does call the pickMilk function
this.physics.add.overlap(player, milks, pickMilk, null, this)
// Checks to see if the player collides with any of the cow
// if he does call the hitCow function
this.physics.add.overlap(player, cows, hitCow, null, this)
// Add score
scoreText = this.add.text(5, 5, "Milk's: 0", {
fontSize: "20px",
fill: "#fff",
})
}
function update() {
if (gameOver) {
return
}
if (cursors.left.isDown) {
player.setVelocityX(-160)
player.anims.play("left", true)
} else if (cursors.right.isDown) {
player.setVelocityX(160)
player.anims.play("right", true)
} else {
player.setVelocityX(0)
}
if (cursors.up.isDown) {
player.setVelocityY(-160)
player.anims.play("up", true)
} else if (cursors.down.isDown) {
player.setVelocityY(160)
player.anims.play("down", true)
} else {
player.setVelocityY(0)
}
if (
!cursors.left.isDown &&
!cursors.right.isDown &&
!cursors.up.isDown &&
!cursors.down.isDown
) {
player.setVelocity(0, 0)
player.anims.play("idle")
}
}
function pickMilk(player, milk) {
milk.disableBody(true, true)
score++
scoreText.setText("Milk's: " + score)
var x =
player.x < 400
? Phaser.Math.Between(408, 792)
: Phaser.Math.Between(8, 392)
var y =
player.y < 400
? Phaser.Math.Between(408, 792)
: Phaser.Math.Between(8, 392)
milks.create(x, y, "milk").setScale(1.3)
var cow = cows.create(x, y, "cow").setScale(1.5)
cow.setBounce(1)
cow.setCollideWorldBounds(true)
cow.setVelocity(
Phaser.Math.Between(-200, 200),
Phaser.Math.Between(-200, 200)
)
cow.anims.play("run", true)
}
function hitCow(player, cow) {
this.physics.pause()
player.setTint(0xff0000)
player.anims.play("idle")
gameOver = true
this.add.text(260, 380, `Game over - ${score}`, {
fontSize: "36px",
fill: "#fff",
})
this.add.text(280, 430, "Refresh the page to restart", {
fontSize: "16px",
fill: "#fff",
})
}
</script>
</body>
</html>