Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
int pspeeda = 5; //player speed A
int pspeedd = 5; //player speed D
int pmx = 180; //player move
int pmy = 300; //player move
int pjmx = 0; //projectile move
int pjmy = 0; //projectile move
float hmx = random(40,360); //Target spawn
float rndcR = random (100,255); //Red
float rndcG = random (100,255); //Green
float rndcB = random (100,255); //Blue
int hmy = 0; //Terget speed / pos
boolean fired = false; //Is projectile fired
boolean dead = false; //Is target dead

//SETUP/ - / - / - / - / - / - / - / - / - / - / - / - / - / - / - / - /
void setup () {
  size(400, 400);
  frameRate (120);
}

//DRAW/ - / - / - / - / - / - / - / - / - / - / - / - / - / - / - / - /
void draw () {
  background(255 - rndcR, 255 - rndcG, 255 - rndcB);
  createPlayerShip();
  if (fired == true){createPlayerProjectile();}
  if (dead == false) {createTarget();}

}
//PLAYERSHIP/ - / - / - / - / - / - / - / - / - / - / - / - / - / - / - / - /
void createPlayerShip () { //Player Ship
  rectMode(CENTER);
  
  //Shadow
  fill(255 - rndcR - 100, 255 - rndcG - 100, 255 - rndcB - 100);
  triangle(pmx+4,pmy+5,pmx-6,pmy+25,pmx+4,pmy+20);
  triangle(pmx+4,pmy+5,pmx+14,pmy+25,pmx+4,pmy+20);
  
  //Body
  fill(rndcR, rndcG, rndcB);
  triangle(pmx,pmy,pmx-10,pmy+20,pmx,pmy+15);
  triangle(pmx,pmy,pmx+10,pmy+20,pmx,pmy+15);
  noStroke();
  
  //Movment
  pmx = pmx - pspeeda;
  pmx = pmx + pspeedd;
  if (key == 'a' && keyPressed == true) {pspeeda = 2;}
    else {pspeeda = 0;}
  if (key == 'd' && keyPressed == true) {pspeedd = 2;}
    else {pspeedd = 0;}
    
  //Fire Projectile
  if (keyCode == CONTROL && keyPressed ) {fired = true;}
  
  //Screen restriction
  if (pmx > width - 10) {pspeedd = 0;}
  if (pmx < 10) {pspeeda = 0;}
}

//PROJECTILE/ - / - / - / - / - / - / - / - / - / - / - / - / - / - / - / - /
void createPlayerProjectile () {
  if (pjmy > -400 && fired == true) {rect(pjmx + pmx,pjmy + pmy,5,10); pjmy = pjmy - 6;}
  else if (pjmy < -400) {fired = false; pjmx = 0; pjmy = 0;}
}

//TARGET/ - / - / - / - / - / - / - / - / - / - / - / - / - / - / - / - /
void createTarget () {
  //Hit Detection
  if (hmy > 400 || pjmy < hmy-270 && pjmy > hmy-290 && (pjmx + pmx) < hmx +15 && (pjmx + pmx) > hmx -15) {
    rndcR = random (100,255); rndcG = random (100,255); rndcB = random (100,255); 
    dead = true; dead = false; hmy = 0; hmx = random(40,360);}
    
  //Target
  if (dead == false) {
    fill(255 - rndcR - 100, 255 - rndcG - 100, 255 - rndcB - 100); rect(hmx+4,hmy+5,20,20); 
    fill(rndcR, rndcG, rndcB); rect(hmx,hmy,20,20); hmy = hmy +1;}
  if (dead == true) {rndcR = random (100,255); rndcG = random (100,255); rndcB = random (100,255);}
}