-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
142 lines (107 loc) · 3.13 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var game = new Phaser.Game(400, 500, Phaser.AUTO, 'game_div');
var main_state = {
preload: function()
{
this.game.stage.backgroundColor = '#71c5cf';
this.game.load.image('bird', 'assets/bird.png');
this.game.load.image('pipe', 'assets/pipe.png');
this.game.load.image('background', 'assets/background.png');
},
create: function()
{
this.bg1 = this.game.add.sprite(0, 0, 'background');
this.bg2 = this.game.add.sprite(398, 0, 'background');
this.score = 0;
var style = {font:"30px Arial", fill:"#ffffff"};
this.label_score = this.game.add.text(20, 20, "0", style);
var anlustyle = {font:"10px Arial", fill:"#ffffff"};
this.anlu = this.game.add.text(330, 20, "Dear Anlu", anlustyle);
//为小鸟增加sprite
this.bird = game.add.sprite(100, 245, 'bird');
//游戏引擎中的重力
this.bird.body.gravity.y = 1000;
//空格键监听,jump()作为回调函数
var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
space_key.onDown.add(this.jump, this);
//暂停按钮
var pause_key = this.game.input.keyboard.addKey(Phaser.Keyboard.ENTER);
pause_key.onDown.add(this.pause, this);
//关注事件
this.game.onPause.add(this.monPause, this);
this.game.onResume.add(this.monResume, this);
//为水管批量添加sprite
this.pipes = game.add.group();
this.pipes.createMultiple(20, 'pipe');
//为add_row_of_pipes函数设置循环调用
this.timer = this.game.time.events.loop(1500, this.add_row_of_pipes, this);
//this.timer2 = this.game.time.events.loop(200, this.fly, this);
},
update: function()
{
if(this.bird.inWorld == false)
this.restart_game();
//游戏引擎中的碰撞监测
this.game.physics.overlap(this.bird, this.pipes, this.restart_game, null, this);
//移动背景
this.move_background(this.bg1);
this.move_background(this.bg2);
},
jump: function()
{
//垂直位置给一个速度
this.bird.body.velocity.y = -350;
},
fly: function()
{
this.bird.angle = 0;
var animation = this.game.add.tween(this.bird);
animation.to({angle:-20},100);
animation.start();
},
move_background: function(bg)
{
if(bg.x < -398)
bg.x = 398;
bg.x -= 2;
},
restart_game: function()
{
this.game.time.events.remove(this.timer);
this.game.state.start('main');
},
pause: function()
{
if(this.game.paused === true)
this.game.paused = false;
else
this.game.paused = true;
},
monPause: function()
{
var pstyle = {font:"40px Arial", fill:"#ffffff"};
this.pausefont = this.game.add.text(140, 200, "Pause", pstyle);
},
monResume: function()
{
this.game.world.remove(this.pausefont);
},
add_one_pipe: function(x, y)
{
var pipe = this.pipes.getFirstDead();
pipe.reset(x, y);
pipe.body.velocity.x = -200;
pipe.outOfBoundsKill = true;
this.score += 1;
this.label_score.content = this.score;
},
add_row_of_pipes: function()
{
var hole = Math.floor(Math.random()*5) + 1;
//八个方框组成一个管子
for(var i=0;i<8;i++)
if(i != hole && i != hole + 1)
this.add_one_pipe(400, i*60+10);
}
};
game.state.add('main', main_state);
game.state.start('main');