forked from midudev/super-midu-bros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
116 lines (94 loc) · 2.59 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
/* global Phaser */
import { createAnimations } from "./animations.js"
const config = {
type: Phaser.AUTO, // webgl, canvas
width: 256,
height: 244,
backgroundColor: '#049cd8',
parent: 'game',
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload, // se ejecuta para precargar recursos
create, // se ejecuta cuando el juego comienza
update // se ejecuta en cada frame
}
}
new Phaser.Game(config)
// this -> game -> el juego que estamos construyendo
function preload () {
this.load.image(
'cloud1',
'assets/scenery/overworld/cloud1.png'
)
this.load.image(
'floorbricks',
'assets/scenery/overworld/floorbricks.png'
)
this.load.spritesheet(
'mario', // <--- id
'assets/entities/mario.png',
{ frameWidth: 18, frameHeight: 16 }
)
this.load.audio('gameover', 'assets/sound/music/gameover.mp3')
} // 1.
function create () {
// image(x, y, id-del-asset)
this.add.image(100, 50, 'cloud1')
.setOrigin(0, 0)
.setScale(0.15)
this.floor = this.physics.add.staticGroup()
this.floor
.create(0, config.height - 16, 'floorbricks')
.setOrigin(0, 0.5)
.refreshBody()
this.floor
.create(150, config.height - 16, 'floorbricks')
.setOrigin(0, 0.5)
.refreshBody()
this.mario = this.physics.add.sprite(50, 100, 'mario')
.setOrigin(0, 1)
.setCollideWorldBounds(true)
.setGravityY(300)
this.physics.world.setBounds(0, 0, 2000, config.height)
this.physics.add.collider(this.mario, this.floor)
this.cameras.main.setBounds(0, 0, 2000, config.height)
this.cameras.main.startFollow(this.mario)
createAnimations(this)
this.keys = this.input.keyboard.createCursorKeys()
}
function update () { // 3. continuamente
if (this.mario.isDead) return
if (this.keys.left.isDown) {
this.mario.anims.play('mario-walk', true)
this.mario.x -= 2
this.mario.flipX = true
} else if (this.keys.right.isDown) {
this.mario.anims.play('mario-walk', true)
this.mario.x += 2
this.mario.flipX = false
} else {
this.mario.anims.play('mario-idle', true)
}
if (this.keys.up.isDown && this.mario.body.touching.down) {
this.mario.setVelocityY(-300)
this.mario.anims.play('mario-jump', true)
}
if (this.mario.y >= config.height) {
this.mario.isDead = true
this.mario.anims.play('mario-dead')
this.mario.setCollideWorldBounds(false)
this.sound.add('gameover', { volume: 0.2 }).play()
setTimeout(() => {
this.mario.setVelocityY(-350)
}, 100)
setTimeout(() => {
this.scene.restart()
}, 2000)
}
}