Objektbewegung

tyno1584

Mitglied
Kreis kreis;
Rechteck rechteck;
Bricks bricks;
int x,y,r; //Kreis Position nd grösse
int x2,y2,l2,b2; // Rechteck grösse und Position
int vx,vy; // Kreis Geschwindigkeit
int vx2,vy2;// Rechteck Geschwindigkeit
color c,c2; //Farben Kreis&Rechteck
int x3,y3;
int l3,b3;

int a;
void setup(){
x=100; y=350; r=25;
x2=200; y2=600; l2=150; b2=20;
vx=2; vy=2; vx2=2; vy2=2;
; c2=100;
x3= l3+b3; y3= 40;
l3=50; b3=20;
kreis = new Kreis(x,y,r,x2,y2,vx,vy,l2,b2,c2);
rechteck = new Rechteck(x,y,r,x2,y2,l2,b2,vx,vy,vx2,vy2,c2);
bricks = new Bricks(x3,y3,l3,b3,c);

size(650,650);



}
void draw(){

; bricks.zeichnen();
kreis.zeichnen();
kreis.kolrand();
rechteck.keyPressed();
rechteck.zeichnen();
kreis.kolobj();
rechteck.kolrand();


}

class Kreis{
Rechteck rechteck;
int x,y,r;
int vx,vy;
color c;
int x2,y2,l2,b2;




Kreis(int x0, int y0, int r0, int x20, int y20, int vx0, int vy0, int l20, int b20, int c0){ // defintion varaibelne
x=x0; y=y0; r=r0;x2= x20; y2=y20;vx=vx0; vy=vy0; l2=l20 ;b2=b20;c=c0;
}

void zeichnen(){
fill(c);circle(x,y,r);
x=x-vx; y=y+vy;
}
//Kreis kollidiert mit Rändern
void kolrand(){
if( (x+r/2>=width) || (x-r/2<=0) ){ vx=-vx;}
if( (y-r/2<=0) ){ vy=-vy;}
}
void kolobj(){
//Kollision an den Objekten
if (y>=y2 && y<=y2+b2) { //Kreis ist über oder unter Rand
if(abs(x2-x-l2) <=r/2 || abs(x-x2) <=r/2){ vx=-vx;}
}
if (x>=x2 && x<=x2+l2) { //Kreis ist über oder unter Rand
if(abs(y-y2-b2) <=r/2 || abs(y-y2) <=r/2){ vy=-vy;
}
}
}

}
class Rechteck{
int x,y,r;
int vx,vy;

int x2,y2,l2,b2;
int vx2,vy2;
color c;

Rechteck(int x0, int y0, int r0,int x20, int y20, int l20,int b20, int vx0, int vy0, int vx20, int vy20, int c0){
x=x0; y=y0; r=r0;x2=x20; y2=y20; l2=l20; b2=b20; vx=vx0; vy=vy0;vx2= vx20; vy2=vy20; c=c0;
}
void zeichnen(){
fill(c);
rect(x2,y2,l2,b2);
}

void kolrand(){
if(x2<=0){
x2=x2+vx2;
}
if(x2+l2>=width){
x2=x2-vx2;
}
}

void keyPressed(){
if (keyCode == RIGHT) {x2=x2+vx2;}

if( keyCode == LEFT) {x2=x2-vx2;}
}
}
class Bricks{
int x3,y3;
int l3,b3;
color c = color(random(0,255),random(0,255),random(0,255));
int a=0;
int d=0;
int lange;



Bricks(int x30,int y30,int l30, int b30, color c0 ){
x3=x30; y3=y30; l3=l30; b3=b30; c = c0;
}
void zeichnen(){
while(d<=12){
fill(random(0,255),random(0,255),random(0,255));
rect(l3+l3*a,2*b3+b3*d,l3,b3);
a++;
if(a>=11){
a=0;
d++;
}

}
}
}
 

temi

Top Contributor
Wie hast du es denn beim Ball gemacht? Sollte doch genau so beim Rechteck funktionieren. Ansonsten mal halt an der alten Position ein Rechteck in der Farbe des Hintergrunds und danach das neue Rechteck.
 

Neue Themen


Oben