Last active
August 29, 2015 14:07
-
-
Save stuartallen/fc5d06afaacf3be572d6 to your computer and use it in GitHub Desktop.
Cubert
This file contains hidden or 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
// constants | |
int TOP_X = 250; | |
int TOP_Y = 150; | |
int ROWS = 5; | |
int CUBERT_MOVE = 50; | |
float MOVE_TIME = 0.5; | |
// global variables | |
int cubert_x = TOP_X; | |
int cubert_y = TOP_Y; | |
float cubert_move_time = -1.0; | |
int cubert_start_x, cubert_start_y, | |
cubert_end_x = TOP_X, cubert_end_y = TOP_Y; | |
void setup() { | |
background(0); | |
frameRate(30); | |
size(500, 500); | |
} | |
void draw_board() { | |
int x = TOP_X; | |
int y = TOP_Y + 10; | |
for (int row = 0; row < ROWS; row++) { | |
x = TOP_X - row*CUBERT_MOVE; | |
for (int columns = 0; columns < row + 1; columns++) { | |
fill(200, 200, 200); | |
ellipse(x, y, 50, 30); | |
x += 2*CUBERT_MOVE; | |
} | |
y += CUBERT_MOVE; | |
} | |
} | |
boolean cubert_outside_board() { | |
// if all these are true, it is inside the board | |
if( (cubert_y <= TOP_Y + (ROWS-1)*CUBERT_MOVE) && // above bottom | |
(cubert_x - 100 - cubert_y <= 0) && // inside right diagonal | |
(-cubert_x + 400 - cubert_y <= 0)) { // inside left diagonal | |
return(false); | |
} | |
return(true); | |
} | |
void update_cubert() { | |
if (cubert_move_time > 0.0) { | |
float t = (MOVE_TIME - cubert_move_time)/MOVE_TIME; // FIXME? | |
cubert_x = (int)lerp(cubert_start_x, cubert_end_x, t); | |
cubert_y = (int)lerp(cubert_start_y, cubert_end_y, t); | |
cubert_move_time -= 1/30.0; | |
} else { | |
cubert_x = cubert_end_x; | |
cubert_y = cubert_end_y; | |
if (cubert_outside_board()) { | |
cubert_move_time = MOVE_TIME; | |
cubert_start_x = cubert_x; | |
cubert_start_y = cubert_y; | |
cubert_end_x = cubert_x; | |
cubert_end_y = cubert_y + 9*CUBERT_MOVE; | |
} | |
} | |
} | |
void draw_cubert() { | |
fill(200, 0, 0); | |
ellipse(cubert_x, cubert_y, 25, 30); | |
} | |
void move_cubert(int dx, int dy) { | |
if (cubert_move_time > 0.0) { | |
return; | |
} | |
cubert_move_time = MOVE_TIME; | |
cubert_start_x = cubert_x; | |
cubert_start_y = cubert_y; | |
cubert_end_x = cubert_x + dx*CUBERT_MOVE; | |
cubert_end_y = cubert_y + dy*CUBERT_MOVE; | |
} | |
void draw() { | |
update_cubert(); | |
background(0); | |
draw_board(); | |
draw_cubert(); | |
} | |
void keyPressed() { | |
if (key == 'd' || key == 'D') { | |
move_cubert(1, 1); | |
} else if (key == 'e' || key == 'E') { | |
move_cubert(1, -1); | |
} else if (key == 's' || key == 'S') { | |
move_cubert(-1, 1); | |
} else if (key == 'w' || key == 'W') { | |
move_cubert(-1, -1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment