Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//define global variables (center coords for player and target, as well as a score count and variable to determine whether to make a new target)
int playX = 200;
int playY = 200;
int tarX = 0;
int tarY = 0;
boolean doesExist;
int score = 0;



void setup(){
  size (400,400);
  //noCursor();  later realized this didn't benefit or detract from the game
  doesExist = false;
}



void draw(){
  background(25,0,25);
  
  //setup walls
  rectMode (CORNERS);
  fill(100,0,100);
  //borders
  rect(0,0,400,10);
  rect(0,0,10,400);
  rect(390,0,400,400);
  rect(0,390,400,400);
  //maze
  rect(300,40,400,50);
  rect(0,110,100,120);
  rect(90,40,100,120);
  rect(290,40,300,130);
  rect(240,120,300,130);
  rect(270,120,280,270);
  rect(270,260,350,270);
  rect(340,180,350,270);
  rect(330,180,370,190);
  rect(120,60,170,70);
  rect(150,60,160,300);
  rect(100,190,160,200);
  rect(150,290,230,300);
  rect(220,290,230,400);
  rect(0,230,70,240);
  rect(60,230,70,370);
  rect(60,360,190,370);
  
  //activate player and target
  player();
  target();
  
  //positioning debug
  //println(doesExist);
  //println(tarX, tarY);
  //println(playX, playY);
  
  //display score
  text(score, 15,25);
}



void player(){
  rectMode(CENTER);
  fill(150,150,255);
  rect(playX, playY, 20, 20, 7);
  //previously contained boundary collisions, but was moved to keyPressed with the others
}



void target(){
  
  if (doesExist == false){
    tarX = ((int)random(10,391));
    tarY = ((int)random(10,391));
    doesExist = true;
  }
  if (doesExist ==true){
    rectMode(CENTER);
    fill((int)random(100, 250),(int)random(100, 250),(int)random(100, 250));
    rect(tarX, tarY, 20, 20);
  }
  
  if (playX >= (tarX-17) && playY >= (tarY-17) && playX <= (tarX+17) && playY <= (tarY+17)){
    score++;
    doesExist = false;
  }
  
}



void keyPressed(){
  //this allows player movement. unfortunately, it is a bit janky with holding inputs or multiple input, but it's functional.
  //each input checks the position of the player in relation to each 'wall', preventing input when colliding. Unfortunately I couldn't think of a function to streamline this...
  //amount of calculations is reduces by walls sharing sides
  if (key == 'W' || key == 'w'){
    if (playY==20 || playY==60 && playX>=295 || playY==130 && playX<=105 || playY==140 && playX>=235 && playX<=305 || playY==280 && playX>=265 && playX<=355 || playY==200 && playX>=325 && playX<=375 || playY==80 && playX>=115 && playX<=175 ||
    playY==210 && playX>=95 && playX<=165 || playY==310 && playX>=145 && playX<=235 || playY==250 && playX<=75 || playY==380 && playX>=55 && playX<=195){  
    }else{
      playY -= 5;
    }
  }
  if (key == 'A' || key == 'a'){
    if (playX==20 || playX==110 && playY>=35 && playY<=125 || playX==310 && playY>=40 && playY<=135 || playX==290 && playY>=115 && playY<=275 || playX==360 && playY>=175 && playY<=275 || playX==370 && playY>=175 && playY<=195 ||
    playX==180 && playY>=55 && playY<=75 || playX==170 && playY>=55 && playY<=305 || playX==240 && playY>=285 || playX==80 && playY>=225 && playY<=375 || playX==200 && playY>=355 && playY<=375){
    }else{
      playX -= 5;
    }
  }
  if (key == 'S' || key == 's'){
    if (playY==380 || playY==30 && playX>=285 || playY==100 && playX<=100 || playY==30 && playX>=85 && playX<=105 || playY==110 && playX>=235 && playX<=300 || playY==250 && playX>=265 && playX<=355 || playY==170 && playX>=325 && playX<=375 ||
    playY==50 && playX>=115 && playX<=175 || playY==180 && playX>=95 && playX<=165 || playY==280 && playX>=145 && playX<=235 || playY==220 && playX<=75 || playY==350 && playX>=55 && playX<=195){
    }else{
      playY += 5;
    }
  }
  if (key == 'D' || key == 'd'){
    if (playX==380 || playX==280 && playY>=35 && playY<=125 || playX==80 && playY>=35 && playY<=110 || playX==230 && playY>=115 && playY<=135 || playX==260 && playY>=117 && playY<=275 || playX==330 && playY>=175 && playY<=275 ||
    playX==320 && playY>=175 && playY<=195 || playX==110 && playY>=55 && playY<=75 || playX==140 && playY>=55 && playY<=305 || playX==90 && playY>=185 && playY<=205 || playX==210 && playY>=285 || playX==50 && playY>=225 && playY<=375){
    }else{
      playX += 5;
    }
  }
}