In this game, you play as a small red square and move left and right on the screen trying to shoot the yellow boxes that fall from the top of the screen. It is infinite and each time you remove a line of enemies, new ones spawn and move down at a faster pace. If the enemies reach the bottom, the game is over and it stops.
The code was originally in multiple files.
//Enemy constructor
function enemy(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = 50;
this.show = function(){
ctx.fillStyle = "yellow";
ctx.fillRect(this.x,this.y,this.w,this.h);
}
this.move = function(speed){
this.clear();
this.y += speed;
this.show();
}
this.clear = function(){
ctx.clearRect(this.x,this.y,this.w,this.h);
}
}
//Bullet constructor
function bullet(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.show = function(){
ctx.fillStyle = "blue";
ctx.fillRect(this.x,this.y,this.w,this.h);
}
this.move = function(){
this.clear();
this.y -= 5;
this.show();
}
this.clear = function(){
ctx.clearRect(this.x,this.y,this.w,this.h);
}
this.hits = function(bullet, enemy){
if(bullet.y < enemy.y + enemy.h +10 && bullet.x < enemy.x + enemy.w && bullet.x > enemy.x - 3){
return(true);
}
}
}
//Player constructure
function player(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.show = function(){
ctx.fillStyle = "red";
ctx.fillRect(this.x,this.y,this.w,this.h);
if(this.x <= 0) this.x = 0;
if(this.x >= canvas.width - this.w) this.x = canvas.width - this.w;
}
this.move = function(dir){
this.clear();
this.x += dir;
this.show();
}
this.clear = function(){
ctx.clearRect(this.x,this.y,this.w,this.h);
}
}
var canvas = document.getElementById("mainCanvas");
var ctx = canvas.getContext("2d");
function game(){
document.getElementById("play").style.visibility= "hidden";
var playerOne = new player(canvas.width/2,canvas.height-20,20,20);
var bullets = [];
var enemies = [];
var enemySpeed = 1;
var score = 0;
var level = 1;
//Draws enemies
function drawEnemies(yPos){
for(var i = 0; i < 7; i++){
var enemyOne = new enemy(i*80,20+yPos,20,20);
enemies.push(enemyOne);
enemies[i].show();
}
}
//Main game loop
function update(){
playerOne.show();
document.getElementById("scoreText").innerHTML = score;
//Shoot the bullets and checks if they hit an enemy
for(var i = 0; i < bullets.length; i++){
bullets[i].move();
for(var j = 0; j < enemies.length; j++){
if(bullets[i].hits(bullets[i],enemies[j])){
enemies[j].clear();
bullets[i].clear();
enemies.splice(j,1);
score += 100;
level++;
}
}
if(bullets[i].y <= 0 ){
bullets[i].clear();
bullets.splice(i,1);
}
}
if(enemies.length <= 0){
drawEnemies(20);
enemySpeed += 1;
}
window.requestAnimationFrame(update);
}
var enemyMove = setInterval(function(){
for(var i = 0; i < enemies.length; i++){
enemies[i].move(enemySpeed);
if(enemies[i].y > 400){
lost();
clearInterval(enemyMove);
}
}
},100);
function lost(){
setInterval(function(){
ctx.fillStyle = "red";
ctx.font = "80px Arial";
ctx.fillText("GAME OVER",0,100);
ctx.font = "24px Arial";
ctx.fillText("Your Score Was: " + score,0,150);
ctx.font = "24px Arial";
ctx.fillText("Your Killed: " + level + " Enemies",0,200);
},50);
}
//Key Press functions
window.addEventListener("keydown",function(event){
if(event.keyCode == 37) {
playerOne.move(-10);
}
if(event.keyCode == 39) {
playerOne.move(10);
}
if(event.keyCode == 32){
var bulletOne = new bullet(playerOne.x + 7, playerOne.y, 5, 5);
bullets.push(bulletOne);
}
});
update();
}
<head>
<title>Shooter JS Game</title>
</head>
<body>
<canvas id="mainCanvas" width="500" height="400" style="background-color: lightgrey"></canvas>
<button style="position: absolute; top:190px; left:200px" id="play" onClick="game()">Play Game</button>
<h3 id="scoreText"></h3>
<script type="text/javascript" src="enemy.js"></script>
<script type="text/javascript" src="bullet.js"></script>
<script type="text/javascript" src="player.js"></script>
<script type="text/javascript" src="mainJS.js"></script>
</body>
I was wondering if this JavaScript code I wrote was in any way acceptable. This is the first game that I have made that I consider somewhat acceptable code.