Learning to Program with Processing 3

On February 19, 2019 I have completed my course on programming with Processing. Over the few weeks, I have learned to write functions for Processing, configure parameters, use proper syntax, commenting, and file management followed by weekly challenges where I have developed basic files that are playable. Among these projects was developing a Pong-like game and a Chase Games for which the code is provided here. If you want to try out both codes and play it, please follow the instructions on Processing. Files are here.

Chase Game

Task:
Simple chase game where 3 Chaser objects chase a Player object around the graphics window. The Player object just follows the mouse.

Conditions:
The Chaser objects have 2 special behaviors:
If a chaser collides with the player
it jumps to a random location & the score decrements by 100
Occasionally (0.3% chance in each animation frame)
the chaser jumps on its own without a penalty
Otherwise, the chaser just chases the player as the default behavior

The player’s goal is to avoid the chasers as much as possible.
The score increments by 1 every frame, minus 100 points per collision.
The game runs until the player has been caught 10 times (10 collisions),
whereupon “Game Over” is displayed, and the score at that point becomes
the final score.

This is the code created for the game:

// Global variables
Player p;              // 1 player
Chaser c1;             // 3 chasers
Chaser c2;
Chaser c3;
float side = 50;       // Length of a side of the square used for all logos
int score = 0;         // Increments each frame; -100 when player caught
int collisions = 0;    // Counts collisions between player & a chaser
int maxCol = 10;       // Game over when collisions reaches this value

void setup()
{
  size(400, 400);
  background(255);
  colorMode(HSB);      // Easier for random colors
  rectMode(CENTER);    // Make reference points center instead of corner

  // Create the actual objects
  p = new Player(side);
  c1 = new Chaser(side);
  c2 = new Chaser(side);
  c3 = new Chaser(side);
}

void draw()
{
  background(255);

  // If the game is still active
  if (collisions < maxCol)
  {
    // First move everybody
    p.move();
    c1.move(p);
    c2.move(p);
    c3.move(p);

    // Then display everybody
    p.display();
    c1.display();
    c2.display();
    c3.display();

    // Increment the score to reflect game still active
    score = score + 1;
  }  
  // Else it's game over (game no longer active)
  else
  {
    fill(160, 255, 255);
    textSize(40);
    text("Game Over", width/2-100, height/2-10);
  }
  
  // Display the score in lower left corner of the graphics window
  fill(160,255,255);
  textSize(20);
  text("Score = " + score, 7, height-30);
  text("Caught " + collisions + " times", 7, height-7);
}
class Chaser
{
  // Attributes
  float xLoc;          // Location
  float yLoc;
  float side;          // Length of a side of the square logo
  float hue;           // Hue of the square logo
  float speed = 1;     // Speed in each dimension, 1 pixel per frame
  
  // Initialize size, generate random hue, start at a random place
  Chaser(float s)
  {
    side = s;          // 4
    hue = random(255); // 4
    jump();            // 4
  }
  
  // Handles 3 possible moves: collision with p, random jump, or chase p
  // Player p passed in as parameter so that Chaser can see where it is
  void move(Player p)
  {
    // If chaser caught the player (it collided with Player p)
    if (collide(p))
    {
      // Count the collision, assess the penalty, jump to a random place
      collisions = collisions + 1;     // 8
      score = score - 100;             // 8
      jump();                          // 8
    }
    
    // Every now and then (0.3% chance), jump anyway with no penalty
    else if (random(1000) < 3)         // 9
      jump();                          // 9
    
    // Otherwise Chaser chases Player
    else
    {
      // Move either left
      if (p.xLoc < this.xLoc)          // 10
        this.xLoc = this.xLoc - speed; // 10
      // or right
      else                             // 10
        this.xLoc = this.xLoc + speed; // 10

      // Move either up
      if (p.yLoc < this.yLoc)          // 11
        this.yLoc = this.yLoc - speed; // 11
      // or down
      else                             // 11
        this.yLoc = this.yLoc + speed; // 11
    }
  }
  
  // Moves Chaser to random place on graphics window
  void jump()
  {
    xLoc = random(width);              // 6
    yLoc = random(height);             // 6
  }
  
  // Collision detector; returns true if Chaser % p overlap, false o/w
  boolean collide(Player p)
  {
    if (abs(p.xLoc - this.xLoc) < side &&  // 7
        abs(p.yLoc - this.yLoc) < side)    // 7
      return true;                         // 7
    else                                   // 7
      return false;
  }
  
  // Chaser logo is just a square centered on current location
  void display()
  {
    fill(hue,255,255);          // 5
    strokeWeight(2);            // 5
    rect(xLoc,yLoc,side,side);  // 5
  }
}
class Player
{
  // Attributes
  float xLoc;          // Location
  float yLoc;
  float side;          // Length of a side of the square logo
  float hue;           // Hue of the square logo
  
  Player(float s)
  {
    // Start Player in center of screen
    xLoc = width/2;    // 1
    yLoc = height/2;   // 1
    side = s;          // 1
    hue = random(255); // 1
  }
  
  // Movement just tracks the mouse
  void move()
  {
    xLoc = mouseX;     // 3
    yLoc = mouseY;     // 3
  }
  
  // Player is just a square
  void display()
  {
    fill(hue,255,255);         // 2
    strokeWeight(2);           // 2
    rect(xLoc,yLoc,side,side); // 2
  }
}

Leave a comment