Skip to content

Commit

Permalink
added shooting / sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisOptimism committed Apr 7, 2021
1 parent 6ab6992 commit 4e7fec0
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 13 deletions.
Binary file modified assets/.DS_Store
Binary file not shown.
Binary file modified assets/images/.DS_Store
Binary file not shown.
Binary file modified assets/sound/.DS_Store
Binary file not shown.
Binary file added assets/sound/coin.wav
Binary file not shown.
Binary file added assets/sound/hit.wav
Binary file not shown.
Binary file added assets/sound/shot.wav
Binary file not shown.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ <h1>Press Enter to start</h1>
<script src="js/firstaid.js"></script>
<script src="js/background.js"></script>
<script src="js/foreground.js"></script>
<script src="js/shot.js"></script>
<script src="js/tesla.js"></script>
<script src="js/villan.js"></script>
<script src="js/player.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions js/coins.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Coin {
return false
} else {
game.player.score++;
game.coinSound.play();
game.coinSound.setVolume(0.2)
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/foreground.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Foreground {


this.x2 -= 7;
if (this.x2 <= -width - 400) {
if (this.x2 <= -width - 2000) {
this.x2 = 0
}
this.x -= 6;
Expand Down
50 changes: 39 additions & 11 deletions js/game.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
class Game {
constructor() {
this.mode;
this.gameSpeed = 1;
this.obstacleFreq = 1;
this.playerImage;
this.firstAidImage;
this.obstacleImage;
this.backgroundImages;
this.foregroundImage;
this.coinImage;
this.mySound;

this.coins = [];
this.coinImage;
this.coinSound;

this.obstacles = [];
this.obstacleImage;

this.firstAidArr = [];
this.firstAidImage;

this.teslaArr = [];
this.gameSpeed = 1;
this.obstacleFreq = 1;

this.gameoverSound;
this.gameoverSoundCounter = 0;
this.jumpSound;
this.gettingHitSound;
this.dogImage;
this.shotSound;

}
setup() {
Expand All @@ -29,6 +37,7 @@ class Game {
}
preload() {
// neuro = loadFont('assets/fonts/neuropol.ttf');
// images
this.coinImage = loadImage('assets/images/lab.png');
this.playerImage = loadImage('assets/images/steffen.gif');
this.firstAidImage = loadImage('assets/images/pizza.png');
Expand All @@ -38,6 +47,9 @@ class Game {
this.dogImage = loadImage('assets/images/background/day/dog.gif');

// sounds
this.shotSound = loadSound('assets/sound/shot.wav');
this.coinSound = loadSound('assets/sound/coin.wav');
this.gettingHitSound = loadSound('assets/sound/hit.wav');
this.jumpSound = loadSound('assets/sound/jump.wav');
this.gameoverSound = loadSound('assets/sound/gameover.wav');

Expand Down Expand Up @@ -88,17 +100,19 @@ class Game {
}

draw() {
// if (this.mode === 0)
// game mode
if (this.mode === 1) {
this.background.draw()
this.drawFirstAid()
this.drawObstacles();
this.drawCoin();
if (this.player.score > 20) {
if (this.player.score > 5) {
this.villan.draw();
this.drawTesla();
}
this.player.draw();
this.shoot();
this.foreground.draw();
this.player.healthBarDraw();
this.player.scoreDraw();
Expand Down Expand Up @@ -130,13 +144,28 @@ class Game {
this.obstacles = [];
this.coins = [];
this.firstAidArr = [];
this.teslaArr = [];
song.pause();
if (!this.gameoverSound.isPlaying() && this.gameoverSoundCounter === 0) {
this.gameoverSound.play();
this.gameoverSoundCounter++
}
}
}
shoot() {
this.player.bullets.forEach(bullet => {
bullet.draw();
})
this.obstacles.forEach(obs => {
this.player.bullets = this.player.bullets.filter(bullet => {
if (bullet.collision(obs) || bullet.x > 1000) {
return false
} else {
return true;
}
})
})
}

levelStatusDraw() {
push();
Expand All @@ -145,7 +174,6 @@ class Game {
pop();
}


drawObstacles() {
if (frameCount * this.obstacleFreq % 100 === 0) {
this.obstacles.push(this.obstacle = new Obstacles(1));
Expand All @@ -154,7 +182,7 @@ class Game {
obs.draw();
})
this.obstacles = this.obstacles.filter(obstacle => {
if (obstacle.collision(this.player) || this.obstacle.x < 0) {
if (obstacle.collision(this.player) || obstacle.x < 0) {
return false
} else {
return true
Expand All @@ -170,7 +198,7 @@ class Game {
firstAid.draw();
})
this.firstAidArr = this.firstAidArr.filter(firstAid => {
if (firstAid.collision(this.player) || this.firstAid.x < 0) {
if (firstAid.collision(this.player) || firstAid.x < 0) {
return false
} else {
return true
Expand All @@ -179,14 +207,14 @@ class Game {
}

drawCoin() {
if (frameCount % 30 === 0) {
if (frameCount % 400 === 0) {
this.coins.push(this.coin = new Coin());
}
this.coins.forEach(coin => {
coin.draw();
})
this.coins = this.coins.filter(coin => {
if (coin.collision(this.player)) {
if (coin.collision(this.player) || coin.x < 0) {
return false
} else {
return true
Expand Down
1 change: 1 addition & 0 deletions js/obstacles.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Obstacles {
if (dist(obstacleX, obstacleY, playerX, playerY) > 90) {
return false
} else {
game.gettingHitSound.play();
game.player.health -= 20;
return true
}
Expand Down
11 changes: 10 additions & 1 deletion js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ class Player {
this.y = 400;
this.height = 200;
this.width = 100;
this.score = 0;
this.score = 30;
this.health = 150;
this.level = 1;
this.gravity = 0.6;
this.velocity = 0;
this.playerImage;
this.jumpSound;
this.bullets = [];
}
setup() {}

draw() {
if (keyIsDown(65) && this.x >= 5) this.moveLeft();
Expand Down Expand Up @@ -77,5 +79,12 @@ class Player {
if (keyCode === 32 && this.y > 290) {
game.jumpSound.play();
}
if (keyCode === 80) {
if (this.score > 0) {
this.bullets.push(this.gun = new Shot())
game.shotSound.play();
this.score--
}
}
}
}
27 changes: 27 additions & 0 deletions js/shot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Shot {
constructor() {
this.x = game.player.x + game.player.width / 2;
this.y = game.player.y + game.player.height / 3;
this.width = 20;
this.height = 20;
this.damage = 20;
}
draw() {
rect(this.x, this.y, this.width, this.height)
this.x += 19
}
collision(objectInfo) {
const bulletX = this.x + this.width / 2;
const bulletY = this.y + this.height / 2;
const objectX = objectInfo.x + objectInfo.width / 2;
const objectY = objectInfo.y + objectInfo.height / 2;

if (dist(bulletX, bulletY, objectX, objectY) > 50) {
return false
} else {
console.log('asd')
game.obstacles.splice(game.obstacles.indexOf(objectInfo));
return true
}
}
}
1 change: 1 addition & 0 deletions js/tesla.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Tesla {
return false
} else {
game.player.health -= 20;
game.gettingHitSound.play();
return true
console.log('hit by tesla')
}
Expand Down
1 change: 1 addition & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ main {

.menu {
text-align: center;
opacity: 0.3;
position: absolute;
top: 0;
right: 0;
Expand Down

0 comments on commit 4e7fec0

Please sign in to comment.