Skip to content

Instantly share code, notes, and snippets.

@lily-mara
Created November 19, 2014 14:06
Show Gist options
  • Save lily-mara/8c808299e3b263b56040 to your computer and use it in GitHub Desktop.
Save lily-mara/8c808299e3b263b56040 to your computer and use it in GitHub Desktop.
ArrayList<Sprite> sprites;
int current = 0;
void setup() {
size(400, 400);
sprites = new ArrayList<Sprite>();
sprites.add(new Sprite(width/2, height/2));
}
void draw() {
background(127);
drawCurrentArrow();
makePlatform(350, 300, 80, 10);
makePlatform(200, 380, 10, 40);
for (Sprite s : sprites) {
for (Sprite i : sprites) {
s.repel(i);
}
s.update();
s.display();
}
}
// Makes a platform (could be a wall, floor, or ceiling),
// drawing it AND telling sprites to repel it (the repelling is
// important because it is how the sprite becomes aware of it).
// Note that the platform is defined by its CENTER (x, y) and
// its width and height.
void makePlatform(float x, float y, float w, float h) {
rectMode(CENTER);
fill(200, 70, 70);
rect(x, y, w, h);
// Tell all sprites about the platform location
for (Sprite s : sprites) {
s.repel(x, y, w, h);
}
}
// draw a red arrow over the head of the current sprite
void drawCurrentArrow() {
float x = sprites.get(current).getX();
float y = sprites.get(current).getY() - 45;
fill(255, 0, 0);
triangle(x, y, x+10, y-10, x-10, y-10);
}
// Control the sprites with the keyboard
void keyPressed() {
Sprite s = sprites.get(current);
if (key == CODED) {
if (keyCode == RIGHT) {
s.moveRight();
} else if (keyCode == LEFT) {
s.moveLeft();
} else if (keyCode == UP) {
s.jump();
} else if (keyCode == DOWN) {
s.crouch();
}
} else if (key == ' ') {
current = (current + 1) % sprites.size();
}
}
// create a new Sprite at the mouse position when the mouse is pressed
void mousePressed() {
sprites.add(new Sprite(mouseX, mouseY));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment