• fullscreen
  • Enemy.pde
  • Man.pde
  • P4_Boxer.pde
  • class Enemy {
      float x;
      float y = random(height/8, height - height/8);
      float dia = 50;
      float speed = 4;
      float angle1 = PI + HALF_PI;
      float angle2 = atan2(y-height/2, x-width/2);
    
    
      Enemy(float tempX) {
        x = tempX;
      }
    
      void drawEnemy() {
    
    
    
        noStroke();
        fill(0, 155, 0);
        rect(0, 0, 0, 0);
        pushMatrix();
        ellipse(x, y, dia, dia);
        rotate(angle1);
        popMatrix();
    
        angle1 += random(-0.1, 0.1);
        x += cos(angle1) * speed;
        y += sin(angle1) * speed;
    
    
        if (x > width - dia || x < 0 + dia || y > height - dia || y < 0 + dia) {
          angle1 += PI;
        }
    
        //    if(score >= 1) {
        //      speed += 1;
        //    }
      }
    }
    
    
    class Man {
      int mx = width/2;
      int my = height/2;
      int dia = 100;
    
    
    
      float angle = 0.0;
      float offset = 0;
      float scalar = dia/2;
      float speed = .2;
      float easing = 0.05;
    
      Man() {
      }
    
      void drawMan() {
    
        pushMatrix();
        fill(255, 0, 0);
        float zx = offset + cos(angle) * scalar;
        float zy = offset + sin(angle) * scalar;
    
        stroke(225, 140, 100);
        strokeWeight(30);
        line(mx, my, mx+zx, my+zy);
        line(mx, my, mx-zx, my-zy);
        noStroke();
    
        ellipse(mx-zx, my-zy, -dia/2, -dia/2);
        ellipse(mx+zx, my+zy, dia/2, dia/2);
    
    
        popMatrix();
    
        float lh1 = dist(mx-zx, my-zy, enemy1.x, enemy1.y);
        float rh1 = dist(mx+zx, my+zy, enemy1.x, enemy1.y);
    
        if((lh1 <= dia/2.5) || (rh1 <= dia/2.5)) {
    
          score += 1;
          enemy1.speed *= 1.01;
          enemy2.speed *= 1.01;
          enemy3.speed *= 1.01;
          enemy4.speed *= 1.01;
    
          if((enemy1.x <= width/2) && (enemy1.x <= height/2)) {
            enemy1.x = width/8;
            enemy1.y = random(height/8, height/2);
          }
          else if((enemy1.x >= width/2) && (enemy1.x <= height/2)) {
            enemy1.x = width - width/8;
            enemy1.y = random(height/8, height/2);
          }
          else if((enemy1.x <= width/2) && (enemy1.x >= height/2)) {
            enemy1.x = width/8;
            enemy1.y = random(height - height/8, height/2);
          }
          else if((enemy1.x >= width/2) && (enemy1.x >= height/2)) {
            enemy1.x = width - width/8;
            enemy1.y = random(height - height/8, height/2);
          }
          else {
            enemy1.x = width/8;
            enemy1.y = height/2;
          }
        }
    
        float lh2 = dist(mx-zx, my-zy, enemy2.x, enemy2.y);
        float rh2 = dist(mx+zx, my+zy, enemy2.x, enemy2.y);
    
        if((lh2 <= dia/2.5) || (rh2 <= dia/2.5)) {
    
          score += 1;
          enemy1.speed *= 1.01;
          enemy2.speed *= 1.01;
          enemy3.speed *= 1.01;
          enemy4.speed *= 1.01;
    
          if((enemy2.x <= width/2) && (enemy2.x <= height/2)) {
            enemy2.x = width/8;
            enemy2.y = random(height/8, height/2);
          }
          else if((enemy2.x >= width/2) && (enemy2.x <= height/2)) {
            enemy2.x = width - width/8;
            enemy2.y = random(height/8, height/2);
          }
          else if((enemy2.x <= width/2) && (enemy2.x >= height/2)) {
            enemy2.x = width/8;
            enemy2.y = random(height - height/8, height/2);
          }
          else if((enemy2.x >= width/2) && (enemy2.x >= height/2)) {
            enemy2.x = width - width/8;
            enemy2.y = random(height - height/8, height/2);
          }
          else {
            enemy2.x = width/8;
            enemy2.y = height/2;
          }
        } 
    
    
        float lh3 = dist(mx-zx, my-zy, enemy3.x, enemy3.y);
        float rh3 = dist(mx+zx, my+zy, enemy3.x, enemy3.y);
    
        if((lh3 <= dia/2.5) || (rh3 <= dia/2.5)) {
    
          score += 1;
          enemy1.speed *= 1.01;
          enemy2.speed *= 1.01;
          enemy3.speed *= 1.01;
          enemy4.speed *= 1.01;
    
          if((enemy3.x <= width/2) && (enemy3.x <= height/2)) {
            enemy3.x = width/8;
            enemy3.y = random(height/8, height/2);
          }
          else if((enemy3.x >= width/2) && (enemy3.x <= height/2)) {
            enemy3.x = width - width/8;
            enemy3.y = random(height/8, height/2);
          }
          else if((enemy3.x <= width/2) && (enemy3.x >= height/2)) {
            enemy3.x = width/8;
            enemy3.y = random(height - height/8, height/2);
          }
          else if((enemy3.x >= width/2) && (enemy3.x >= height/2)) {
            enemy3.x = width - width/8;
            enemy3.y = random(height - height/8, height/2);
          }
          else {
            enemy3.x = width/8;
            enemy3.y = height/2;
          }
        } 
    
    
        float lh4 = dist(mx-zx, my-zy, enemy4.x, enemy4.y);
        float rh4 = dist(mx+zx, my+zy, enemy4.x, enemy4.y);
    
        if((lh4 <= dia/2.5) || (rh4 <= dia/2.5)) {
    
          score += 1;
          enemy1.speed *= 1.01;
          enemy2.speed *= 1.01;
          enemy3.speed *= 1.01;
          enemy4.speed *= 1.01;
    
          if((enemy4.x <= width/2) && (enemy4.x <= height/2)) {
            enemy4.x = width/8;
            enemy4.y = random(height/8, height/2);
          }
          else if((enemy4.x >= width/2) && (enemy4.x <= height/2)) {
            enemy4.x = width - width/8;
            enemy4.y = random(height/8, height/2);
          }
          else if((enemy4.x <= width/2) && (enemy4.x >= height/2)) {
            enemy4.x = width/8;
            enemy4.y = random(height - height/8, height/2);
          }
          else if((enemy4.x >= width/2) && (enemy4.x >= height/2)) {
            enemy4.x = width - width/8;
            enemy4.y = random(height - height/8, height/2);
          }
          else {
            enemy4.x = width/8;
            enemy4.y = height/2;
          }
        } 
    
    
        fill(225, 140, 100);
    
        ellipse(mx, my, dia, dia);
      }
    
      void moveRIGHT() {
        angle += speed;
      }
    
      void moveLEFT() {
        angle -= speed;
      } 
    
      void checkMouse() {
        if (mousePressed == true) {
          float extend = 10;
          scalar = scalar + extend;
        }
        else {
          scalar = scalar - 20;
          if (scalar <= dia/2) {
            scalar = dia/2;
          }
        }
      }
    }
    
    
    Enemy enemy1;
    Enemy enemy2;
    Enemy enemy3;
    Enemy enemy4;
    Man man;
    
    PFont bhb;
    
    int time = 0;
    
    PImage logo;
    PImage msc;
    PImage esc;
    
    int mode = 0;
    int score = 0;
    int back;
    
    void setup() {
      size(800, 480);
      smooth();
      ellipseMode(CENTER);
      rectMode(CENTER);
      textAlign(CENTER);
      noCursor();
    
      enemy1 = new Enemy(width/8);
      enemy2 = new Enemy(width/8);
      enemy3 = new Enemy(width-width/8);
      enemy4 = new Enemy(width-width/8);
      man = new Man();
    
      logo = loadImage("logo.png");
      msc = loadImage("msc.png");
      esc = loadImage("esc.png");
      bhb = loadFont("BlueHighway-Bold-48.vlw");
    }
    
    void draw() {
    
      float me1 = dist(man.mx, man.my, enemy1.x, enemy1.y); 
      float me2 = dist(man.mx, man.my, enemy2.x, enemy2.y); 
      float me3 = dist(man.mx, man.my, enemy3.x, enemy3.y); 
      float me4 = dist(man.mx, man.my, enemy4.x, enemy4.y); 
    
    
      if (mode == 0) {
        background(0);
    
            pushMatrix();
        fill(255);
    
        image(msc, 0, 0, width, height);
        
        textSize(14);
        back -= 2;
        text("Extend your arms to punch the infection away from you. Try to hit the enemy as many times as possible before becoming infected!", 600+back, 160);
        
        textSize(25);
    
        translate(width/2-40, height/2  + 105);
    
        textAlign(LEFT);
    
        text("PUNCH", 0, 0);
        text("TURN", 0, 50);
        text("BEGIN!", 0, 100);
    
        popMatrix();
    
        man.drawMan();
        man.checkMouse();
    
        pushMatrix();
        translate(0, -150);
        image(logo, 0, 0, width, height);
        popMatrix();
      } 
    
    
    
      if(mode == 1) {
    
    
    
        background(0);
    
        fill(255, 0, 0);
        textSize(100);
    
        //    text(score, width/2, height/2 + 200);
    
        enemy1.drawEnemy();
        enemy2.drawEnemy();
        enemy3.drawEnemy();
        enemy4.drawEnemy();
        man.drawMan();
        man.checkMouse();
    
    
        if ((me1 <= man.dia*.8) || (me2 <= man.dia*.8) || (me3 <= man.dia*.8) || (me4 <= man.dia*.8)) {
          mode = 2;
        }
      }
    
    
    
      if (mode == 2) {
        background(0);
        fill(255);
        textSize(25);
    
        image(esc, 0, 0, width, height);
        
        textAlign(CENTER);
    
        pushMatrix();
        translate(width/2, height/2 - 125);
        text("You have been infected!", 0, 0);
        text("Your score is", 0, 50);
        
        fill(255, 0, 0);
        textSize(100);
        text(score, 0, 150);
        popMatrix();
    
        textAlign(LEFT);
        fill(255);
        
    
        
        textSize(25);
        
        pushMatrix();
        translate(width/2-40, height/2 - 125);
        text("PLAY AGAIN", 0, 200);
        text("MAIN SCREEN", 0, 250);
        popMatrix();
      }
    }
    
    void reset() {
      enemy1.x = width/8;
      enemy1.y = random(height/8, height - height/8);
      enemy2.x = width/8;
      enemy2.y = random(height/8, height - height/8);
      enemy3.x = width-width/8;
      enemy3.y = random(height/8, height - height/8);
      enemy4.x = width-width/8;
      enemy4.y = random(height/8, height - height/8);
      enemy1.speed = 4;
      enemy2.speed = 4;
      enemy3.speed = 4;
      enemy4.speed = 4;
      man.scalar = man.dia/2;
      score = 0;
    }
    
    
    
    
    void keyPressed() {
      if (mode == 0) {
        if(key == CODED) {
          if(keyCode == RIGHT) {
            man.moveRIGHT();
          } 
          else if(keyCode == LEFT) {
            man.moveLEFT();
          }
        }
        if(key == 10) {
          mode = 1;
        }
      } 
      else if (mode == 1) {
        if(key == CODED) {
          if(keyCode == RIGHT) {
            man.moveRIGHT();
          } 
          else if(keyCode == LEFT) {
            man.moveLEFT();
          }
        }
      } 
      else if (mode == 2) {
        if(key == CODED) {
          if(keyCode == UP) {
            mode = 0;
            reset();
          }
        }
        if(key == 10) {
          mode = 1;
          reset();
        }
      }
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Alex Burtson

    Project 4 - Boxed

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    Punch away as many enemies as you can before getting infected.

    You need to login/register to comment.