Created
April 28, 2014 14:09
-
-
Save cangevine/11373279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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