-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.htm
259 lines (229 loc) · 9.05 KB
/
index.htm
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tank</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
margin: 0;
padding: 0;
background-color: #629632;
}
div {
margin: 0 auto;
max-width: 1280px;
}
#score_board {
position: absolute;
width: 100px;
right: 0;
top: 0;
color: #fff;
z-index: 9999;
}
#score_board div {
text-align: center;
}
#score {
font-size: 5em;
font-weight: bold;
}
.end {
background-color: #fff;
color: #000;
width: 400px;
text-align: center;
margin-top: 50px;
padding: 10px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
var engine;
//tank, turret, missile,
var Direction = {
"Up" : 0,
"Right" : 1,
"Down" : 2,
"Left" : 3
};
//Create Engine Class
function Engine() {
var self = this;
this.canvas = document.getElementById('myCanvas');
this.ctx = this.canvas.getContext('2d');
this.player = new Bomberman(2, 2, Direction.Right);
this.map = new Map();
this.bombList = [];
this.run = function() {
requestAnimationFrame(self.run);
self.map.draw(self.ctx);
self.player.draw(self.ctx);
for (key in self.bombList) {
self.bombList[key].draw(self.ctx);
}
};
this.isValidPlayerPosition = function (row,col) {
// Check is the pos within the board
if (col < 0 || col > Map.MAX_HORIZONTAL || row < 0 || row > Map.MAX_VERTICAL) {
return false;
}
// Check if there is a bomb
for (key in self.bombList) {
var bomb = self.bombList[key];
if (bomb.row == row && bomb.col == col) {
return false;
}
}
return (self.map.tileMap[row][col] == 0);
};
this.isValidBombPosition = function (row, col) {
return self.isValidPlayerPosition(row, col);
};
// Set up key handler
document.addEventListener('keydown', function(event) {
if(event.keyCode == 37) {
if (self.isValidPlayerPosition(self.player.row, self.player.col - 1)) {
self.player.move(Direction.Left);
}
self.player.setDirection(Direction.Left);
} else if (event.keyCode == 38) {
if (self.isValidPlayerPosition(self.player.row-1, self.player.col)) {
self.player.move(Direction.Up);
}
self.player.setDirection(Direction.Up);
} else if(event.keyCode == 39) {
if (self.isValidPlayerPosition(self.player.row, self.player.col + 1)) {
self.player.move(Direction.Right);
}
self.player.setDirection(Direction.Right);
} else if (event.keyCode == 40) {
if (self.isValidPlayerPosition(self.player.row + 1, self.player.col)) {
self.player.move(Direction.Down);
}
self.player.setDirection(Direction.Down);
} else if (event.keyCode == 32) {
if (self.isValidBombPosition(self.player.row, self.player.col)) {
self.bombList.push(new Bomb(self.player.col, self.player.row, 3));
}
}
});
};
/*
* Map Class
*/
function Map() {
var brick = new Image();
brick.src = 'brick.png';
var rock = new Image();
rock.src = 'stone.png';
var field = new Image();
field.src = 'field0.png';
this.tileMap = [
[0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2],
[2, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 2],
[2, 0, 0, 1, 1, 1, 0, 0, 0, 0, 2, 1, 0, 0, 2],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0]];
this.draw = function(ctx) {
ctx.drawImage(field, 0, 0);
//ctx.drawImage(sprites,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
var numOfRow = this.tileMap.length;
var numOfCol = this.tileMap[0].length;
for (var row = 0; row < numOfRow; row++) {
for (var col = 0; col < numOfCol; col++) {
var cordinate = Map.rowColToXY(row, col);
if (this.tileMap[row][col] == 0) {
// nth here
} else if (this.tileMap[row][col] == 1) {
ctx.drawImage(brick, cordinate.x, cordinate.y);
} else if (this.tileMap[row][col] == 2) {
ctx.drawImage(rock, cordinate.x, cordinate.y);
}
}
}
};
}
Map.rowColToXY = function(row, col) {
return {
'x': 18 + col * 40,
'y': 64 + row * 36
};
}
// Some constant here
Map.MAX_HORIZONTAL = 15;
Map.MAX_VERTICAL = 11;
/*
* Bomberman Class
*/
function Bomberman(col, row, direction) {
this.col = col;
this.row = row;
this.direction = direction;
}
// Two advantage of using prototype to setup function
// 1. Can access public shared var (set by prototype)
// 2. only a single function shared by every instance object
Bomberman.prototype.move = function(key) {
if (key == Direction.Up) {
this.row = this.row - 1;
} else if (key == Direction.Right) {
this.col = this.col + 1;
} else if (key == Direction.Down) {
this.row = this.row + 1;
} else if (key == Direction.Left) {
this.col = this.col - 1;
}
}
Bomberman.prototype.setDirection = function(direction) {
this.direction = direction;
}
Bomberman.prototype.bombermanSprite = new Image();
Bomberman.prototype.bombermanSprite.src = "green-player.png";
Bomberman.prototype.draw = function(ctx) {
var cordinate = Map.rowColToXY(this.row, this.col);
if (this.direction == Direction.Up) {
ctx.drawImage(this.bombermanSprite, 0, 77, 51, 77, cordinate.x, cordinate.y, 40, 36);
} else if (this.direction == Direction.Right) {
ctx.drawImage(this.bombermanSprite, 0, 0, 51, 77, cordinate.x, cordinate.y, 40, 36);
} else if (this.direction == Direction.Down) {
ctx.drawImage(this.bombermanSprite, 0, 154, 51, 77, cordinate.x, cordinate.y, 40, 36);
} else if (this.direction == Direction.Left) {
ctx.drawImage(this.bombermanSprite, 0, 231, 51, 77, cordinate.x, cordinate.y, 40, 36);
}
}
/*
* Bomb class
*/
function Bomb (col, row, power) {
this.col = col;
this.row = row;
this.power = power;
}
Bomb.prototype.sprite = new Image();
Bomb.prototype.sprite.src = "bomb.png";
Bomb.prototype.draw = function(ctx) {
var cordinate = Map.rowColToXY(this.row, this.col);
ctx.drawImage(this.sprite, 0, 0, 61, 60, cordinate.x, cordinate.y, 40, 36);
}
//Bootstap Game
var bootstrap = function() {
engine = new Engine();
requestAnimationFrame(engine.run);
};
//Bootstap Game (when page is loaded)
window.addEventListener("load", bootstrap, false);
</script>
</head>
<body>
<canvas id="myCanvas" width="640" height="480"></canvas>
</body>
</html>