Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

how to do this game

$
0
0

Hi, im doing a project to university and i need some help , how i can collide the ellipses with the paddle that i draw and, how i can control an ellipse with mouse and she has to collide with the others and finally make a goal and their balls go out the goal and go back when she dont hit goal .

Their is the code, PLZ HELP ME

import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.effects.*; import ddf.minim.signals.*; import ddf.minim.spi.*; import ddf.minim.ugens.*;

AudioPlayer sound ; Minim minim; PImage background ; int totalBolas = 16; float contadorX = 10, y=10, passo=3; Bola[] Bolas = new Bola[totalBolas];

void setup() { size(800,400);

background = loadImage("pokemon1.jpg");

minim= new Minim(this); sound = minim.loadFile("106-the-road-to-viridian-city-from-palette.mp3"); sound.loop();

for (int i = 0; i < totalBolas; i++) { Bola bola = new Bola(); bola.x = random(width); bola.y = random(height); bola.vx = random(10) - 5; bola.vy = random(10) - 5; bola.raio = 14; Bolas[i] = bola; bola.cor = color(random(255), random(255), random(255)); } }

void draw() { background(background); fill(color(255, 0, 0)); for (int i = 0; i < totalBolas; i++) { Bolas[i].x += Bolas[i].vx; Bolas[i].y += Bolas[i].vy; checkwallcolison(Bolas[i]); Bolas[i].desenha(); } checkballcolision(); rectMode(CENTER); //rect(width/2,0,350,100); //rectMode(CENTER);

rectMode(CORNER); strokeWeight(1); fill (255,0,0); noStroke(); rect (contadorX,y+10,80,20); contadorX = contadorX+passo; if ((contadorX >= width-90) || (contadorX <=0)) passo = passo*(-1);

}

void checkballcolision() { for (int i = 0; i< totalBolas; i++) { for ( int j= i +1; j <totalBolas; j++) { float dx = Bolas[j].x - Bolas[i].x;//distancia de x float dy = Bolas[j].y - Bolas[i].y;//distancia de y float dist = sqrt(dxdx + dydy);//teorema de pitagoras if (dist<(Bolas[j].raio + Bolas[i].raio)) { //OS Circulos tocam-se float normalX = dx/dist; float normalY = dy/dist;

    //encontrar  ponto médio
    float midpointX = (Bolas[i].x + Bolas[j].x)/2;
    float midpointY = (Bolas[i].y + Bolas[j].y)/2;


    //bolas tocam-se e mexem-se
    Bolas[i].x = midpointX - Bolas[i].raio * normalX;
    Bolas[i].y = midpointY - Bolas[i].raio*  normalY;
    Bolas[j].x = midpointX + Bolas[i].raio * normalX;
    Bolas[j].y = midpointY + Bolas[i].raio*  normalY;

    float dVector = ( Bolas[i].vx-Bolas[j].vx) * normalX;
    dVector += (Bolas[i].vy - Bolas[j].vy) * normalY;

    float dvx = dVector * normalX;
    float dvy = dVector * normalY;

    Bolas[i].vx -= dvx;
    Bolas[i].vy -= dvy ;
    Bolas[j].vx += dvx;
    Bolas[j].vy += dvy;
  }
}

} } void checkwallcolison(Bola bola) {

if (bola.x < bola.raio) { bola.x = bola.raio; bola.vx *= -1 ; } if (bola.x > width - (bola.raio)) { bola.x = width - (bola.raio); bola.vx *= -1; }
//if (bola.y < bola.raio) { //bola.y = bola.raio; //bola.vy *= -1; //}
if (bola.y > height - (bola.raio)) { bola.y = height - (bola.raio); bola.vy *= -1; } }

class Bola { float x = 0; float y = 0; float vx = 0; float vy = 0; float raio= 5; color cor; void desenha() { fill(cor);
ellipse(x, y, raio * 2, raio * 2); } }


Viewing all articles
Browse latest Browse all 2896

Trending Articles