public
Created

  • Download Gist
gistfile1.html
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 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
<!DOCTYPE html>
<html>
<head>
<title>Final</title>
 
<style type="text/css">
*{
 
padding:0px;
margin:0px;
}
 
#field {
 
height:500px;
width:500px;
background: #ff9;
float:left;
z-index: 1;
}
 
#rails {
 
height:450px;
width:450px;
background: #ff9;
position: absolute;
top:25px;
left:25px;
border: 1px solid black;
z-index: 2;
}
 
#player {
 
position: absolute;
height:50px;
width:50px;
z-index: 3;
}
 
#bullet {
 
position: absolute;
height:10px;
width:10px;
z-index: 3;
background-color: black;
 
}
 
#smallEnemy {
 
height:15px;
width:15px;
background-color: #C09;
 
}
 
#mediumEnemy {
 
height:50px;
width:50px;
background-color: #0f0;
}
 
#largeEnemy {
 
height:100px;
width:100px;
background-color: #ff0
 
}
 
#xlargeEnemy {
height:200px;
width:200px;
background-color: #00f;
}
 
.starter{background-color: black;}
.down{ background-color: #ff0;}
.up{background-color: #339}
.left{background-color:#ccf;}
.right{background-color:#6C3;}
.hit{visibility: none;}
 
 
 
 
 
 
</style>
</head>
<body>
 
<section id="field">
<div id="rails"></div>
<div id="player" class="starter"></div>
</section>
<section id="startField">
<button type="button" id="startBtn">START</button>
</section>
 
 
 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.timer.js"></script>
<script type="text/javascript">
 
var gameOver = false;
//declare some clean up variables
var Pleft, Ptop;
//declare some variables that will help determine where the bullet will start from
var bulletOriginY, bulletOriginX;
 
//start the game once you click the start button
$('#startBtn').click(function() {
//disable the button when it is clicked so you can't click it any more
$('#startBtn').attr('disabled', 'disabled');
//start the game function
game();
});
 
//----------- main game function --------------//
var game = function() {
 
//if the game is not over, run the main code
if (gameOver === false) {
 
//------------enemy set up--------------------//
 
//--------------------player movement and imaging--------------//
$(document).keydown(function(event) {
 
Ptop = $('#player').position().top;
Pleft = $('#player').position().left;
 
//firing function
var shoot = function() {
//determine which way the bullet will fire
if ($('#player').hasClass('down')) {
bulletOriginY = Ptop - 5;
bulletOriginX = Pleft + 23;
};
if ($('#player').hasClass('right')) {
bulletOriginY = Ptop + 23;
bulletOriginX = Pleft - 5;
};
if ($('#player').hasClass('left')) {
bulletOriginY = Ptop + 23;
bulletOriginX = Pleft + 46;
};
if ($('#player').hasClass('up')) {
bulletOriginY = Ptop + 46;
bulletOriginX = Pleft + 23;
};
//create the bullet
var bullet = $('<div>');
//give the bullet the right ID
bullet.attr({
id: 'bullet'
});
//set up the CSS of the bullet so it will spawn in the right spot
bullet.css({
'top': (bulletOriginY + 'px'),
'left': (bulletOriginX + 'px')
});
//append the bullet to the field
$('#field').append(bullet);
};
 
 
 
 
//----------------------MOVEMENT AND FIRING-------------------------//
//check if it's top is zero or it's left is zero
if (Ptop === 0 || Ptop === 450 || Pleft === 0 || Pleft === 450) {
 
//check if both are zero (TOP CORNER)
if (Ptop === 0 && Pleft === 0) {
switch (event.keyCode) {
case 68:
$('#player').animate({
left: '+=5px'
}, 10); // MOVE RIGHT
break;
case 83:
$('#player').animate({
top: '+=5px'
}, 10); // MOVE DOWN
break;
}
}
 
//check if top is zero only
if (Ptop === 0 && Pleft !== 0 && Pleft !== 450) {
 
//give the player the right class depending on where it is in the field
if (!$('#player').hasClass("up")) {
$('#player').removeClass("down left right");
$('#player').addClass("up");
}
switch (event.keyCode) {
case 65:
$('#player').animate({
left: '-=5px'
}, 10); // MOVE LEFT
break;
case 68:
$('#player').animate({
left: '+=5px'
}, 10); // MOVE RIGHT
break;
}
}
//check if left is zero only
if (Ptop !== 0 && Ptop !== 450 && Pleft === 0) {
//give the player the right class depending on where it is in the field
if (!$('#player').hasClass("left")) {
$('#player').removeClass("down right up");
$('#player').addClass("left");
}
switch (event.keyCode) {
case 87:
$('#player').animate({
top: '-=5px'
}, 10); // MOVE UP
break;
case 83:
$('#player').animate({
top: '+=5px'
}, 10); // MOVE DOWN
break;
}
}
//check to see if the player is in the bottom left corner
if (Ptop === 450 && Pleft === 0) {
 
switch (event.keyCode) {
case 87:
$('#player').animate({
top: '-=5px'
}, 10); // MOVE UP
break;
case 68:
$('#player').animate({
left: '+=5px'
}, 10); // MOVE RIGHT
break;
}
}
if (Ptop === 450 && Pleft !== 0 && Pleft !== 450) {
//give the player the right class depending on where it is in the field
if (!$('#player').hasClass("down")) {
$('#player').removeClass("left right up");
$('#player').addClass("down");
}
switch (event.keyCode) {
case 65:
$('#player').animate({
left: '-=5px'
}, 10); // MOVE LEFT
break;
case 68:
$('#player').animate({
left: '+=5px'
}, 10); // MOVE RIGHT
break;
}
}
if (Ptop === 450 && Pleft === 450) {
 
switch (event.keyCode) {
case 87:
$('#player').animate({
top: '-=5px'
}, 10); // MOVE UP
break;
case 65:
$('#player').animate({
left: '-=5px'
}, 10); // MOVE LEFT
break;
}
}
if (Ptop !== 450 && Ptop !== 0 && Pleft == 450) {
//give the player the right class depending on where it is in the field
if (!$('#player').hasClass("right")) {
$('#player').removeClass("left down up");
$('#player').addClass("right");
}
switch (event.keyCode) {
case 87:
$('#player').animate({
top: '-=5px'
}, 10); // MOVE UP
break;
case 83:
$('#player').animate({
top: '+=5px'
}, 10); // MOVE DOWN
break;
}
}
if (Ptop === 0 && Pleft === 450) {
 
switch (event.keyCode) {
case 83:
$('#player').animate({
top: '+=5px'
}, 10); // MOVE DOWN
break;
case 65:
$('#player').animate({
left: '-=5px'
}, 10); // MOVE LEFT
break;
}
}
}
});
}
}‚Äč
 
</script>
</body>
</html>
}
}
 
</script>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.