Skip to content

Instantly share code, notes, and snippets.

@cangevine
Created April 28, 2014 14:09
Show Gist options
  • Save cangevine/11373279 to your computer and use it in GitHub Desktop.
Save cangevine/11373279 to your computer and use it in GitHub Desktop.
Train t;
void setup() {
t = new Train();
size(500, 500);
}
void draw() {
background(255);
t.update();
t.display();
}
void keyPressed() {
if (keyCode == UP) {
t.speedUp();
} else if (keyCode == DOWN) {
t.slowDown();
} else if (keyCode == RIGHT) {
t.turn("right");
} else if (keyCode == LEFT) {
t.turn("left");
}
}
void keyReleased() {
t.coast();
}
class Train {
PVector loc;
PVector vel;
PVector acc;
float angle;
Train() {
loc = new PVector(250, 250);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
angle = 0;
}
void update() {
vel.add(acc);
loc.add(vel);
}
void speedUp() {
acc = new PVector(0, -0.1);
}
void slowDown() {
acc = new PVector(0, 0.1);
}
void turn(String dir) {
if (dir == "left") {
angle = angle - 0.1;
} else if (dir == "right") {
angle = angle + 0.1;
}
}
void coast() {
acc.mult(0);
}
void display() {
pushMatrix();
translate(loc.x, loc.y);
rotate(angle);
rectMode(CENTER);
rect(0, 0, 20, 40);
popMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment