-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
208 lines (179 loc) · 4.45 KB
/
game.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(function(){
//ui :catch each element
mit.ui = {
_start: document.getElementById("start"),
_score_board: document.getElementById("score_board"),
_canvas : document.getElementById("canvas")
//
};
//Main canvas
var time = 1e3/60;
var content = mit.ui._canvas.getContext("2d");
canvas.width = mit.World.world_w;
canvas.height = mit.World.world_h;
//var score_board = document.getElementById("score_board");
var sb_ctx = mit.ui._score_board.getContext("2d");
mit.Game = {
start_btn_clicked:false,
game_started : false,
game_over : false,
game_pause : false,
score: 0,
distance:0,
startGame : function()
{
//console.log("game start");
//init data
this.score = 0;
this.distance = 0;
this.game_started = true;
this.start_btn_clicked = true;
this.game_pause = false;
//draw background
mit.Background.init();
mit.Background.drawWater(content);
//init whale
mit.whale = new mit.Whale();
mit.whale.init();
mit.whale.draw(content);
//draw board
mit.Board.init(sb_ctx);
window.clearInterval(window.stop);
},
gameOver : function()
{
//console.log("game over");
window.clearInterval(window.stop);
mit.Background.drawWater(content);
//write game over
content.font = "bold 20px Arial";
content.textAlign = "center";
content.textBaseline = "middle";
var text = "Game Over";
content.fillText(text, 300,200);
text = "Your Score: "+mit.Game.score;
content.fillText(text, 300,230);
text = "Your Distance: "+mit.Game.distance;
content.fillText(text, 300,260);
mit.Game.start_btn_clicked=false;
},
renderGame : function()
{
//settime foo
//console.log("renderGame begin");
//document.getElementById("score_board").innerHTML = "Distance: "+mit.Game.distance+"m |Score: "+mit.Game.score+"| HP: "+mit.whale.Hp+"| Air: "+mit.whale.Air;
//window.requestAnimationFrame(mit.Game.renderGame);
//draw bg
content.clearRect(0,0,mit.World.world_w, mit.World.world_h);
mit.Background.drawWater(content);
//if game over
//console.log(mit.Game.game_started);
if(mit.whale.isDead == true)
{
//console.log("whale dead");
if(mit.whale.Hp <0) mit.whale.Hp = 0;
if(mit.whale.Air <0) mit.whale.Air = 0;
this.game_over = true;
mit.Game.gameOver();
return;
}
else if(mit.Game.game_started==true)//if game start
{
//console.log("draw whale");
mit.whale.pos_y -= mit.goUp;
mit.whale.draw(content);
mit.EnermyFactory.draw(content);
}
//draw all sprites
mit.Board.draw(sb_ctx);
},
//canvas,content
ascend : function()//listen game start button
{
if(mit.Game.start_btn_clicked==false)
return;
if(mit.Game.game_started==false)
{
mit.Game.game_started = true;
mit.Game.game_over = false;
}
mit.goUp = 10;
},
descend : function()//listen game end button
{
if(mit.Game.start_btn_clicked==false)
return;
mit.goUp = 0;
}
};
//Event handler
window.onload = LoopGame;
mit.ui._start.addEventListener("click",LoopGame,false);
window.addEventListener("mousedown",mousedown_event,false);
window.addEventListener("mouseup",mouseup_event,false);
window.addEventListener("keydown",keydown_event,false);
window.addEventListener("keyup",keyup_event,false);
//Event deal
function LoopGame(e)
{
//console.log("loop game");
if(!(document.getElementById("score_board").getContext))
{alert("该浏览器不支持Html5,请确认运行平台为:Firefox1.5+,Chrome,Opera9+");}
if(mit.Game.start_btn_clicked==false)
{
mit.Game.startGame();
mit.Game.start_btn_clicked = true;
}
else
{
//console.log(window.stop);
if(window.stop)
{
clearInterval(stop);
}
window.stop = setInterval(mit.Game.renderGame,time);
}
}
function mousedown_event(e)
{
///console.log("mosue down");
mit.Game.ascend();
};
function mouseup_event(e)
{
//console.log("mosue up");
mit.Game.descend();
};
function keydown_event(e)
{
//space || enter
if(e.keyCode==32 || e.keyCode==13)
{
//console.log("space down");
//mit.Game.startGame();
LoopGame();
//e.preventDefault();
}
//up
else if(e.keyCode == 38)
{
//console.log("key up on");
mit.Game.ascend();
//e.preventDefault();
}
else
{
//console.log("key down on");
e.preventDefault();
}
};
function keyup_event(e)
{
if(e.keyCode == 38)
{
//console.log("key up");
mit.Game.descend();
//e.preventDefault();
}
};
}());