Skip to content

Instantly share code, notes, and snippets.

@koluku
Last active May 20, 2016 04:16
Show Gist options
  • Save koluku/26f2c9a16bafddb4fa5c5f86b893f956 to your computer and use it in GitHub Desktop.
Save koluku/26f2c9a16bafddb4fa5c5f86b893f956 to your computer and use it in GitHub Desktop.
void setup() {
size(400, 300);
}
float startX = 10;
float startY = 10;
float initDx = 1;
float initDy = 2;
float boalWidth = 3;
float boalHeight = 3;
float boardWidth = 50;
float boardHeight = 6;
int game = 3;
float x = startX;
float y = startY;
float dx = initDx;
float dy = initDy;
int score = 0;
int result[] = {};
int gameCount = 0;
boolean flag = true;
boolean exit = false;
float reflectSideX(float x) {
if(x < 0 || x > width) {
dx = dx * -1;
return dx;
}
return dx;
}
float reflectSideY(float y) {
if(y < 0) {
dy = dy * -1;
return dy;
}
return dy;
}
void boal() {
noStroke();
fill(255);
rect(x, y, boalWidth, boalHeight);
}
void board() {
fill(255);
if(mouseX - 25 <= 0) {
rect(0, 250, 50, 6);
}
else if(mouseX + 25 >= width) {
rect(width - 50, 250, boardWidth, boardHeight);
}
else {
rect(mouseX - 25, 250, boardWidth, boardHeight);
}
}
void info() {
fill(0);
text("score:", 10, height - 25);
text(score, 50, height - 25);
text("gameCount:", 10, height - 10);
text(gameCount, 85, height - 10);
}
void draw() {
background(192, 192, 255);
if(flag == true) {
if(y == 250 && x >= mouseX - boardWidth/2 && x <= mouseX + boardWidth/2) {
dy = dy * -1;
score++;
}
if(y == height) {
x = startX;
y = startY;
dx = initDx;
dy = initDy;
result = append(result, score);
score = 0;
gameCount++;
}
x = x + reflectSideX(x);
y = y + reflectSideY(y);
textSize(12);
boal();
board();
info();
if(gameCount == 3) {
flag = false;
}
}
else {
if(exit == true) {
delay(30000);
exit();
}
// result
textSize(32);
text("Game Over", width/2 - 80, 30);
textSize(16);
text("Automatically exit after 30 secands" ,width/2 - 130,70);
for(int i = 0;i<result.length;i++) {
int positionY = 100 + 20 * i;
text("game", width/2 - 45, positionY);
text(i + 1, width/2 + 5, positionY);
text(":", width/2 + 15, positionY);
text(result[i], width/2 + 35, positionY);
}
exit = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment