-
-
Save rogerallen/a3ccae2946dfa0a9d625 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
// global variables | |
int top_x = 250; | |
int top_y = 150; | |
int cubert_move = 50; | |
int cubert_x = top_x; | |
int cubert_y = top_y; | |
float MOVE_TIME = 0.5; | |
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 rows = 0; rows < 5; rows++) { | |
x = top_x - rows*cubert_move; | |
for (int columns = 0; columns < rows + 1; columns++) { | |
fill(200, 200, 200); | |
ellipse(x, y, 50, 30); | |
x += 2*cubert_move; | |
} | |
y += cubert_move; | |
} | |
} | |
boolean cubert_outside_board() { | |
if(cubert_y < top_y) { | |
return(true); | |
} | |
return(false); | |
} | |
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