-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame3.js
128 lines (126 loc) · 4.9 KB
/
game3.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
import Phaser from "phaser";
//this.scene.start("Game1")
export default class Game3 extends Phaser.Scene{
constructor() {
super("Game3")
this.gameOver = false;
this.platforms;
this.floor;
this.cursors;
this.player;
this.space;
this.gameWon = false
this.red_platforms;
this.deathSound;
this.black_platforms;
this.flag;
this.winSound;
}
preload() {
//Game config code
this.load.image('backdrop', "./assets/backdrop2.jpg")
this.load.image("block1", "./assets/pixil-frame-0 (1).png")
this.load.image("block2", "./assets/pixil-frame-0 (2).png")
this.load.image("block3", "./assets/pixil-frame-0 (3).png")
this.load.image("block4", "./assets/pixil-frame-0 (4).png")
this.load.image("floor", './assets/floor.png')
this.load.image('flag', "./assets/flag.jpg")
this.load.audio("die", './assets/mixkit-cartoon-whistle-game-over-606.wav')
this.load.audio("win", "./assets/mixkit-arcade-score-interface-217.wav")
this.load.image('red_platform', "./assets/red_platform.png")
this.load.spritesheet("person", "./assets/sprite.png", {
frameWidth: 32,
frameHeight: 32,
});
this.cursors = this.input.keyboard.createCursorKeys();
this.space = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE)
}
create() {
this.add.image(100, 75, "backdrop")
this.deathSound = this.sound.add("die")
this.winSound = this.sound.add('win')
this.platforms = this.physics.add.staticGroup()
this.floor = this.physics.add.staticGroup()
this.flag = this.physics.add.staticGroup()
this.red_platforms = this.physics.add.staticGroup()
this.black_platforms = this.physics.add.staticGroup()
this.player = this.physics.add.sprite(20, 20, "person");
this.player.setBounce(0.2);
this.player.setCollideWorldBounds(true);
this.physics.add.collider(this.player, this.platforms);
this.physics.add.collider(this.player, this.black_platforms);
this.anims.create({
key: "right",
frames: this.anims.generateFrameNumbers("person", { start: 0, end: 7 }),
frameRate: 10,
repeat: -1,
});
this.anims.create({
key: "left",
frames: this.anims.generateFrameNumbers("person", {
start: 8,
end: 15,
}),
frameRate: 10,
repeat: -1,
})
this.add.text(150, 200, 'You were really naive enough to think this game can be beaten', { fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif' });
function createBordersAndFloor(scene) {
//left and righ border border
let k = 0
let y = 0
for (let j = 0; j < 13; j++) {
k += 50
y += 50
scene.platforms.create(0, k, "block4").refreshBody()
scene.platforms.create(800, y, "block4").refreshBody()
}
//floor
for (let i = 0; i < 9; i ++) {
scene.floor.create((i * 100), 600, "floor").refreshBody()
}
}
function generatePlatforms(scene) {
console.log("game 2")
scene.platforms.create(30, 100, "block1").setScale(0.5).refreshBody()
scene.platforms.create(765, 450, "block1").setScale(0.5).refreshBody()
scene.flag.create(765, 435, "flag").setScale(0.2).refreshBody()
}
this.physics.add.collider(this.player, this.floor, (player, floor) => {
this.gameOver = true
})
this.physics.add.collider(this.player, this.red_platforms, (player, red_platforms) => {
this.gameOver = true
})
this.physics.add.collider(this.player, this.flag, (player, flag) => {
this.gameWon = true
})
createBordersAndFloor(this)
generatePlatforms(this)
}
update() {
//code that runs for every frame
if (this.gameOver && !this.deathSound.isPlaying) {
this.gameOver = false
this.deathSound.play()
this.scene.start("GameOver")
}
if (this.gameWon && !this.winSound.isPlaying) {
this.gameWon = false
this.winSound.play()
this.scene.restart()
}
if (this.cursors.right.isDown) {
this.player.setVelocityX(160);
this.player.anims.play("right", true);
} else if (this.cursors.left.isDown) {
this.player.setVelocityX(-160);
this.player.anims.play("left", true);
} else if (this.space.isDown && this.player.body.touching.down) {
this.player.setVelocityY(-320)
} else {
this.player.setVelocityX(0);
this.player.anims.stop()
}
}
}