-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
127 lines (100 loc) · 3.75 KB
/
Copy pathmain.js
File metadata and controls
127 lines (100 loc) · 3.75 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
// Create our 'main' state that will contain the game
var mainState = {
preload: function () {
// This function will be executed at the beginning
// That's where we load the images and sounds
game.load.image('player', 'assets/player.png');
game.load.image('wall', 'assets/wall.png');
game.load.image('coin', 'assets/coin.png');
game.load.image('enemy', 'assets/enemy.png');
},
create: function () {
// This function is called after the preload function
// Here we set up the game, display sprites, etc.
// Set the background color to blue
game.stage.backgroundColor = '#3598db';
// Start the Arcade physics system (for movements and collisions)
game.physics.startSystem(Phaser.Physics.ARCADE);
// Add the physics engine to all game objects
game.world.enableBody = true;
// Variable to store the arrow key pressed
this.cursor = game.input.keyboard.createCursorKeys();
// Create the player in the middle of the game
this.player = game.add.sprite(70, 100, 'player');
// Add gravity to make it fall
this.player.body.gravity.y = 600;
// Create 3 groups that will contain our objects
this.walls = game.add.group();
this.coins = game.add.group();
this.enemies = game.add.group();
// Design the level. x = wall, o = coin, ! = lava.
var level = [
'xxxxxxxxxxxxxxxxxxxxxx',
'! ! x',
'! o x',
'! o x',
'! x',
'! o ! x x',
'xxxxxxxxxxxxxxxx!!!!!x'
];
// Create the level by going through the array
for (var i = 0; i < level.length; i++) {
for (var j = 0; j < level[i].length; j++) {
// Create a wall and add it to the 'walls' group
if (level[i][j] === 'x') {
var wall = game.add.sprite(30 + 20 * j, 30 + 20 * i, 'wall');
this.walls.add(wall);
wall.body.immovable = true;
}
// Create a coin and add it to the 'coins' group
else if (level[i][j] === 'o') {
var coin = game.add.sprite(30 + 20 * j, 30 + 20 * i, 'coin');
this.coins.add(coin);
}
// Create a enemy and add it to the 'enemies' group
else if (level[i][j] === '!') {
var enemy = game.add.sprite(30 + 20 * j, 30 + 20 * i, 'enemy');
this.enemies.add(enemy);
}
}
}
},
update: function () {
// This function is called 60 times per second
// It contains the game's logic
// Make the player and the walls collide
game.physics.arcade.collide(this.player, this.walls);
// Call the 'takeCoin' function when the player takes a coin
game.physics.arcade.overlap(this.player, this.coins, this.takeCoin, null, this);
// Call the 'restart' function when the player touches the enemy
game.physics.arcade.overlap(this.player, this.enemies, this.restart, null, this);
// Move the player when an arrow key is pressed
if (this.cursor.left.isDown) {
this.player.body.velocity.x = -200;
}
else if (this.cursor.right.isDown) {
this.player.body.velocity.x = 200;
}
else {
this.player.body.velocity.x = 0;
}
// Make the player jump if he is touching the ground
if (this.cursor.up.isDown && this.player.body.touching.down) {
this.player.body.velocity.y = -250;
}
},
// Function to kill a coin
takeCoin: function (player, coin) {
coin.kill();
},
// Function to restart the game
restart: function () {
game.state.start('main');
}
};
// Initialize Phaser, and create a 500px by 200px game
var game = new Phaser.Game(500, 200);
// Add the 'mainState' and call it 'main'
game.state.add('main', mainState);
// Start the state to actually start the game
game.state.start('main');