-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
124 lines (112 loc) · 3.52 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
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script type="text/javascript" src='assets/js/phaser.min.js'></script>
<style type="text/css">
body{
margin:0;
}
</style>
</head>
<body>
<script type="text/javascript">
var game = new Phaser.Game(800,600,Phaser.Auto,'',{preload:preload,create:create,update:update});
function preload()
{
game.load.image('paddle','assets/imgs/paddle.jpg',200,200);
game.load.image('ball','assets/imgs/ball.png');
game.load.audio('hit_1',['assets/audio/hit.ogg','assets/audio/hit.wav','assets/audio/hit.mp3']);
game.load.audio('hit_2',['assets/audio/hit.ogg','assets/audio/hit.wav','assets/audio/hit.mp3']);
}
var paddle1,ball,score1,score2,
paddle2,score_text1,score_text2;
function create()
{
ball_lanched = false;
ball_velocity = 440;
paddle1 = create_paddle(0,game.world.centerY);
paddle2 = create_paddle(game.world.width - 8,game.world.centerY);
ball = create_ball(game.world.centerX,game.world.centerY);
game.input.onDown.add(control_ball,this);
score_text1 = game.add.text(128,30,'0',{
font:'64px Arial ',
fill:"#ffffff",
align: 'center'
});
score_text2 = game.add.text(600,30,'0',{
font:'64px Arial ',
fill:"#ffffff",
align: 'center'
});
score1 = 0;
score2 = 0;
}
function update()
{
score_text1.text = score1;
score_text2.text = score2;
control_paddle(paddle1,game.input.y);
game.physics.arcade.collide(paddle1,ball,function(){
game.sound.play('hit_1');
});
game.physics.arcade.collide(paddle2,ball,function(){
game.sound.play('hit_2');
});
if (ball.body.blocked.left) {
score2 +=1;
}
if (ball.body.blocked.right) {
score1 +=1;
}
paddle2.body.velocity.setTo(ball.body.velocity.y);
paddle2.body.velocity.x = 0;
paddle2.body.maxVelocity.y = 250;
}
function create_paddle(x,y)
{
var paddle = game.add.sprite(x,y,'paddle');
paddle.anchor.setTo(0.5,0.5);
game.physics.arcade.enable(paddle);
paddle.body.collideWorldBounds = true;
paddle.body.immovable = true;
paddle.scale.setTo(.5,.5);
return paddle;
}
function control_paddle(paddle,y)
{
paddle.y = y;
if(paddle.y < paddle.height /2)
{
paddle.y = paddle.height/2;
}
else if (paddle.y > game.world.height - paddle.height/2)
{
paddle.y = game.world.height - paddle.height/2;
}
}
function create_ball(x,y)
{
var ball = game.add.sprite(x,y,'ball');
ball.anchor.setTo(.5,.5);
game.physics.arcade.enable(ball);
ball.body.collideWorldBounds = true;
ball.body.bounce.setTo(1,1);
return ball;
}
function control_ball()
{
if(ball_lanched)
{
ball.x = game.world.centerX;
ball.y = game.world.centerY;
ball.body.velocity.setTo(0,0);
}else{
ball.body.velocity.x = -ball_velocity;
ball.body.velocity.y = ball_velocity;
}
}
</script>
</body>
</html>