Skip to content

Submarine Game.ino

stephanielha edited this page Nov 22, 2017 · 1 revision

// store the sub graphic var subGraphic; var subNobubbleGraphic;

var bgImage;

// store the player object var player;

// should re-size sub depending on window size var subSize;

// hold projectiles var projectiles = [];

// squids var squids = []; var startingSquidCount = 10; var lastSquidSpawn = 0; var squidSpawnInterval = 1000; // add a new squid every second?

// explosion effects var explosions = [];

// game over stuff var gameOver = false; var timeAlive = 0;

//preload array

var sounds = [];

function preload() { subGraphic = loadImage("assets/bubble.png"); subNobubbleGraphic = loadImage("assets/ship1.png"); squidGraphic = loadImage("assets/squid.png"); bgImage = loadImage("assets/water.jpg"); sounds[0] = loadSound("assets/water.mp3"); sounds[1] = loadSound("assets/explosion.wav"); }

function setup() { createCanvas(windowWidth, windowHeight); background(0); //bgm sounds[0].loop();

// how big should sub be?
subSize = 200;

// create player object
player = new Sub();

// create some squid
for (var i = 0; i < startingSquidCount; i++) {
	squids.push(new Squid());
}

}

function draw() { background(0);

// background image
push();
imageMode(CORNERS);
image(bgImage, 0, 0, width, height);
pop();

push();
textSize(20);
fill(255);
text( + int(timeAlive) + " seconds", 10, 20);
pop();



if(!gameOver) {

timeAlive = millis() / 1000;

// update and display player
player.update();
player.display();
	
// did player collide w squid?
for (var i = 0; i < squids.length; i++) {
var distToSquid = dist(squids[i].x, squids[i].y, player.x, player.y);
		if(distToSquid < subSize / 2.5) {

// EXPLOSION!
explosions.push(new Explosion(player.x, player.y));
sounds[1].play();

gameOver = true;

fill(255,255,0);
noStroke();
ellipse(player.x, player.y, 100, 100);
		}
	
	}
	
	

} else {

// game is over
push();
textSize(100);
textAlign(CENTER, CENTER);
fill(255);
text("GAME OVER", width/2, height/3);
pop();

	
textSize(50);
textAlign(CENTER, CENTER);
fill(255);
text("Refresh to try again?", width/2, height/2);
	

}

// update all squids
for (var i = 0; i < squids.length; i++) {
	squids[i].update();
	squids[i].display();
}

// and explosions
for (var i = 0; i < explosions.length; i++) {
	explosions[i].updateAndDisplay();

	if(explosions[i].life <= 0)
		explosions.splice(i,1);
}

}

// construct a Sub class function Sub() { this.x = width/2; this.y = height/2;

this.rot = 0.0;	// rotation/orientation of player
this.rotSpeed = .1;	// speed of rotation

// acceleration!
this.acc = 0.1;
this.topSpeed = 4.0;
this.speed = 0.0;	// current speed of sub

this.isMoving = false;	// true if pressing forward

// need to keep track of size for collisions
this.size = subSize;

// method to update player
this.update = function() {

	// since we now have acceleration, we always need to
	// update the sub position
	this.x += cos(this.rot) * this.speed;
	this.y += sin(this.rot) * this.speed;

	// check keys
	if(keyIsDown(UP_ARROW)) {
		if(this.speed < this.topSpeed)
			this.speed += this.acc;

		// draw sub bubbles if pressing up
		this.isMoving = true;

	} else {
		// up arrow NOT pressed
		this.isMoving = false;

		// de-accelerate
		if(this.speed > 0)
			this.speed -= this.acc;
	}

	if(keyIsDown(LEFT_ARROW)) {
		// LEFT = forward motion
		this.rot -= this.rotSpeed;
	}

	if(keyIsDown(RIGHT_ARROW)) {
		// RIGHT = forward motion
		this.rot += this.rotSpeed;
	}




}

// method to draw the sub
this.display = function() {
	imageMode(CENTER);
	// dont effect the rest of my sketch
	push();
	// redefine zero as the sub location
	translate(this.x, this.y);
	// rotate around its center
	rotate(this.rot);
	// draw the actual sub image...
	if(this.isMoving) {
		// if moving, draw bubble graphic
		image(subGraphic,0,0);
	} else {
		// if not moving dont draw bubble graphic
		image(subNobubbleGraphic,0,0);
	}
	pop();
}

}

function Squid() { this.x = 0; this.y = 0; this.speed = 4.0; // speed of overall sub

this.rot = random(TWO_PI);	// rotation/orientation of player

this.size = 50;

// method to update player
this.update = function() {
	// move forward!
	this.x += cos(this.rot) * this.speed;
	this.y += sin(this.rot) * this.speed;

	if(this.x > width)
		this.x = 0;
	if(this.x < 0)
		this.x = width;
	if(this.y > height)
		this.y = 0;
	if(this.y < 0)
		this.y = height;

}

this.display = function() {
	push();
	translate(this.x, this.y);
	// rotate(this.rot);
	rectMode(CENTER);
	fill(0, 100);
	noStroke();
	imageMode(CENTER);
	image(squidGraphic,this.size/2, this.size/2, this.size * 2, this.size * 2)
	pop();
}

}

function Explosion(x, y) {

this.x = x; // placeholder
this.y = y;
this.life = 20;	// how many frames long?

this.updateAndDisplay = function() {

	// want explosion to fade out
	this.life--;

	// map opacity to life of explosion
	var op = map(this.life, 20, 0, 255, 0);

	// draw the thing
	push();
	translate(this.x, this.y);
	fill(255, 0, 0, op);
	strokeWeight(subSize);
	stroke(255, 255, 0, op);	// yellow and red, explosion!
	ellipse(0, 0, subSize*2, subSize*2);
	pop();

}

}

function keyPressed() { // show key codes in console if (keyCode == 32 && !gameOver) { console.log("Player X: " + player.x); console.log("Player Y: " + player.y); } }

Clone this wiki locally