-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
200 lines (179 loc) · 6.45 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
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
<!DOCTYPE html>
<html>
<head>
<title>Super Fight Game!</title>
<link rel="stylesheet" href="assets/stylesheet.css" type="text/css" media="screen" />
<script type="text/javascript" src="assets/FrameTimer.js"></script>
<script type="text/javascript" src="assets/SpriteSheet.js"></script>
<script type="text/javascript" src="assets/Animation.js"></script>
<script type="text/javascript">
var canvas;
var ctx;
// Distance for each keystroke
var dx = 5;
var dy = 5;
// Initial coordinates of fighter
var x = 50;
var y = 50;
// Width and height of the canvas
var WIDTH = 600;
var HEIGHT = 400;
// For displaying different frames of the fighterImage spritesheet
var fighterImage;
var frame;
var timer = new FrameTimer();
var currentAnimation;
var sprites = new SpriteSheet({
width: 32,
height: 32,
sprites: [
// Right-facing
{ name: 'stand' },
{ name: 'walk_1', x: 0, y: 0 },
{ name: 'walk_2', x: 0, y: 0 },
{ name: 'run', x: 0, y: 0 },
{ name: 'jump', x: 0, y: 0 },
{ name: 'punch_1', x: 0, y: 0 },
{ name: 'punch_2', x: 0, y: 0 },
{ name: 'punch_3', x: 0, y: 0 },
{ name: 'hit_1', x: -256, y: 32 },
{ name: 'hit_2', x: -256, y: 32 },
{ name: 'die', x: -256, y: 32 },
// Left-facing
{ name: 'left_punch_3', x: -352, y: 64 },
{ name: 'left_punch_2', x: -352, y: 64 },
{ name: 'left_punch_1', x: -352, y: 64 },
{ name: 'left_jump', x: -352, y: 64 },
{ name: 'left_run', x: -352, y: 64 },
{ name: 'left_walk_2', x: -352, y: 64 },
{ name: 'left_walk_1', x: -352, y: 64 },
{ name: 'left_stand', x: -352, y: 64 },
{ name: 'left_hit_1', x: -768, y: 96 },
{ name: 'left_hit_2', x: -768, y: 96 },
{ name: 'left_die', x: -768, y: 96 }
]
});
// Animations
var stand = new Animation([
{ sprite: 'stand', time: 0.1 }
], sprites);
var walk = new Animation([
{ sprite: 'walk_1', time: 0.2 },
{ sprite: 'stand', time: 0.2 },
{ sprite: 'walk_2', time: 0.2 },
{ sprite: 'stand', time: 0.2 }
], sprites);
var doItAll = new Animation([
{ sprite: 'stand', time: 0.2 },
{ sprite: 'walk_1', time: 0.2 },
{ sprite: 'walk_2', time: 0.2 },
{ sprite: 'run', time: 0.2 },
{ sprite: 'jump', time: 0.2 },
{ sprite: 'punch_1', time: 0.05 },
{ sprite: 'punch_2', time: 0.05 },
{ sprite: 'punch_3', time: 0.2 },
{ sprite: 'hit_1', time: 0.2 },
{ sprite: 'hit_2', time: 0.2 },
{ sprite: 'die', time: 1.0 }
], sprites);
var leftDoItAll = new Animation([
{ sprite: 'left_stand', time: 0.5 },
{ sprite: 'left_walk_1', time: 0.5 },
{ sprite: 'left_walk_2', time: 0.5 },
{ sprite: 'left_run', time: 0.5 },
{ sprite: 'left_jump', time: 0.5 },
{ sprite: 'left_punch_1', time: 0.5 },
{ sprite: 'left_punch_2', time: 0.5 },
{ sprite: 'left_punch_3', time: 0.5 },
{ sprite: 'left_hit_1', time: 0.5 },
{ sprite: 'left_hit_2', time: 0.5 },
{ sprite: 'left_die', time: 1.0 }
], sprites);
var jump = new Animation([
{ sprite: 'stand', time: 0.2 },
{ sprite: 'jump', time: 0.4 }
], sprites);
var getHit = new Animation([
{ sprite: 'hit_1', time: 0.2 },
{ sprite: 'hit_2', time: 0.2 },
{ sprite: 'die', time: 0.2 }
], sprites);
var punch = new Animation([
{ sprite: 'punch_1', time: 0.05 },
{ sprite: 'punch_2', time: 0.05 },
{ sprite: 'punch_3', time: 0.2 }
], sprites);
function draw() {
clear();
// TODO: Somehow, frame is undefined on the very first time this is called.
if (typeof frame != "undefined")
ctx.drawImage(fighterImage, frame.x, frame.y, 32, 32, x, y, 32, 32);
}
function clear() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
}
function init() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
currentAnimation = stand;
fighterImage = new Image();
fighterImage.src = 'assets/fighter_complete.gif';
fighterImage.onload = function() {
setInterval(function(){
currentAnimation.animate(timer.getSeconds());
frame = currentAnimation.getSprite();
timer.tick();
}, 5);
};
return setInterval(draw, 10);
}
function doKeyDown(evt){
switch (evt.keyCode) {
case 38: /* Up arrow was pressed */
if (y - dy > 0){
y -= dy;
}
currentAnimation = jump;
break;
case 40: /* Down arrow was pressed */
if (y + dy < (HEIGHT - 60)){
y += dy;
}
currentAnimation = stand;
break;
case 37: /* Left arrow was pressed */
if (x - dx > 0){
x -= dx;
}
currentAnimation = walk;
break;
case 39: /* Right arrow was pressed */
if (x + dx < (WIDTH - 60)){
x += dx;
}
currentAnimation = walk;
break;
case 90: /* "Z" was pressed */
timer.reset();
currentAnimation = punch;
break;
}
}
window.onload = function() {
init();
window.addEventListener('keydown',doKeyDown,true);
};
</script>
</head>
<body style="">
<h1><marquee behavior='alternate'><blink>Super Fight Game!</blink></marquee></h1>
<canvas id="canvas" class="rounded_corners" width="600" height="400"></canvas>
<audio controls="controls" autoplay="autoplay" loop="loop" style="margin: auto;display:block;">
<source src="assets/8_bit_Boss_Battle_3.mp3" type="audio/mp3" />
Your browser does not support the audio tag.
</audio>
<div class="footer rounded_corners">
Music by <a href="http://eliteferrex.newgrounds.com/">EliteFerrex</a>
</div>
</body>
</html>