-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
152 lines (143 loc) · 4.34 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Gamedev Phaser Workshop - lesson 01: by @theghost1980</title>
<style>* { padding: 0; margin: 0; }</style>
<script src="js/phaser.min.js"></script>
</head>
<body>
<script>
var game = new Phaser.Game(480, 320, Phaser.AUTO, null, {
preload: preload, create: create, update: update
});
//other vars
var ball;
var paddle;
var bricks;
var sbd;
var newBrick;
var brickInfo;
var scoreText;
var score = 0;
var lives = 3;
var livesText;
var lifeLostText;
function preload() {
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
game.stage.backgroundColor = '#eee';
game.load.image('ball', 'img/ball.png');
game.load.image('paddle', 'img/paddle.png');
game.load.image('brick', 'img/brick.png');
game.load.image('sbd', 'img/sbd.png');
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
ball = game.add.sprite(game.world.width*0.5, game.world.height-25, 'ball');
ball.anchor.set(0.5);
game.physics.enable(ball, Phaser.Physics.ARCADE);
ball.body.velocity.set(150, -150);
ball.body.collideWorldBounds = true;
ball.body.bounce.set(1);
//disable the collision below so it can loose
game.physics.arcade.checkCollision.down = false;
ball.checkWorldBounds = true;
ball.events.onOutOfBounds.add(ballLeaveScreen, this);
//ball.body.allowRotation = true;
//ball.body.angularVelocity = 100;
paddle = game.add.sprite(game.world.width*0.5, game.world.height-5, 'paddle');
paddle.anchor.set(0.5,1);
game.physics.enable(paddle, Phaser.Physics.ARCADE);
paddle.body.immovable = true;
sbd = game.add.sprite(75, 3, 'sbd');
sbd.width = 20;
sbd.height = 26;
//creating the bricks
initBricks();
//score text
textStyle = { font: '15px Arial', fill: '#0095DD' };
scoreText = game.add.text(5, 5, 'SBD: 0', textStyle);
livesText = game.add.text(game.world.width-5, 5, 'Lives: '+lives, textStyle);
livesText.anchor.set(1,0);
lifeLostText = game.add.text(game.world.width*0.5, game.world.height*0.5, 'Life lost, click to continue', textStyle);
lifeLostText.anchor.set(0.5);
lifeLostText.visible = false;
}
function update() {
game.physics.arcade.collide(ball, paddle);
game.physics.arcade.collide(ball, bricks, ballHitBrick);
paddle.x = game.input.x || game.world.width*0.5;
}
function initBricks() {
brickInfo = {
width: 100,
height: 21,
count: {
row: 4,
col: 3
},
offset: {
top: 40,
left: 70
},
padding: 15
};
bricks = game.add.group();
for(c=0; c<brickInfo.count.col; c++) {
for(r=0; r<brickInfo.count.row; r++) {
var brickX = (r*(brickInfo.width+brickInfo.padding))+brickInfo.offset.left;
var brickY = (c*(brickInfo.height+brickInfo.padding))+brickInfo.offset.top;
newBrick = game.add.sprite(brickX, brickY, 'brick');
game.physics.enable(newBrick, Phaser.Physics.ARCADE);
newBrick.body.immovable = true;
newBrick.anchor.set(0.5);
bricks.add(newBrick);
}
}
}
function ballHitBrick(ball, brick) {
brick.kill();
//making it difficult
paddle.width = paddle.width - 8;
//score
score += 10;
scoreText.setText('SBD: '+score);
var count_alive = 0;
for (i = 0; i < bricks.children.length; i++) {
if (bricks.children[i].alive == true) {
count_alive++;
}
}
if (count_alive == 0) {
playAudio();
alert('You won the game, congratulations! \n WE ARE STEEMIANS \n Now go and spread the platform!');
location.reload();
}
}
function ballLeaveScreen() {
lives--;
if(lives) {
livesText.setText('Lives: '+lives);
lifeLostText.visible = true;
ball.reset(game.world.width*0.5, game.world.height-25);
paddle.reset(game.world.width*0.5, game.world.height-5);
game.input.onDown.addOnce(function(){
lifeLostText.visible = false;
ball.body.velocity.set(150, -150);
}, this);
}
else {
alert('You lost, game over! \n Other Social Media WON :(');
location.reload();
}
}
function playAudio() {
document.getElementById("myAudio").play();
}
</script>
<p><center>by theghost1980</center></p>
<audio src="./audio/vegas.wav" id="myAudio"></audio>
</body>
</html>