Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Last active December 20, 2015 14:29
Show Gist options
  • Select an option

  • Save jordanorelli/6147343 to your computer and use it in GitHub Desktop.

Select an option

Save jordanorelli/6147343 to your computer and use it in GitHub Desktop.
import java.awt.AWTException;
import java.awt.Robot;
Robot robot;
float turn;
boolean SOOPASPEED;
int fullspeed = 6;
int regspeed = 1;
PVector pos;
PVector velocity = new PVector(0, 0); // initially, you're not moving.
void setup() {
size(displayWidth, displayHeight, P2D);
noCursor();
smooth();
robot = makeRobot();
pos = new PVector(width / 2, height / 2);
}
void draw() {
background(255);
fill(0);
drawPlayer();
updatePosition();
}
void drawPlayer() {
pushMatrix();
translate(pos.x, pos.y);
rotate(turn);
noFill();
strokeWeight(4.0);
stroke(0);
triangle(10.0, 0.0, 0.0, -34.6, -10.0, 0.0);
popMatrix();
}
void updatePosition() {
PVector v = velocity.get();
v.rotate(turn);
if (SOOPASPEED) {
v.mult(fullspeed);
}
pos.add(v);
wrap(pos);
}
void mouseMoved() {
turn += 0.0025 * constrain(mouseX - width / 2, -100, 100);
robot.mouseMove(width / 2, height / 2);
}
void wrap(PVector p) {
if (p.x > width) {
p.x = 0;
}
else if (p.x < 0) {
p.x = width;
}
if (p.y > height) {
p.y = 0;
}
else if (p.y < 0) {
p.y = height;
}
}
Robot makeRobot() {
try {
return new Robot();
}
catch (AWTException e) {
e.printStackTrace();
}
return null;
}
boolean sketchFullScreen() {
return true;
}
void keyPressed() {
if (key == 'a' || key == 'A') {
velocity.x -= regspeed;
}
else if (key == 'd' || key == 'D') {
velocity.x += regspeed;
}
else if (key == 's' || key == 'S') {
velocity.y += regspeed;
}
else if (key == 'w' || key == 'W') {
velocity.y -= regspeed;
}
else if (key == CODED && keyCode == SHIFT) {
SOOPASPEED = true;
}
}
void keyReleased() {
if (key == 'a' || key == 'A') {
velocity.x += regspeed;
}
else if (key == 'd' || key == 'D') {
velocity.x -= regspeed;
}
else if (key == 's' || key == 'S') {
velocity.y -= regspeed;
}
else if (key == 'w' || key == 'W') {
velocity.y += regspeed;
}
else if (key == CODED && keyCode == SHIFT) {
SOOPASPEED = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment