Giant Robots and Little People

The program below has been extended to contain 2 robots and a small player person. 
Copy it into Processing and run it.  You can use the 'h' and 'u' keys to move the player.
(continue reading below)

void setup()
{
  size(800,600);
  background(255,255,255);
  smooth();
  frameRate(20);


int x=100;
int y=50;

int x2=200;
int y2=100;

int xp=350;
int yp=350;

void draw()
{
  background(255);
 
  if(keyPressed)
  {
     if(key=='h')
     { xp = xp-2; }
     if(key=='u')
     { yp = yp-2; }
  }

  player(xp,yp);
 
  roundbot(x,y);
  x = x+10;
  if(x>800)
  { x = 0; }
 
  squarebot(x2,y2);
  y2 = y2+10;
  if(y2>600)
  { y2 = 0;}
}

void roundbot(int x,int y) 
{
  strokeWeight(5);
  fill(200,200,200);
  stroke(0,0,0);
  ellipse(50+x,120+y,100,200);
  ellipse(50+x,20+y,100,40);
  ellipse(30+x,220+y,40,40);
  ellipse(70+x,220+y,40,40);
}

void squarebot(int x,int y)
{
  strokeWeight(5);
  stroke(0,0,0);
  fill(128,0,0);
  rect(0+x,0+y,60,60);
  rect(20+x,60+y,20,20);
  fill(255,192,0);
  rect(0+x,80+y,60,100);
  fill(0,0,0);
  rect(-10+x,170+y,30,30);
  rect(40+x,170+y,30,30);
}

void player(int x,int y)
{
  strokeWeight(1);
  stroke(0,0,0);
  fill(250,200,150);
  ellipse(20+x,20+y,15,20);
  fill(0,0,255);
  rect(10+x,30+y,20,25);
  rect(5+x,30+y,6,30);
  rect(29+x,30+y,6,30);
  fill(0,0,100);
  rect(11+x,55+y,9,25);
  rect(20+x,55+y,9,25);
}


Here are the commands that take care of moving the player:
       ........
int xp=350;
int yp=350;

void draw()
{
  background(255);
 
  if(keyPressed)              // check whether a key is pressed
  {                                        //  if so, then
     if(key=='h')      //  if 'h' is pressed       
     { xp = xp-2; }    //   subtract 2 from xp (move left)
     if(key=='u')      //  if 'u' is pressed
     { yp = yp-2; }    //   subtract 2 from yp (move up)
  }

 
player(xp,yp);
.......

Practice
  1. Change the keys that are used for moving left and up.
    You won't be able to use arrow keys - choose any two letters.
    Maybe to use a,w,d,x for moving your player.
  2. Make the player move faster than 2 pixels at a time.
  3. Place a target in the top left corner of the screen.
    This should be an image, stored in a PImage variable,
    loaded with loadImage..., and displayed with the image command.
    For example, it could be a small frog picture.
  4. The goal of the game is to reach the target.
    Add commands to return to the start when the target is reached.
    Something like this:
         if(xp < 100  &&  yp < 100)      // && means "and"
         { xp = 400; yp = 400;}          // return to start

  5. Add commands so it's possible to move up and down and right and left.
  6. Make any other changes you wish, as time permits today.
    For example, make a new METHOD (void) for another object
    and make that object move from right to left.  Add as many
    moving objects as time permits.
Of course this game would be better if the player needed to avoid the robots -
or if he could shoot the robots.  We'll learn how to detect collisions next class.