/* Space Warfare by Ashwin Kamath
*********************************************************
Instructions: Use arrow keys to move the main ship
Press any mouse button to shoot a missile
*********************************************************
*/
//Declare arrays and global variables
Planet[] pln= new Planet[1];
Enemies[] ene= new Enemies[5];
Rain[] raincl= new Rain[2000];
//Missile[] mis= new Missile[3];
float pos_X, pos_Y, bul_X, bul_Y;
void setup()
{
//Set canvas size to 800x600
size(800, 600);
for (int i=0; i<2000; i++)
{
raincl[i]= new Rain();
}
for (int j=0; j<5; j++)
{
ene[j]= new Enemies();
}
for (int k=0; k<1; k++)
{
pln[k]= new Planet();
}
// for(int l=0;l<3;l++)
//{
// mis[l]= new Missile();
//}
//Set initial position values for bullet/missile
bul_X=width/2;
bul_Y=550;
//Set initial position values for player's ship
pos_X= width/2;
pos_Y= 550;
}
void draw()
{
//Set framerate to 60
frameRate(60);
background(0, 0, 25);
//Display one planet on a screen at any given time
for (int k=0; k<1; k++)
{
pln[k].display();
pln[k].move();
}
//Display and move stars
for (int i=0; i<2000; i+=20)
{
raincl[i].display();
raincl[i].move();
}
//Display and move three enemies on the screen at any given time
for (int i=0; i<5; i+=2)
{
ene[i].display();
ene[i].move();
}
// for (int i=0;i<3;i++)
//{
//mis[i].display();
//mis[i].move();
//}
//Call user-defined functions from the main program
bulletDraw();
bulletMove();
playerDraw();
playerMove();
}
//Draw player's ship
void playerDraw()
{
fill(0, 0, 200);
ellipse(pos_X, pos_Y-10, 40, 20);
fill(200, 0, 0);
rect(pos_X, pos_Y, 70, 20);
fill(100, 0, 0);
rect(pos_X+20, pos_Y, 35, 20);
fill(0, 0, 200);
rect(pos_X+30, pos_Y-30, 10, 40);
rect(pos_X-30, pos_Y-30, 10, 40);
}
void playerMove()
{
if (keyPressed)
{
if (keyCode==LEFT)
{
pos_X-=12;
if (pos_X<0)
{
pos_X=0;
}
}
}
if (keyCode==RIGHT)
{
pos_X+=12;
if (pos_X>width)
{
pos_X=800;
}
}
}
void bulletDraw()
{
fill(200, 255, 255);
ellipse(pos_X, bul_Y, 10, 10);
}
void bulletMove()
{
if (mousePressed)
{
fill(200, 255, 255);
ellipse(pos_X, bul_Y, 10, 10);
bul_Y-=20;
;
if (bul_Y<0)
{
bul_Y=550;
}
}
}
//If mouse button is released, missile position is reset
void mouseReleased()
{
bul_Y=550;
}class Enemies
{
PVector pos= new PVector(random(0, 800), random(-25, 0));
PVector vel= new PVector(random(1, 4), random(1, 3));
Enemies()
{
}
void display()
{
fill(230, 0, 0);
rectMode(CENTER);
rect(pos.x, pos.y, 60, 20, 4);
fill(200);
rect(pos.x+30, pos.y-5, 10, 30, 4);
rect(pos.x-30, pos.y-5, 10, 30, 4);
fill(130);
rect(pos.x+32, pos.y-5, 5, 30, 4);
rect(pos.x-32, pos.y-5, 5, 30, 4);
fill(145);
rect(pos.x, pos.y-20, 5, 60, 2);
rect(pos.x+10, pos.y-20, 5, 60, 2);
rect(pos.x-10, pos.y-20, 5, 60, 2);
}
void move()
{
//Add velocity to enemy ships
pos.y+=vel.y;
if (pos.y>height)
{
//Randomize and reset position of ship if it hits the boundary
pos.y=0;
pos.x=random(0, width);
}
}
}//This class is not used
class Missile {
PVector pos= new PVector(width/2, 550);
PVector vel= new PVector(0, 20);
Missile()
{
}
void display()
{
fill(200, 255, 255);
ellipse(pos.x, pos.y, 20, 20);
}
void move()
{
if (keyPressed)
{
if (keyCode==TAB)
{
pos.y-=vel.y;
}
}
}
}class Planet
{
PVector pos= new PVector(random(0, width), random(-100, 0));
PVector vel= new PVector(random(0, 3), random(2, 4));
float r, g, b, size;
Planet()
{
r=115;
g=200;
b=0;
size=random(0, 80);
}
//Display planet
void display()
{
noStroke();
fill(r, g, b);
ellipse(pos.x, pos.y, size, size);
fill(200, g, 0, 245);
//ellipse(pos.x+2, pos.y+5, size/4, size/4);
ellipse(pos.x-8, pos.y-8, size/4, size/4);
}
//Add velocity to the planet
void move()
{
pos.y+=vel.y;
if (pos.y>height)
{
pos.x=random(0, width);
size=random(0, 80);
pos.y=-10;
r-=20;
g-=40;
b+=13;
//If planet hits boundary change color
if (r<0)
{
r=0;
r+=20;
}
if (r>255)
{
r=255;
r-=20;
}
if (g<0)
{
g=0;
g+=40;
}
if (g>255)
{
g=255;
g-=40;
}
if (b<0)
{
b=0;
b+=20;
}
if (b>255)
{
b=255;
b-=40;
}
}
}
}class Rain
{
//global variables
PVector pos= new PVector(random(0, 800), random(-200, -100));
PVector vel= new PVector(random(0, 2), random(4, 10));
float grav;
Rain()
{
grav=5;
}
void display()
{
stroke(255);
line(pos.x, pos.y, pos.x, pos.y+10);
}
void move()
{
pos.y+=vel.y;
if (pos.y>height)
{
pos.y=0;
pos.x=random(0, width);
}
}
}