commit cf9296a418d9e6800714be160a3f193a21915fa8 Author: rain Date: Thu Oct 26 17:01:46 2023 -0500 Initial Play Area stuff created diff --git a/css/game.css b/css/game.css new file mode 100644 index 0000000..503e339 --- /dev/null +++ b/css/game.css @@ -0,0 +1,32 @@ +body, html { + color: white; + background: black; +} + +canvas { + width: 960px; + height: 540px; + + image-rendering: pixelated; + + display: flex; + margin: 0 auto; + + border:1px solid #d3d3d3; + background: rgb(92,88,164); + background: linear-gradient(180deg, rgba(92,88,164,1) 30%, rgba(156,90,188,1) 100%); +} + +h1,h2,h3 { + text-align: center; +} + +button { + display: flex; + padding: 5px; + margin: 10px auto; +} + +.sprite { + display: none; +} \ No newline at end of file diff --git a/img/dark-sky.jpg b/img/dark-sky.jpg new file mode 100644 index 0000000..6262149 Binary files /dev/null and b/img/dark-sky.jpg differ diff --git a/img/heart.png b/img/heart.png new file mode 100644 index 0000000..3eec6c0 Binary files /dev/null and b/img/heart.png differ diff --git a/img/pip.png b/img/pip.png new file mode 100644 index 0000000..e7b4733 Binary files /dev/null and b/img/pip.png differ diff --git a/img/pip_down.png b/img/pip_down.png new file mode 100644 index 0000000..d8c1531 Binary files /dev/null and b/img/pip_down.png differ diff --git a/img/pip_left.png b/img/pip_left.png new file mode 100644 index 0000000..12a67ac Binary files /dev/null and b/img/pip_left.png differ diff --git a/img/pip_right.png b/img/pip_right.png new file mode 100755 index 0000000..495d546 Binary files /dev/null and b/img/pip_right.png differ diff --git a/img/pip_up.png b/img/pip_up.png new file mode 100644 index 0000000..b13b954 Binary files /dev/null and b/img/pip_up.png differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..2b18c7f --- /dev/null +++ b/index.html @@ -0,0 +1,23 @@ + + + Halloween Game! + + + + + +

The Spooky Adventures of Pip

+
+ +
+ + + + + + + + + + + \ No newline at end of file diff --git a/js/game.js b/js/game.js new file mode 100644 index 0000000..a0e626d --- /dev/null +++ b/js/game.js @@ -0,0 +1,193 @@ +var pip; +var myObstacles = []; +var myScore; + +// Define Global Sounds +var theme; +var jump; + +var canvasWidth = 480; +var canvasHeight = 270; +var pWidth = 50; +var pHeight = 50; + +function startGame() { + pip = new player(pWidth, pHeight, 0 + pWidth*2, canvasHeight - pHeight); + pip.gravity = 1; + + overlay = new interface(pip); + + // Add sound effects and music + jump = new sound("sfx/pip_jump.wav"); + hurt = new sound("sfx/pip_hurt.wav"); + duck = new sound("sfx/pip_duck.wav"); + //theme = new sound("sfx/theme.wav"); + //theme.play(); + + game.start(); +} + +var game = { + canvas : document.createElement("canvas"), + start : function() { + this.canvas.width = 480; + this.canvas.height = 270; + this.context = this.canvas.getContext("2d"); + document.getElementById("game").insertBefore(this.canvas, document.getElementById("game").childNodes[0]); + + this.frameNo = 0; + this.interval = setInterval(updateGame, 20); + + window.addEventListener('keydown', function (e) { + game.keys = (game.keys || []); + game.keys[e.keyCode] = true; + }) + window.addEventListener('keyup', function (e) { + game.keys[e.keyCode] = false; + }) + }, + clear : function() { + this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); + } +} + +function player(width, height, x, y) { + this.hearts = 3; + this.score = 0; + this.width = width; + this.height = height; + this.strength = 16; + this.speedX = 0; + this.speedY = 0; + this.x = x; + this.y = y; + this.gravity = 0; + + this.update = function() { + ctx = game.context; + + let img = document.getElementById("player"); + if(this.speedY >= -(this.strength/2) && this.speedY < 0) { + img = document.getElementById("up_player"); + }else if(this.speedY <= this.strength && this.speedY > (this.strength/2)) { + img = document.getElementById("down_player"); + }else if(this.speedX < 0) { + img = document.getElementById("left_player"); + } else if (this.speedX > 0) { + img = document.getElementById("right_player"); + } + ctx.drawImage(img, this.x, this.y, this.width, this.height); + } + + this.newPos = function() { + // Calc Y + this.speedY += this.gravity; + this.y += this.speedY; + this.hitBottom(); + + // Calc X + this.x += this.speedX; + } + this.hitBottom = function() { + var bottom = game.canvas.height - this.height; + if(this.y > bottom){ + this.y = bottom; + this.speedY = 0; + } + } +} + +function interface(pip) { + this.hearts = pip.hearts; + this.width = 30; + this.height = 30; + this.padding = 5; + this.x = canvasWidth; + this.y = 0; + + this.update = function() { + ctx = game.context; + let img = document.getElementById("heart"); + + for(var i = 1;i<=this.hearts;i++){ + let offset = i*(this.padding+this.width); + ctx.drawImage(img, this.x - offset, this.y+this.padding, this.width, this.height); + } + } +} + +function updateGame() { + game.clear(); + game.frameNo += 1; + + //if (game.keys && game.keys[37]) {pip.speedX = -10; } + //if (game.keys && game.keys[39]) {pip.speedX = 10; } + + var bottom = game.canvas.height - pip.height; + var strength = 16; // Must be even number + + // Jump + if (game.keys && game.keys[38]) { + if(pip.y >= bottom) { + pip.speedY = -strength; + jump.play(); + } + } + // Duck + if (game.keys && game.keys[40]) { + if(pip.y < bottom) { + pip.speedY = strength; + } else { + if (pip.height != pHeight / 2) { + duck.play(); + } + // Ducking position + pip.height = pHeight / 2; + pip.y += pHeight / 2; + } + } else { + // Exit Ducking Position + pip.height = pHeight; + } + + // Right + if (game.keys && game.keys[39]) { + pip.speedX = strength / 2; + } else { + // Slow Down + if(pip.speedX > 0) { + pip.speedX -= strength/8; + } + } + + // Left + if (game.keys && game.keys[37]) { + pip.speedX = 0 - strength / 2; + } else { + // Slow Down + if(pip.speedX < 0) { + pip.speedX += strength/8; + } + } + + pip.newPos(); + // Update his pos to calc damage before his position + overlay.update(); + pip.update(); +} + + +function sound(src) { + this.sound = document.createElement("audio"); + this.sound.src = src; + this.sound.setAttribute("preload", "auto"); + this.sound.setAttribute("controls", "none"); + this.sound.style.display = "none"; + document.body.appendChild(this.sound); + this.play = function(){ + this.sound.play(); + } + this.stop = function(){ + this.sound.pause(); + } +} \ No newline at end of file diff --git a/sfx/enemy_laugh.wav b/sfx/enemy_laugh.wav new file mode 100644 index 0000000..58e4d8b Binary files /dev/null and b/sfx/enemy_laugh.wav differ diff --git a/sfx/pip_duck.wav b/sfx/pip_duck.wav new file mode 100644 index 0000000..f886de7 Binary files /dev/null and b/sfx/pip_duck.wav differ diff --git a/sfx/pip_happy.wav b/sfx/pip_happy.wav new file mode 100644 index 0000000..2ce85dc Binary files /dev/null and b/sfx/pip_happy.wav differ diff --git a/sfx/pip_hurt.wav b/sfx/pip_hurt.wav new file mode 100644 index 0000000..ec72634 Binary files /dev/null and b/sfx/pip_hurt.wav differ diff --git a/sfx/pip_jump.wav b/sfx/pip_jump.wav new file mode 100644 index 0000000..df954a2 Binary files /dev/null and b/sfx/pip_jump.wav differ diff --git a/sfx/theme.wav b/sfx/theme.wav new file mode 100644 index 0000000..1a876e6 Binary files /dev/null and b/sfx/theme.wav differ