-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
301 lines (276 loc) · 7.89 KB
/
index.html
File metadata and controls
301 lines (276 loc) · 7.89 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<!--
ICONS (Players, Item) by sbed http://opengameart.org/content/95-game-icons
Followed a tutorial for some of this, it can be found here: http://html5gamedev.samlancashire.com/
-->
<html>
<head>
<link rel="stylesheet" type="text/css" href="style1.css" media="screen" />
<title>HTML5 game by Brandon Duncan</title>
</head>
<body>
<div class="box">
<h3>HTML5 Test Game:</h3>
<hr>
<h5><img src="player1icon.png"></img> Player 1: ARROW KEYS</h5>
<img src="player2icon.png"></img> Player 2: WASD<br>
<br>
<hr>
Collect the + in the middle<br>
to Score a point! Who can<br>
Score the most points?<br>
<br>
Every time you catch a +<br>
a new one will appear<br>
randomly somewhere within<br>
the dark box!<br>
<br>
<b>There's no win-condition</b><br>
but I intended it to end<br>
when the black box reaches<br>
the size of zero.<br>
<hr>
</div>
<canvas id="canvas" class="thingo"></canvas>
<div class="box">
<h3>Game by Brandon Duncan</h3>
<hr>
Hey I made this in a few hours<br>
on Nov. 20th 2013 as a look into<br>
HTML5 games.<br>
<br>
Go check out my Website:<br>
<b><a href="http://moltanem2000.github.io" target="_blank">MOLTANEM2000.GITHUB.IO</a></b><br>
<br>
Feel free to check out the source<br>
(Just right click -> view source)<br>
<hr>
</div>
<script type="text/javascript">
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 700;
canvas.height = 700;
//LOAD IMAGES
var CharacterSpriteOne = {
loaded: false,
image: new Image(),
tileWidth: 24,
tileHeight: 24
};
var CharacterSpriteTwo = {
loaded: false,
image: new Image(),
tileWidth: 24,
tileHeight: 24
};
var itemtile = {
loaded: false,
image: new Image(),
tileWidth: 24,
tileHeight: 24
};
var background = {
loaded: false,
image: new Image(),
tileWidth: 800,
tileHeight: 800
};
CharacterSpriteOne.image.onload = function() {
CharacterSpriteOne.loaded = true;
}
CharacterSpriteTwo.image.onload = function() {
CharacterSpriteTwo.loaded = true;
}
itemtile.image.onload = function() {
itemtile.loaded = true;
}
background.image.onload = function() {
background.loaded = true;
}
CharacterSpriteTwo.image.src = 'player1.png';
CharacterSpriteOne.image.src = 'player2.png';
itemtile.image.src = 'item.png';
background.image.src = 'background.png';
//INSTANIATE DEM OBJECTS
var CharacterOne = {
width: 24,
height: 24,
x: 96 - 24/2,
y: canvas.height/2 - 24/2,
speed: 12,
xspeed: 0,
yspeed: 0,
state: 0
};
var CharacterTwo = {
width: 24,
height: 24,
x: canvas.width -96 -24/2,
y: canvas.height/2 - 24/2,
speed: 12,
xspeed: 0,
yspeed: 0,
state: 2
};
var item = {
width: 24,
height: 24,
x: canvas.width/2 - 24/2,
y: canvas.height/2 - 24/2,
};
var keysDown = {};
window.addEventListener('keydown', function(e) {
keysDown[e.keyCode] = true;
});
window.addEventListener('keyup', function(e) {
delete keysDown[e.keyCode];
});
//AW YEAH
//GameVars
var itemCounterOne = 0;
var itemCounterTwo = 0;
var bordersize = 48;
var speedconst = 2;
var slowconst = 1.25;
var bounceconst = 4;
function collisionBox(A,B) {
return (
A.x < B.x + B.width &&
A.x + A.width > B.x &&
A.y < B.y + B.height &&
A.y + A.height > B.y
);
}
function update(mod) {
//oh god I should really do something about the following awful code but I'm in too deep now!
//Player 1 movement
if (65 in keysDown) {
CharacterOne.state = 2; //left
if (CharacterOne.xspeed > -CharacterOne.speed) {CharacterOne.xspeed -= speedconst;}
}
if (87 in keysDown) {
CharacterOne.state = 3; //up
if (CharacterOne.yspeed > -CharacterOne.speed) {CharacterOne.yspeed -= speedconst;}
}
if (68 in keysDown) {
CharacterOne.state = 0; //right
if (CharacterOne.xspeed < CharacterOne.speed) {CharacterOne.xspeed += speedconst;}
}
if (83 in keysDown) {
CharacterOne.state = 1; //down
if (CharacterOne.yspeed < CharacterOne.speed) {CharacterOne.yspeed += speedconst;}
}
CharacterOne.x += CharacterOne.xspeed;
CharacterOne.y += CharacterOne.yspeed;
CharacterOne.xspeed /= slowconst;
CharacterOne.yspeed /= slowconst;
//can't leave room
if (CharacterOne.x > canvas.width && CharacterOne.xspeed > 0) {CharacterOne.xspeed *= -bounceconst}
if (CharacterOne.x < 0 && CharacterOne.xspeed < 0) {CharacterOne.xspeed *= -bounceconst}
if (CharacterOne.y > canvas.height&& CharacterOne.yspeed > 0) {CharacterOne.yspeed *= -bounceconst}
if (CharacterOne.y < 0 && CharacterOne.yspeed < 0) {CharacterOne.yspeed *= -bounceconst}
//Abandon hope all ye who enjoy modularity
//Player 2 movement
if (37 in keysDown) {
CharacterTwo.state = 2; //left
if (CharacterTwo.xspeed > -CharacterTwo.speed) {CharacterTwo.xspeed -= speedconst;}
}
if (38 in keysDown) {
CharacterTwo.state = 3; //up
if (CharacterTwo.yspeed > -CharacterTwo.speed) {CharacterTwo.yspeed -= speedconst;}
}
if (39 in keysDown) {
CharacterTwo.state = 0; //right
if (CharacterTwo.xspeed < CharacterTwo.speed) {CharacterTwo.xspeed += speedconst;}
}
if (40 in keysDown) {
CharacterTwo.state = 1; //down
if (CharacterTwo.yspeed < CharacterTwo.speed) {CharacterTwo.yspeed += speedconst;}
}
CharacterTwo.x += CharacterTwo.xspeed;
CharacterTwo.y += CharacterTwo.yspeed;
CharacterTwo.xspeed /= slowconst;
CharacterTwo.yspeed /= slowconst;
if (CharacterTwo.x > canvas.width && CharacterTwo.xspeed > 0) {CharacterTwo.xspeed *= -bounceconst}
if (CharacterTwo.x < 0 && CharacterTwo.xspeed < 0) {CharacterTwo.xspeed *= -bounceconst}
if (CharacterTwo.y > canvas.height&& CharacterTwo.yspeed > 0) {CharacterTwo.yspeed *= -bounceconst}
if (CharacterTwo.y < 0 && CharacterTwo.yspeed < 0) {CharacterTwo.yspeed *= -bounceconst}
if (collisionBox(CharacterOne,item)) {
item.x = bordersize + (Math.random() * (canvas.width - 2*bordersize));
item.y = bordersize + (Math.random() * (canvas.height - 2*bordersize));
itemCounterOne ++;
bordersize += 4;
}
if (collisionBox(CharacterTwo,item)) {
item.x = bordersize + (Math.random() * (canvas.width - 2*bordersize));
item.y = bordersize + (Math.random() * (canvas.height - 2*bordersize));
itemCounterTwo ++;
bordersize += 4;
}
}
function render() {
//Draw background
if (background.loaded) {
ctx.drawImage(
background.image,
0,
0
);
}
ctx.fillStyle = 'rgba(0,0,0,0.5)';
ctx.fillRect(bordersize,bordersize,canvas.width - 2*bordersize,canvas.height-2*bordersize);
//Draw Item
if (itemtile.loaded) {
ctx.drawImage(
itemtile.image,
Math.round(item.x),
Math.round(item.y)
);
}
//Draw Player1
if (CharacterSpriteOne.loaded) {
ctx.drawImage(
CharacterSpriteOne.image,
CharacterOne.state * CharacterSpriteOne.tileWidth,
0,
CharacterOne.width,
CharacterOne.height,
Math.round(CharacterOne.x),
Math.round(CharacterOne.y),
CharacterOne.width,
CharacterOne.height
);
}
//Draw Player2
if (CharacterSpriteTwo.loaded) {
ctx.drawImage(
CharacterSpriteTwo.image,
CharacterTwo.state * CharacterSpriteTwo.tileWidth,
0,
CharacterTwo.width,
CharacterTwo.height,
Math.round(CharacterTwo.x),
Math.round(CharacterTwo.y),
CharacterTwo.width,
CharacterTwo.height
);
}
//Draw Scores
ctx.font = '36pt Arail';
ctx.textBaseLine = 'top';
ctx.fillStyle = '#fD0';
ctx.fillText(itemCounterOne, 10, 48);
ctx.fillStyle = '#0Cf';
ctx.fillText(itemCounterTwo, canvas.width-48, 48)
}
function run() {
update((Date.now() - time) / 1000);
render();
time = Date.now();
}
var time = Date.now();
setInterval(run, 10);
</script>
</body>
</html>