Hello
1
so far i have this and i need help in making the circle get collected
int x;
int y;
float circleX;
float circleY;
int points = 0;
void setup(){
textSize(20);
size (400, 400);
x = width/2;
y = height/2;
circleX = random(0,400);
circleY = random(0,400);
}
void draw(){
background(0,255,0);
fill(255,0,0);
rect(x, y, 20,20);
fill(255,255,0);
ellipse(circleX, circleY, 6,6);
fill(0);
text("Score :" + points, 0,20);
}
void keyPressed() {
if ((keyPressed == true) && (key == CODED)) {
if (keyCode == UP){
y --;
} else if (keyCode == DOWN) {
y ++;
} else if (keyCode == LEFT){
x --;
} else if (keyCode == RIGHT) {
x ++;
}
}
//collision with circle
if((x >= circleX && y >= circleY)){
}else if( x <= circleX && y <= circleY){
ellipse(circleX, circleY, 6,6);
points++;
}
}
1 Like
@jeremydouglass has an old post at this thread linking to this page which you should probably read
1 Like
ok, this is the complete code.
int x;
int y;
float circleX;
float circleY;
int points = 0;
void setup() {
textSize(20);
size (400, 400);
x = width/2;
y = height/2;
circleX = random(0, 400);
circleY = random(0, 400);
rectMode(CENTER);
}
void draw() {
background(0, 255, 0);
fill(255, 0, 0);
rect(x, y, 20, 20);
fill(255, 255, 0);
ellipse(circleX, circleY, 6, 6);
fill(0);
text("Score :" + points, 0, 20);
}
void keyPressed() {
if ((keyPressed == true) && (key == CODED)) {
if (keyCode == UP) {
y --;
} else if (keyCode == DOWN) {
y ++;
} else if (keyCode == LEFT) {
x --;
} else if (keyCode == RIGHT) {
x ++;
}
}
//collision with circle
if(is_player_on_coin()){
points++;
circleX = random(width);
circleY = random(height);
}
}
boolean is_player_on_coin(){
float d = dist(x, y, circleX, circleY);
//we assuming the player as a circle
// player radius = 10
// circle radius = 3
if(d < 3 + 10){
return true;
}
return false;
}
1 Like
this is how the distance between two circle calculated
solve the distance using pythagorean theorem
if the distance less than player radius + coin radius, then the player is on coin
2 Likes