Project 15 – COVID Shooter Game

For this project, I was inspired by scrolling shooter games. These games were popular in arcades and on older gaming consoles. I used to play games like these a lot as a kid, so I was really looking forward to creating this. As COVID-19 is something that affected us all this year, I used this theme for my project. You move the ship around in this game by using the WASD keys and you shoot by using the spacebar. The goal is to shoot the coronavirus molecules that are coming at the ship. If you shoot enough, you’ll win the game and prevent the world from getting infected. If the ship gets hit too many times, you’ll lose the game. If too many molecules pass the screen, you’ll lose the game and the world will be infected. There is a bit of a randomness factor to this game as the bullets will shoot from either the left or right gun on the ship. If I had more time, I would have wanted to add animations to this game. For example, I was considering having the viruses explode when hit. 

sketch
//Dreami Chambers; Section C; dreamic@andrew.cmu.edu; Assignment-14-Project

var stars = []
var bullets = []
var viruses = []
var hearts = []
var hearts2 = []
var x = 170 //ship x position
var y = 320 //ship y position
var dx = 0 //ship x velocity
var dy = 0 //ship y velocity
var health = 3
var worldHealth = 3
var virusesHit = 0
var gameStart = false //checks if game was started

function preload() {
	ship=loadImage("https://i.imgur.com/te1SxyO.png")
	bullet=loadImage("https://i.imgur.com/YHVJAW8.png")
	virus=loadImage("https://i.imgur.com/RrjmBFe.png")
	hearts[0]=loadImage("https://i.imgur.com/eEYu6KF.png")
	hearts[1]=loadImage("https://i.imgur.com/xe4b6B7.png")
	hearts[2]=loadImage("https://i.imgur.com/Jupb3vO.png")
	hearts[3]=loadImage("https://i.imgur.com/ByIC9eQ.png")
	hearts2[0]=loadImage("https://i.imgur.com/GdyaHu7.png")
	hearts2[1]=loadImage("https://i.imgur.com/chdj98I.png")
	hearts2[2]=loadImage("https://i.imgur.com/onnPuBv.png")
	hearts2[3]=loadImage("https://i.imgur.com/6ZwZ3ej.png")
} 

function setup() {
	noLoop() //game does not start at first
    createCanvas(400, 400);
    //stars setup
    for (var i = 0; i < 100; i++) {
    	var rx = random(width)
    	var ry = random(height)
    	var rdy = random(4)
        stars[i] = makeStars(rx, ry, 2, rdy)
    }
    //virus setup
    for (var j = 0; j < 1; j++) {
    	var rx = random(50, 350)
    	var ry = random(-50, 0)
        viruses[j] = makeVirus(rx, ry)
    }
}

function draw() {
	background(0)
	//draws, removes, and adds stars
	starUpdate()
	starsRemove()
	starsAdd()
	//draws, removes, and adds viruses
	virusUpdate()
	virusRemove()
	virusAdd()
	//draws and removes bullets
	bulletUpdate()
	bulletRemove()
	image(ship, x, y, 60, 70) //spaceship
	image(hearts[health], 10, 10, 60, 20) //healthbar
	image(hearts2[worldHealth], 330, 10, 60, 20) //world healthbar

	if (gameStart == false){ //if game has not been started, a start message will show
		start()
	}
	if (collides() == true){ //if bullet hits virus, remove virus
		virusesHit += 1
		viruses.splice(0, 1)
	}
	if (collides2() == true){ //if virus hits the spaceship, remove a heart from the healthbar
		health -= 1
		viruses.splice(0, 1)
	}
	if (viruses[0].y > height){
		worldHealth -= 1
		print(worldHealth)
	}
	if (keyIsDown(87) & y > 0){ //move up
		y-=2
	}
	if (keyIsDown(83) && y < 380){ //move down
		y+=2
	}
	if (keyIsDown(68) && x < 380){ //move right
		x+=2
	}
	if (keyIsDown(65) && x > -40){ //move left
		x-=2
	}
	endings() //different ending screens depending on whether or not the game is won
}

function start(){ //start message
	strokeWeight(3)
	stroke(240, 175, 0)
	textSize(40)
	translate(200, 190)
	textAlign(CENTER, CENTER)
	text("Click to Start", 0, 0)
}

function mousePressed(){ //starts game when mouse clicked
	loop()
	gameStart = true
}

//if spacebar pressed, shoot bullet
function keyPressed(){
	if (key === ' '){
		x2 = random([x+5, x+45]) //bullet shoots from either left or right gun
		var bullet = makeBullets(x2, y, 2)
		bullets.push(bullet)
	}
}

//star functions
function starUpdate(){
	for (var i = 0; i < stars.length; i++){
        stars[i].stepFunction()
        stars[i].drawFunction()
	}
}

//removes stars when off screen
function starsRemove(){
	starsToKeep = []
	for (var i = 0; i < stars.length; i++){
        if (stars[i].y + stars[i].size > 0){
        	starsToKeep.push(stars[i])
        }
	}
	stars = starsToKeep
}

//adds new stars
function starsAdd(){
	var starProb = 0.5
	if (random(1) < starProb){
		stars.push(makeStars(random(width), random(-5, 0), 2, random(4)))
	}
}

//moves stars down
function starStep() {
    this.y += this.dy
}

function starDraw() {
	stroke(this.c)
	strokeWeight(this.size)
	point(this.x, this.y)
}

function makeStars(px, py, pdy, ps) {
	var s = {x: px, y: py, dy: pdy, 
		size: ps, c: color(random(200, 255),random(150, 230),random(100, 145)),
		drawFunction: starDraw, stepFunction: starStep
	}
	return s
}

//bullet functions
function bulletUpdate(){
	for (var i = 0; i < bullets.length; i++){
	    bullets[i].stepFunction()
	    bullets[i].drawFunction()
	}
}

//moves bullet up
function bulletStep() {
    this.y -= this.dy
}

function bulletDraw() {
	image(bullet, this.x, this.y, 10, 20)
}

//removes bullet when off screen
function bulletRemove(){
	bulletsToKeep = []
	for (var i = 0; i < bullets.length; i++) {
        if (bullets[i].y > 0){
        	bulletsToKeep.push(bullets[i])
        }
	}
	bullets = bulletsToKeep
}

function makeBullets(px, py, pdy) {
	var b = {x: px, y: py, dy: pdy, 
		drawFunction: bulletDraw, stepFunction: bulletStep
	}
	return b
}

//virus functions
function virusUpdate(){
	for (var i = 0; i < 1; i++){
        viruses[i].stepFunction()
        viruses[i].drawFunction()
	}
}

//adds virus when there are none on the screen
function virusAdd(){
	var virusProb = 0.7
	if (random(1) < virusProb){
		viruses.push(makeVirus(random(50, 350), random(-100, -20)))
	}
}

//removes virus when off screen
function virusRemove(){
	virusToKeep = []
	for (var i = 0; i < viruses.length; i++) {
        if (viruses[i].y < height+2){
        	virusToKeep.push(viruses[i])
        }
	}
	viruses = virusToKeep
}

//moves virus down
function virusStep() {
    this.y += this.dy
}

function virusDraw() {
	image(virus, this.x, this.y, this.size, this.size)
}

function makeVirus(px, py) {
	var v = {x: px, y: py, dy: 2, 
		size: random(40,50),
		drawFunction: virusDraw, stepFunction: virusStep
	}
	return v
}

//checks to see if any bullet hits the virus
function collides() {
	for (var i = 0; i < bullets.length; i++){
		var d = dist(bullets[i].x, bullets[i].y, viruses[0].x, viruses[0].y)
		if (d < viruses[0].size-10){
			return true
		}
	}	
}

//checks to see if any virus hits the spaceship
function collides2() {
	var d = dist(x, y, viruses[0].x, viruses[0].y)
		if (d < viruses[0].size){
			return true
		}
	}

function endings(){
	if (health == 0 ){ //bad ending
		image(hearts[0], 10, 10, 60, 20) //makes sure that the heart bar shows zero hearts
		strokeWeight(3)
		stroke(240, 175, 0)
		textSize(40)
		translate(200, 190)
		textAlign(CENTER, CENTER)
		text("GAME OVER", 0, 0)
		stroke(0)
		fill(240, 175, 0)
		textSize(12)
		translate(0, 40)
		text("You were unable to prevent the spread of the virus.\nBetter luck next time!", 0, 0)
		noLoop()
	}
	if (worldHealth == 0){ //second bad ending
		strokeWeight(3)
		stroke(240, 175, 0)
		textSize(40)
		translate(200, 190)
		textAlign(CENTER, CENTER)
		text("GAME OVER", 0, 0)
		stroke(0)
		fill(240, 175, 0)
		textSize(12)
		translate(0, 40)
		text("You let too many viruses pass and the world has been infected.\nBetter luck next time!", 0, 0)
		noLoop()
	}
	if (virusesHit == 10){ //good ending
		strokeWeight(3)
		stroke(240, 175, 0)
		textSize(40)
		translate(200, 190)
		textAlign(CENTER, CENTER)
		text("YOU WIN", 0, 0)
		stroke(0)
		fill(240, 175, 0)
		textSize(12)
		translate(0, 40)
		text("You have successfully destroyed all the viruses.\nRefresh to play again!", 0, 0)
		noLoop()
	}
}

Leave a Reply