Initial Play Area stuff created

This commit is contained in:
rain
2023-10-26 17:01:46 -05:00
commit cf9296a418
16 changed files with 248 additions and 0 deletions
+32
View File
@@ -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;
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 668 B

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

+23
View File
@@ -0,0 +1,23 @@
<html>
<head>
<title>Halloween Game!</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="css/game.css">
</head>
<body onload="startGame()">
<h1>The Spooky Adventures of Pip</h1>
<div id="game">
<script type="text/javascript" src="js/game.js"></script>
</div>
<!--<button id="start" onclick="startGame();">Restart Game</button>-->
<img class="sprite" id="heart" src="img/heart.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="up_player" src="img/pip_up.png">
<img class="sprite" id="right_player" src="img/pip_right.png">
<img class="sprite" id="left_player" src="img/pip_left.png">
</body>
</html>
+193
View File
@@ -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();
}
}
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.