Skip to content

Instantly share code, notes, and snippets.

Created July 18, 2013 14:15
Show Gist options
  • Save anonymous/6029691 to your computer and use it in GitHub Desktop.
Save anonymous/6029691 to your computer and use it in GitHub Desktop.
select a challenger in given range randomly;
int total = 100;
int range = 10;
int player = 50;
void setup() {
size(640, 480);
frameRate(5);
}
void draw() {
background(200);
fill(0);
for (int i=0; i<total; i++)
ellipse(width/(float)total*i, height/2, 5, 5);
ellipse(width/(float)total*player, height/2, 10, 10);
int challenger = getChallenger(player, total, range);
fill(255);
ellipse(width/(float)total*challenger, height/2, 10, 10);
}
void mousePressed() {
player = (int)(mouseX/(width/(float)total));
}
int getChallenger(int player, int total, int range) {
int left = min(player, range);
int right = min(total-(player+1), range);
int offset = (int)random(left+right)-left;
offset += offset<0?0:1;
return player+offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment