Ellipse colision
in
Programming Questions
•
9 months ago
So I have this game: It's ellipses moving randomly and you have to hit them. The only thing I need is to make them collide against each other. Can anyone help me?
(to start you have to move the mouse to the ellipse in the middle)
thanks in advance
- int numBolas = 10;
- int pont, contador;
- int bolas_pretas;
- int contador_tempo=1;
- int starttime;
- boolean acertou,ja_entrou,menu=true;
- Bola myBola = new Bola(20, 20, 1, 0.5);
- Bola[] bolas = new Bola[numBolas];
- void setup()
- {
- size(500, 500);
- for (int i=0; i<numBolas;i++) {
- bolas[i] = new Bola(random(width), random(height), random(1, 6), random(1, 6));
- }
- }
- void draw() {
- background(255);
- if(menu==true){
- fill(255);
- rect(0,0,width,height);
- fill(0);
- text("Posiciona o rato na circufrência para começar", 30, 25);
- ellipseMode(CENTER);
- ellipse(width/2,height/2,30,30);
- //desenho da mira:
- stroke(2);
- noFill();
- ellipse(mouseX, mouseY, 30, 30);
- line(mouseX+7.5, mouseY, mouseX+22.5, mouseY);
- line(mouseX-7.5, mouseY, mouseX-22.5, mouseY);
- line(mouseX, mouseY+7.5, mouseX, mouseY+22.5);
- line(mouseX, mouseY-7.5, mouseX, mouseY-22.5);
- }
- if((mouseX ==width/2) && (mouseY==height/2))menu=false;
- if(menu==false){
- text(pont, 30, 25);
- for (int i=0; i<numBolas; i++) {
- bolas[i].update();
- bolas[i].mousePressed();
- bolas[i].tempo();
- bolas[i].desenha();
- }
- //desenho da mira:
- stroke(2);
- noFill();
- ellipse(mouseX, mouseY, 30, 30);
- line(mouseX+7.5, mouseY, mouseX+22.5, mouseY);
- line(mouseX-7.5, mouseY, mouseX-22.5, mouseY);
- line(mouseX, mouseY+7.5, mouseX, mouseY+22.5);
- line(mouseX, mouseY-7.5, mouseX, mouseY-22.5);
- }
- if (millis()>=starttime+1000) {
- starttime=millis();
- contador_tempo++;
- ja_entrou=false;
- }
- if (contador_tempo%10==0) {
- if(ja_entrou==false){
- pont--;
- ja_entrou=true;
- }
- }
- }
- void mousePressed() {
- contador_tempo=1;
- }
- void mouseReleased() {
- pont+=5*bolas_pretas;
- if (bolas_pretas>=4) {
- pont+=5;
- }
- if (acertou==false) {
- pont-=5;
- }
- acertou=false;
- }
- class Bola {
- float ypos, xpos, speedx, speedy, dista, cor1, cor2, cor3;
- int score=0;
- boolean count;
- boolean esta_preta;
- Bola (float x, float y, float s, float s2) {
- xpos = x;
- ypos = y;
- speedy=s2;
- speedx = s;
- cor1=random(50,255);
- cor2=random(50,255);
- cor3=random(255);
- count=true;
- }
- void desenha() {
- fill(cor1, cor2, cor3);
- ellipse(xpos, ypos, 40, 40);
- }
- void update() {
- xpos += speedx;
- ypos += speedy;
- if (xpos>=480) speedx=-random(0.5, 4);
- if (ypos>=480) speedy=-random(0.5, 4);
- if (xpos<=20) speedx=random(0.5, 4);
- if (ypos<=20) speedy=random(0.5, 4);
- }
- void mousePressed() {
- menu=false;
- if (mousePressed && (mouseButton == LEFT)) {
- dista=dist(xpos, ypos, mouseX, mouseY);
- // 15 é o raio das bolas
- if (dista<=15) {
- if(esta_preta==false){
- bolas_pretas++;
- }
- acertou=true;
- count=false;
- cor1=0;
- cor2=0;
- cor3=0;
- esta_preta=true;
- }
- }
- }
- void tempo() {
- score=score+1;
- if (score>=300) {
- cor1=random(50,255);
- cor2=random(50,255);
- cor3=random(50,255);
- count=true;
- bolas_pretas--;
- esta_preta=false;
- }
- if (count) {
- score=0;
- }
- }
- }
1