Added some more sounds and animations

Added a death screen
reworked the design of pip
Added a placeholder zombie
Added a hitbox display mode
This commit is contained in:
rain
2023-10-31 10:58:43 -05:00
parent 0536cf2a8b
commit 6226d33975
12 changed files with 215 additions and 54 deletions
+2 -2
View File
@@ -4,8 +4,8 @@ body, html {
} }
canvas { canvas {
width: 960px; width: auto;
height: 540px; height: 90%;
image-rendering: pixelated; image-rendering: pixelated;
Binary file not shown.

After

Width:  |  Height:  |  Size: 724 B

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 629 B

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

+6
View File
@@ -2,6 +2,7 @@
<head> <head>
<title>Halloween Game!</title> <title>Halloween Game!</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="icon" type="image/png" href="img/pip.png">
<link rel="stylesheet" href="css/game.css"> <link rel="stylesheet" href="css/game.css">
</head> </head>
<body onload="startGame()"> <body onload="startGame()">
@@ -12,6 +13,8 @@
</div> </div>
<!--<button id="start" onclick="startGame();">Restart Game</button>--> <!--<button id="start" onclick="startGame();">Restart Game</button>-->
<img class="sprite" id="exclaimation" src="img/exclaimation.png">
<img class="sprite" id="heart" src="img/heart.png"> <img class="sprite" id="heart" src="img/heart.png">
<img class="sprite" id="player" src="img/pip.png"> <img class="sprite" id="player" src="img/pip.png">
<img class="sprite" id="down_player" src="img/pip_down.png"> <img class="sprite" id="down_player" src="img/pip_down.png">
@@ -25,5 +28,8 @@
<img class="sprite" id="right_ghost" src="img/ghost_right.png"> <img class="sprite" id="right_ghost" src="img/ghost_right.png">
<img class="sprite" id="left_ghost" src="img/ghost_left.png"> <img class="sprite" id="left_ghost" src="img/ghost_left.png">
<img class="sprite" id="right_zombie" src="img/zombie_right.png">
<img class="sprite" id="left_zombie" src="img/zombie_left.png">
</body> </body>
</html> </html>
+207 -52
View File
@@ -11,15 +11,17 @@ var pHeight = 30;
var ghostS = 20; var ghostS = 20;
var drawHitboxes = true;
function startGame() { function startGame() {
pip = new player(pWidth, pHeight, 0 + pWidth*2, canvasHeight - pHeight); pip = new player(pWidth, pHeight, 0 + pWidth*2, canvasHeight - pHeight-30);
ghost = new enemy(ghostS, ghostS, 50, 50, pip, "ghost"); ghost = new fairy(ghostS, ghostS, 50, 50, pip);
enemies.push( new zombie(30, 30, 100, 100) );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, pip, "ghost") ); //enemies.push( new enemy(ghostS, ghostS, 50, 50, pip, "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[0], "ghost") ); //enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[0], "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[1], "ghost") ); //enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[1], "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[2], "ghost") ); //enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[2], "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[3], "ghost") ); //enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[3], "ghost") );
pip.gravity = 1;
overlay = new interface(pip); overlay = new interface(pip);
@@ -29,6 +31,7 @@ function startGame() {
heal = new sound("sfx/pip_heal.wav"); heal = new sound("sfx/pip_heal.wav");
duck = new sound("sfx/pip_duck.wav"); duck = new sound("sfx/pip_duck.wav");
squeak = new sound("sfx/ghost_squeak.wav"); squeak = new sound("sfx/ghost_squeak.wav");
death = new sound("sfx/death.wav");
game.start(); game.start();
} }
@@ -44,13 +47,33 @@ var game = {
this.frameNo = 0; this.frameNo = 0;
this.interval = setInterval(updateGame, 20); this.interval = setInterval(updateGame, 20);
window.addEventListener('keydown', function (e) { this.keydown = function (e) {
game.keys = (game.keys || []); game.keys = (game.keys || []);
game.keys[e.keyCode] = true; game.keys[e.keyCode] = true;
}) };
window.addEventListener('keyup', function (e) { this.keyup = function (e) {
game.keys[e.keyCode] = false; game.keys[e.keyCode] = false;
}) };
window.addEventListener('keydown', this.keydown);
window.addEventListener('keyup', this.keyup);
},
end : function() {
//this.clear();
window.clearInterval(this.interval);
death.play();
let deathMessage = "YOU DIED";
ctx.font = "24px serif";
ctx.textAlign = "center";
ctx.fillStyle = 'black';
let padding = 20;
let width = ctx.measureText(deathMessage).width+padding*2;
let height = ctx.measureText(deathMessage).height;
ctx.fillRect(game.canvas.width/2-width/2, game.canvas.height/2-(24+padding/2), width, parseInt(ctx.font, padding));
ctx.fillStyle = "orange";
ctx.fillText(deathMessage, this.canvas.width/2, this.canvas.height/2);
}, },
clear : function() { clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
@@ -59,7 +82,8 @@ var game = {
function player(width, height, x, y) { function player(width, height, x, y) {
this.hearts = 3; this.hearts = 3;
this.strength = 12; this.speed = 12;
this.iframes = 80;
this.ducking = false; this.ducking = false;
this.score = 0; this.score = 0;
this.width = width; this.width = width;
@@ -68,22 +92,40 @@ function player(width, height, x, y) {
this.speedY = 0; this.speedY = 0;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.gravity = 0; this.gravity = 1;
this.update = function() { this.update = function() {
ctx = game.context; ctx = game.context;
let img = document.getElementById("player"); let img = document.getElementById("player");
if(this.speedY >= -(this.strength/2) && this.speedY < 0) { if(this.speedY >= -(this.speed/2) && this.speedY < 0) {
img = document.getElementById("up_player"); img = document.getElementById("up_player");
}else if(this.speedY <= this.strength && this.speedY > (this.strength/2)) { }else if(this.speedY <= this.speed && this.speedY > (this.speed/2)) {
img = document.getElementById("down_player"); img = document.getElementById("down_player");
}else if(this.speedX < 0) { }else if(this.speedX < 0) {
img = document.getElementById("left_player"); img = document.getElementById("left_player");
} else if (this.speedX > 0) { } else if (this.speedX > 0) {
img = document.getElementById("right_player"); img = document.getElementById("right_player");
} }
// Check if taking damage
// Decay iframes
if(this.iframes > 0) {
if(game.frameNo % 5) {
ctx.globalAlpha = 0.4;
}
this.iframes -= 1;
}
if(drawHitboxes) {
// T R B L
// 0 1 2 3
let hitbox = this.hitbox();
ctx.fillStyle = 'blue';
ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
}
ctx.drawImage(img, this.x, this.y, this.width, this.height); ctx.drawImage(img, this.x, this.y, this.width, this.height);
ctx.globalAlpha = 1;
} }
this.move = function() { this.move = function() {
@@ -93,7 +135,7 @@ function player(width, height, x, y) {
// Jump // Jump
if (game.keys && game.keys[38]) { if (game.keys && game.keys[38]) {
if(pip.y >= bottom) { if(pip.y >= bottom) {
this.speedY = -this.strength; this.speedY = -this.speed;
jump.play(); jump.play();
} }
} }
@@ -101,7 +143,7 @@ function player(width, height, x, y) {
// Duck // Duck
if (game.keys && game.keys[40] || this.ducking) { if (game.keys && game.keys[40] || this.ducking) {
if(pip.y < bottom) { if(pip.y < bottom) {
pip.speedY = this.strength; pip.speedY = this.speed;
} else { } else {
if (pip.height != pHeight / 2) { if (pip.height != pHeight / 2) {
duck.play(); duck.play();
@@ -117,25 +159,33 @@ function player(width, height, x, y) {
// Right // Right
if (game.keys && game.keys[39]) { if (game.keys && game.keys[39]) {
pip.speedX = this.strength / 2; pip.speedX = this.speed / 2;
} else { } else {
// Slow Down // Slow Down
if(pip.speedX > 0) { if(pip.speedX > 0) {
pip.speedX -= this.strength/8; pip.speedX -= this.speed/8;
} }
} }
// Left // Left
if (game.keys && game.keys[37]) { if (game.keys && game.keys[37]) {
pip.speedX = 0 - this.strength / 2; pip.speedX = 0 - this.speed / 2;
} else { } else {
// Slow Down // Slow Down
if(pip.speedX < 0) { if(pip.speedX < 0) {
pip.speedX += this.strength/8; pip.speedX += this.speed/8;
} }
} }
} }
this.hitbox = function() {
let top = this.y + this.height * .45;
let right = this.x + this.width;
let bottom = this.y + this.height * .55;
let left = this.x;
return [top, right, bottom, left];
}
this.newPos = function() { this.newPos = function() {
// Calc Y // Calc Y
this.speedY += this.gravity; this.speedY += this.gravity;
@@ -153,9 +203,18 @@ function player(width, height, x, y) {
} }
} }
this.hurt = function() { this.hurt = function() {
// Detect if ya hit sometin // Do the hurt player things
this.hearts -= 1; // Check if immune
hurt.play(); if(this.iframes > 0) {
return;
}
this.iframes = 40;
if(this.hearts > 0) {
this.hearts -= 1;
hurt.play();
return;
}
game.end();
} }
this.heal = function() { this.heal = function() {
// Detect if ya hit sometin // Detect if ya hit sometin
@@ -166,9 +225,8 @@ function player(width, height, x, y) {
} }
} }
function enemy(width, height, x, y, target, type) { function fairy(width, height, x, y, target) {
this.hearts = 3; this.speed = 4;
this.strength = 4;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.speedX = 0; this.speedX = 0;
@@ -176,8 +234,8 @@ function enemy(width, height, x, y, target, type) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.target = target; this.target = target;
this.type = type;
this.hiding = false; this.hiding = false;
this.scared = 0;
this.gravity = 0; this.gravity = 0;
this.update = function() { this.update = function() {
@@ -186,36 +244,37 @@ function enemy(width, height, x, y, target, type) {
// Control the transition point between animations // Control the transition point between animations
let animateDist = .5; let animateDist = .5;
let img = document.getElementById(`${this.type}`); let img = document.getElementById(`ghost`);
if(this.speedY > animateDist && (this.speedX > -animateDist && this.speedX < animateDist)) { if(this.speedY > animateDist && (this.speedX > -animateDist && this.speedX < animateDist)) {
img = document.getElementById(`down_${this.type}`); img = document.getElementById(`down_ghost`);
}else if(this.speedY < -animateDist && (this.speedX > -animateDist && this.speedX < animateDist)) { }else if(this.speedY < -animateDist && (this.speedX > -animateDist && this.speedX < animateDist)) {
img = document.getElementById(`up_${this.type}`); img = document.getElementById(`up_ghost`);
}else if(this.speedX < -animateDist) { }else if(this.speedX < -animateDist) {
img = document.getElementById(`left_${this.type}`); img = document.getElementById(`left_ghost`);
} else if (this.speedX > animateDist) { } else if (this.speedX > animateDist) {
img = document.getElementById(`right_${this.type}`); img = document.getElementById(`right_ghost`);
} }
ctx.drawImage(img, this.x, this.y, this.width, this.height); ctx.drawImage(img, this.x, this.y, this.width, this.height);
if(this.scared > 0) {
let img = document.getElementById(`exclaimation`);
let topCenterX = this.x+this.width/2.5;
let topCenterY = this.y-this.height/2;
ctx.drawImage(img, topCenterX, topCenterY, this.width/3, this.height/3);
this.scared -= 1;
}
} }
this.move = function() { this.move = function() {
//var distX = this.speedX = (pip.x - this.x)
//var distY = this.speedY = (pip.y - this.y)
// move at a percentage of strength based on distance
//this.speedX = ((distX) / 100) * this.strength;
//this.speedY = ((distY) / 100) * this.strength;
// if the ghost is hiding, only move after player gets a certain distance away // if the ghost is hiding, only move after player gets a certain distance away
var distance = Math.sqrt(Math.pow(this.x - this.target.x, 2) + Math.pow(this.y - this.target.y, 2)); var distance = Math.sqrt(Math.pow(this.x - this.target.x, 2) + Math.pow(this.y - this.target.y, 2));
console.log(distance);
if(distance < 1) { if(distance < 2) {
this.hiding = true; this.hiding = true;
return; return;
} else if(distance > 100 && this.hiding) { } else if(distance > 100 && this.hiding) {
this.hiding = false; this.hiding = false;
this.scared = 20;
squeak.play(); squeak.play();
} }
@@ -224,32 +283,106 @@ function enemy(width, height, x, y, target, type) {
this.speedY = 0; this.speedY = 0;
} else { } else {
// Determine where the player will be and move there instead // Determine where the player will be and move there instead
var distX = this.speedX = this.target.x + (this.target.speedX / 100 * this.target.strength) - this.x; var distX = this.speedX = this.target.x + (this.target.speedX / 100 * this.target.speed) - this.x;
var distY = this.speedY = this.target.y + (this.target.speedY / 100 * this.target.strength) - this.y; var distY = this.speedY = this.target.y + (this.target.speedY / 100 * this.target.speed) - this.y;
this.speedX = ((distX) / 100) * this.strength; this.speedX = ((distX) / 100) * this.speed;
this.speedY = ((distY) / 100) * this.strength; this.speedY = ((distY) / 100) * this.speed;
} }
} }
this.newPos = function() { this.newPos = function() {
// Calc Y // Calc Y
// Add some jiggle
//var jiggle = Math.floor(Math.random() * 2)/2;
//var sign = Math.floor(Math.random() * 2)?1:-1;
//this.speedY += sign*jiggle;
this.y += this.speedY; this.y += this.speedY;
// Calc X // Calc X
this.x += this.speedX; this.x += this.speedX;
} }
} }
function zombie(width, height, x, y) {
this.hearts = 3;
this.speed = 1;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 1;
this.update = function() {
ctx = game.context;
let img = document.getElementById(`left_zombie`);
if(this.speedX < 0) {
img = document.getElementById(`left_zombie`);
} else {
img = document.getElementById(`right_zombie`);
}
if(drawHitboxes) {
// T R B L
let hitbox = this.hitbox();
ctx.fillStyle = 'green';
ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
}
ctx.drawImage(img, this.x, this.y, this.width, this.height);
}
this.move = function() {
let bottom = game.canvas.height - this.height;
let direction = 0;
if(game.frameNo % 40 == 1) {
direction = Math.floor(Math.random() * 2)?-1:1;
} else if(this.x >= game.canvas.width-10) {
// Move Move Left
direction = -1;
} else if(this.x <= 0+10) {
// Move Move Right
direction = 1;
} else {
// Ruin the equation to keep the same state
direction = this.speedX/this.speed;
}
this.speedX = direction * this.speed;
if(this.y < bottom) {
this.speedY += this.gravity;
}
this.hitBottom();
}
this.hitBottom = function() {
var bottom = game.canvas.height - this.height;
if(this.y > bottom){
this.y = bottom;
this.speedY = 0;
}
}
this.hitbox = function() {
let top = this.y + this.height * .1;
let right = this.x + this.width*.8;
let bottom = this.y + this.height * .9;
let left = this.x + this.width*.2;
return [top, right, bottom, left];
}
this.newPos = function() {
// Calc X
this.x += this.speedX;
this.y += this.speedY;
}
}
function interface(pip) { function interface(pip) {
this.hearts = pip.hearts; this.hearts = pip.hearts;
this.width = 30; this.width = 15;
this.height = 30; this.height = 15;
this.padding = 5; this.padding = 5;
this.x = canvasWidth; this.x = canvasWidth;
this.y = 0; this.y = 0;
@@ -278,6 +411,9 @@ function updateGame() {
enemies[i].move(); enemies[i].move();
enemies[i].newPos(); enemies[i].newPos();
enemies[i].update(); enemies[i].update();
if(collision.collide(pip.hitbox(), enemies[i].hitbox()) || collision.inside(pip.hitbox(), enemies[i].hitbox())) {
pip.hurt();
}
} }
ghost.move(); ghost.move();
@@ -291,7 +427,6 @@ function updateGame() {
overlay.update(); overlay.update();
} }
function sound(src) { function sound(src) {
this.sound = document.createElement("audio"); this.sound = document.createElement("audio");
this.sound.src = src; this.sound.src = src;
@@ -305,4 +440,24 @@ function sound(src) {
this.stop = function(){ this.stop = function(){
this.sound.pause(); this.sound.pause();
} }
} }
var collision = {
collide: function (el1, el2) {
return !(
el1[0] > el2[2] ||
el1[1] < el2[3] ||
el1[2] < el2[0] ||
el1[3] > el2[1]
);
},
inside: function (el1, el2) {
return (
((el2[0] <= el1[0]) && (el1[0] <= el2[2])) &&
((el2[0] <= el1[2]) && (el1[2] <= el2[2])) &&
((el2[3] <= el1[3]) && (el1[3] <= el2[1])) &&
((el2[3] <= el1[1]) && (el1[1] <= el2[1]))
);
}
};
BIN
View File
Binary file not shown.