ArrayList ants = new ArrayList();
int NumAnts = 30;
PImage bg;
boolean keys;
Player player;
void setup() {
size(600, 600);
frameRate(60);
player = new Player();
bg = loadImage(“grass.png”);
for (int i = 0; i < NumAnts; i++)
ants.add(new Ant());
keys = new boolean[128];
}
void draw() {
image(bg, 0, 0);
move();
for (int i = 0; i < NumAnts; i++)
ants.get(i).drawAnt();
player.drawPlayer();
}
void move() {
int xDelta = 0;
int yDelta = 0;
if (keys[‘w’])
yDelta–;
if (keys[‘s’])
yDelta++;
if (keys[‘a’])
xDelta–;
if (keys[‘d’])
xDelta++;
player.updatePlayer(xDelta, yDelta);
}
void keyPressed() {
keys[key] = true;
}
void keyReleased() {
keys[key] = false;
}
class Ant {
PVector speed;
PImage img;
public float xDelta, yDelta, w1, h1;
Ant() {
xDelta = random(50, 450);
yDelta = random(50, 450);
w1 = 30;
h1 = 30;
speed = new PVector (random(-0.8, 1), random(-0.8, 1));
img = loadImage(“ant.png”);
}
void drawAnt() {
moveAnt();
pushMatrix();
translate(xDelta + img.width/2, yDelta + img.height/2);
rotate(speed.heading() + PI/2);
translate(-img.width/2, -img.height/2);
image(img, 0, 0, w1, h1);
popMatrix();
}
void moveAnt() {
//90% of the time go striaght
//6% of the time waver right or left
//4% of the time make a larger movement left or right
float testMove = random(0, 1);
if (testMove < 0.1) {
speed.rotate(random(-0.7, 0.7));
}
if (testMove > 0.97) {
speed.rotate(random(-1, 1));
}
xDelta = xDelta + speed.x;
yDelta = yDelta + speed.y;
if(isAntOffScreen() == true) {
xDelta = xDelta - speed.x;
yDelta = yDelta - speed.y;
speed.rotate(random(-2, 2));
}
}
boolean isAntOffScreen() {
if(xDelta < 30 || xDelta > width - 30 ||
yDelta < 10 || yDelta > height - 30)
return true;
return false;
}
}
class Player {
public int x2, y2, w2, h2;
PImage spriteSheet;
PImage movement;
boolean inMotion;
int currentDirection;
float currentFrame;
final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;
Player() {
x2 = 570;
y2 = 544;
w2 = 16;
h2 = 64;
inMotion = false;
currentDirection = 1;
currentFrame = 0;
setupSprites();
}
void setupSprites() {
movement = new PImage[4][9];
spriteSheet = loadImage("theprofessorgreen.png");
for (int i =0; i < 9; i++) {
movement[0][i] = spriteSheet.get(w2 + h2 * i, 8, 32, 56);
movement[1][i] = spriteSheet.get(w2 + h2 * i, 72, 32, 56);
movement[2][i] = spriteSheet.get(w2 + h2 * i, 136, 32, 56);
movement[3][i] = spriteSheet.get(w2 + h2 * i, 200, 32, 56);
}
}
void drawPlayer() {
if (inMotion)
image(movement[currentDirection][1 + int(currentFrame)], x2, y2);
else
image(movement[currentDirection][0], x2, y2);
}
void updatePlayer(int xDelta, int yDelta) {
currentFrame = (currentFrame + 0.3) % 8;
inMotion = true;
if (xDelta == 0 && yDelta == 0)
inMotion = false;
else if (xDelta == -1)
currentDirection = LEFT;
else if (xDelta == 1)
currentDirection = RIGHT;
else if (yDelta == -1)
currentDirection = UP;
else if (yDelta == 1)
currentDirection = DOWN;
x2 = x2 + xDelta;
y2 = y2 + yDelta;
if(isPlayerOffScreen(x2, y2)) {
x2 = x2 - xDelta;
y2 = y2 - yDelta;
}
}
boolean isPlayerOffScreen(float x, float y) {
if (x2 < 0 || x2 > width - 30 ||
y2 < 0 || y2 > height - 56)
return true;
return false;
}
}