// bola pong 1.0 - Daniel Melo Ribeiro www.danielmelo.net // Mais um estudo de programacao orientada a objeto com Processing // variaveis para as cores dos objetos int r; int g; int b; // declaracao dos objetos da classe bola pong Bolapong pelota1; Bolapong pelota2; Bolapong pelota3; void setup () { // tamanho da tela, background preto, sem fio e com smooth size (600, 400); background (0); noStroke(); smooth(); //sorteia numeros entre 0 e 255 para as cores dos objetos r = int(random(256)); g = int(random(256)); b = int(random(256)); //inicializa o objeto do tipo bolapong: cor, posicao inicial x, posicao inicial y e tamanho pelota1 = new Bolapong (color(r, g, b) ,(random(width)), (random(height)), 4); pelota2 = new Bolapong (color(b, g, r) ,(random(width)), (random(height)), 4); pelota3 = new Bolapong (color(g, r, b) ,(random(width)), (random(height)), 4); } void draw () { pelota1.mostrabola(); pelota1.andabola(); pelota2.mostrabola(); pelota2.andabola(); pelota3.mostrabola(); pelota3.andabola(); } class Bolapong { //caracteristicas da bola pong: tamanho, posicao e cor color cor; float posx; float posy; int tamanho; int sentidox=10; int sentidoy=10; Bolapong (color cor_c, float posx_x, float posy_y, int tamanho_t) { //recebe via parametro os atributos de cada bola pong cor = cor_c; posx = posx_x; posy = posy_y; tamanho = tamanho_t; } void mostrabola () { // posiciona e exibe a bola fill(cor, 100); ellipse (posx, posy, tamanho, tamanho); } void andabola () { //desloca a bola na horizontal e vertical posx = posx + sentidox * abs(sin(random(0,0.5))); posy = posy + sentidoy * abs(sin(random(0,0.5))); // no eixo x, ao chegar `as bordas, inverte o sentido, colocando a variavel sentido como negativa if ((posx > width) || (posx < 0)) { sentidox = sentidox*(-1); } // no eixo y, ao chegar `as bordas, inverte o sentido, colocando a variavel sentido como negativa if ((posy > height) || (posy < 0)) { sentidoy = sentidoy*(-1); } } }