problem with alien bouncing
in
Programming Questions
•
1 year ago
whenever it hits the x axis it gets stuck at the top instead of bouncing all around the screen, since i added a class and I cant seem to work it out.
any help would be appreciated thanks!
heres the code (saved as MyAlien)
class Alien {
//body
int bodyXLoc;
int bodyYLoc;
int bodyWidth;
int bodyHeight;
//head
int headWidth;
int headHeight;
int headXLoc;
int headYLoc;
//left eye
int LefteyeWidth;
int LefteyeHeight;
int LefteyeXloc;
int LefteyeYloc;
//right eye
int RighteyeWidth;
int RighteyeHeight;
int RighteyeXloc;
int RighteyeYloc;
//left inner eye
int LinnereyeWidth;
int LinnereyeHeight;
int LinnereyeXloc;
int LinnereyeYloc;
//right inner eye
int RinnereyeWidth;
int RinnereyeHeight;
int RinnereyeXloc;
int RinnereyeYloc;
//nose
int noseWidth;
int noseHeight;
int noseXloc;
int noseYloc;
//hair
int hair1Width;
int hair1Height;
int hair1Xloc;
int hair1Yloc;
int hair2Width;
int hair2Height;
int hair2Xloc;
int hair2Yloc;
int hair3Width;
int hair3Height;
int hair3Xloc;
int hair3Yloc;
int hair4Width;
int hair4Height;
int hair4Xloc;
int hair4Yloc;
int hair5Width;
int hair5Height;
int hair5Xloc;
int hair5Yloc;
int hair6Width;
int hair6Height;
int hair6Xloc;
int hair6Yloc;
//legs
int leg1Width;
int leg1Height;
int leg1Xloc;
int leg1Yloc;
int leg2Width;
int leg2Height;
int leg2Xloc;
int leg2Yloc;
int leg3Width;
int leg3Height;
int leg3Xloc;
int leg3Yloc;
int leg4Width;
int leg4Height;
int leg4Xloc;
int leg4Yloc;
int leg5Width;
int leg5Height;
int leg5Xloc;
int leg5Yloc;
int leg6Width;
int leg6Height;
int leg6Xloc;
int leg6Yloc;
int speed;
int speed2;
Alien(int tempX, int tempY, int tempWidth, int tempHeight, int tempXSpeed, int tempYSpeed) {
bodyXLoc = tempX;
bodyYLoc = tempY;
bodyWidth = tempWidth;
bodyHeight = tempHeight;
speed = tempXSpeed;
speed2 = tempYSpeed;
}
void setup() {
size(1000,1000);//canvas size
speed = 2;
speed2 = -2;// makes alien move
}
Alien() {
bodyXLoc = 200;
bodyYLoc = 240;
bodyWidth = 100;
bodyHeight = 200;
}
void drawBody() {
noFill();
//right leg
leg1Width = bodyHeight ;
leg1Height = bodyHeight;
leg1Xloc = bodyXLoc - (RighteyeWidth*1 ) ;
leg1Yloc = bodyYLoc + (RinnereyeHeight*7) ;
arc(leg1Xloc, leg1Yloc,leg1Width, leg1Height, PI, TWO_PI-PI/2);
//left hand
leg2Width = bodyHeight ;
leg2Height = bodyHeight;
leg2Xloc = bodyXLoc - (RighteyeWidth*1) ;
leg2Yloc = bodyYLoc + (RinnereyeHeight*6) ;
arc(leg2Xloc, leg2Yloc,leg2Width, leg2Height, PI, TWO_PI-PI/2);
leg3Width = bodyHeight ;
leg3Height = bodyHeight;
leg3Xloc = bodyXLoc - (RighteyeWidth *1) ;
leg3Yloc = bodyYLoc + (RinnereyeHeight*5) ;
arc(leg3Xloc, leg3Yloc,leg3Width, leg3Height, PI, TWO_PI-PI/2);
leg4Width = bodyHeight ;
leg4Height = bodyHeight;
leg4Xloc = bodyXLoc - (RighteyeWidth /2) ;
leg4Yloc = bodyYLoc + (RinnereyeHeight*4) ;
arc(leg4Xloc, leg4Yloc,leg4Width, leg4Height, TWO_PI-PI/2, TWO_PI);
leg5Width = bodyHeight ;
leg5Height = bodyHeight;
leg5Xloc = bodyXLoc - (RighteyeWidth /4) ;
leg5Yloc = bodyYLoc + (RinnereyeHeight*7) ;
arc(leg5Xloc, leg5Yloc,leg5Width, leg5Height, TWO_PI-PI/2, TWO_PI);
leg6Width = bodyHeight ;
leg6Height = bodyHeight;
leg6Xloc = bodyXLoc - (RighteyeWidth /6 ) ;
leg6Yloc = bodyYLoc + (RinnereyeHeight*6) ;
arc(leg6Xloc, leg6Yloc,leg6Width, leg6Height, TWO_PI-PI/2, TWO_PI);
fill(0,50,200); //blue fill
ellipse(bodyXLoc,bodyYLoc,bodyWidth,bodyHeight);//alien body
//alien head
headWidth = bodyHeight /2;
headHeight = headWidth;
headXLoc = bodyXLoc;
headYLoc = bodyYLoc - (bodyHeight / 4) - (headHeight / 3);
ellipse(headXLoc,headYLoc,headWidth,bodyHeight);
//left eye
fill(255,255,255);
LefteyeWidth = headWidth /4;
LefteyeHeight = bodyWidth /4;
LefteyeXloc = headXLoc +(headWidth / 4);
LefteyeYloc = headYLoc - (headHeight / 2);
ellipse (LefteyeXloc,LefteyeYloc,LefteyeWidth,LefteyeHeight);
//right eye
RighteyeWidth = headWidth /4;
RighteyeHeight = bodyWidth /4;
RighteyeXloc = headXLoc - (headWidth / 4);
RighteyeYloc = headYLoc - (headHeight / 2);
ellipse(RighteyeXloc,RighteyeYloc,RighteyeWidth,RighteyeHeight);
//left inner eye
fill(255,0,0);
LinnereyeWidth = headWidth / 7;
LinnereyeHeight = bodyWidth /9 ;
LinnereyeXloc = LefteyeXloc ;
LinnereyeYloc = LefteyeYloc + (LefteyeHeight / 4) ;
ellipse(LinnereyeXloc, LinnereyeYloc, LinnereyeWidth, LinnereyeHeight);
//right inner eye
RinnereyeWidth = headWidth / 7;
RinnereyeHeight = bodyWidth / 9;
RinnereyeXloc = RighteyeXloc;
RinnereyeYloc = RighteyeYloc + (RinnereyeHeight/4);
ellipse( RinnereyeXloc,RinnereyeYloc,RinnereyeWidth,RinnereyeHeight );
//red fill
fill(203,0,0);
//nose
noseWidth = headWidth / 4;
noseHeight = headHeight / 6 ;
noseXloc = headXLoc;
noseYloc = headYLoc - (noseHeight / 4);
ellipse(noseXloc,noseYloc,noseWidth,noseHeight);
//hair
hair1Width = headWidth / 6;
hair1Height = bodyWidth / 5 ;
hair1Xloc = RinnereyeXloc + (headWidth / 3);
hair1Yloc = RinnereyeYloc - (headHeight / 2);
ellipse(hair1Xloc,hair1Yloc,hair1Width,hair1Height) ;
//hair
hair2Width = headWidth / 6;
hair2Height = bodyWidth / 5;
hair2Xloc = RinnereyeXloc + (headWidth / 4);
hair2Yloc = RinnereyeYloc - (headHeight / 2);
ellipse(hair2Xloc,hair2Yloc,hair2Width,hair2Height) ;
//hair
hair3Width = headWidth /6 ;
hair3Height = bodyWidth / 5 ;
hair3Xloc = RinnereyeXloc + (headWidth / 6);
hair3Yloc = RinnereyeYloc - (headHeight / 2) ;
ellipse (hair3Xloc,hair3Yloc,hair3Width,hair3Height) ;
}
void move () {
bodyXLoc = bodyXLoc + speed;
bodyYLoc = bodyYLoc + speed2;
}
void Bounce() {
if(bodyXLoc > (width - (bodyWidth / 2))) {
speed = speed * -1;
}
if(headXLoc == (0 +(headWidth / 2))) {
speed = speed * -1;
}
if (bodyYLoc > (height - (bodyHeight / 1) - (headHeight / 2))) {
speed2 = speed2 * -1;
}
if (hair3Yloc < (0 + (hair3Height / 2 + 5 ))) {
speed2 = speed2* -1; //top bounce
}
//for background
}
} //end class
Here is the class file also
Alien myAlien;
void setup() {
size(1000, 1000);
myAlien = new Alien(200,240,100,200,2,2);
}
void draw() {
stroke(0);
fill(0,10);
rect (0,3, width, height); //(something random) motion blur effect
//ellipse(random(width), random(height),3,3); //(hide this for motion blur)
myAlien.drawBody();
myAlien.move();
myAlien.Bounce();
}
any help would be appreciated thanks!
heres the code (saved as MyAlien)
class Alien {
//body
int bodyXLoc;
int bodyYLoc;
int bodyWidth;
int bodyHeight;
//head
int headWidth;
int headHeight;
int headXLoc;
int headYLoc;
//left eye
int LefteyeWidth;
int LefteyeHeight;
int LefteyeXloc;
int LefteyeYloc;
//right eye
int RighteyeWidth;
int RighteyeHeight;
int RighteyeXloc;
int RighteyeYloc;
//left inner eye
int LinnereyeWidth;
int LinnereyeHeight;
int LinnereyeXloc;
int LinnereyeYloc;
//right inner eye
int RinnereyeWidth;
int RinnereyeHeight;
int RinnereyeXloc;
int RinnereyeYloc;
//nose
int noseWidth;
int noseHeight;
int noseXloc;
int noseYloc;
//hair
int hair1Width;
int hair1Height;
int hair1Xloc;
int hair1Yloc;
int hair2Width;
int hair2Height;
int hair2Xloc;
int hair2Yloc;
int hair3Width;
int hair3Height;
int hair3Xloc;
int hair3Yloc;
int hair4Width;
int hair4Height;
int hair4Xloc;
int hair4Yloc;
int hair5Width;
int hair5Height;
int hair5Xloc;
int hair5Yloc;
int hair6Width;
int hair6Height;
int hair6Xloc;
int hair6Yloc;
//legs
int leg1Width;
int leg1Height;
int leg1Xloc;
int leg1Yloc;
int leg2Width;
int leg2Height;
int leg2Xloc;
int leg2Yloc;
int leg3Width;
int leg3Height;
int leg3Xloc;
int leg3Yloc;
int leg4Width;
int leg4Height;
int leg4Xloc;
int leg4Yloc;
int leg5Width;
int leg5Height;
int leg5Xloc;
int leg5Yloc;
int leg6Width;
int leg6Height;
int leg6Xloc;
int leg6Yloc;
int speed;
int speed2;
Alien(int tempX, int tempY, int tempWidth, int tempHeight, int tempXSpeed, int tempYSpeed) {
bodyXLoc = tempX;
bodyYLoc = tempY;
bodyWidth = tempWidth;
bodyHeight = tempHeight;
speed = tempXSpeed;
speed2 = tempYSpeed;
}
void setup() {
size(1000,1000);//canvas size
speed = 2;
speed2 = -2;// makes alien move
}
Alien() {
bodyXLoc = 200;
bodyYLoc = 240;
bodyWidth = 100;
bodyHeight = 200;
}
void drawBody() {
noFill();
//right leg
leg1Width = bodyHeight ;
leg1Height = bodyHeight;
leg1Xloc = bodyXLoc - (RighteyeWidth*1 ) ;
leg1Yloc = bodyYLoc + (RinnereyeHeight*7) ;
arc(leg1Xloc, leg1Yloc,leg1Width, leg1Height, PI, TWO_PI-PI/2);
//left hand
leg2Width = bodyHeight ;
leg2Height = bodyHeight;
leg2Xloc = bodyXLoc - (RighteyeWidth*1) ;
leg2Yloc = bodyYLoc + (RinnereyeHeight*6) ;
arc(leg2Xloc, leg2Yloc,leg2Width, leg2Height, PI, TWO_PI-PI/2);
leg3Width = bodyHeight ;
leg3Height = bodyHeight;
leg3Xloc = bodyXLoc - (RighteyeWidth *1) ;
leg3Yloc = bodyYLoc + (RinnereyeHeight*5) ;
arc(leg3Xloc, leg3Yloc,leg3Width, leg3Height, PI, TWO_PI-PI/2);
leg4Width = bodyHeight ;
leg4Height = bodyHeight;
leg4Xloc = bodyXLoc - (RighteyeWidth /2) ;
leg4Yloc = bodyYLoc + (RinnereyeHeight*4) ;
arc(leg4Xloc, leg4Yloc,leg4Width, leg4Height, TWO_PI-PI/2, TWO_PI);
leg5Width = bodyHeight ;
leg5Height = bodyHeight;
leg5Xloc = bodyXLoc - (RighteyeWidth /4) ;
leg5Yloc = bodyYLoc + (RinnereyeHeight*7) ;
arc(leg5Xloc, leg5Yloc,leg5Width, leg5Height, TWO_PI-PI/2, TWO_PI);
leg6Width = bodyHeight ;
leg6Height = bodyHeight;
leg6Xloc = bodyXLoc - (RighteyeWidth /6 ) ;
leg6Yloc = bodyYLoc + (RinnereyeHeight*6) ;
arc(leg6Xloc, leg6Yloc,leg6Width, leg6Height, TWO_PI-PI/2, TWO_PI);
fill(0,50,200); //blue fill
ellipse(bodyXLoc,bodyYLoc,bodyWidth,bodyHeight);//alien body
//alien head
headWidth = bodyHeight /2;
headHeight = headWidth;
headXLoc = bodyXLoc;
headYLoc = bodyYLoc - (bodyHeight / 4) - (headHeight / 3);
ellipse(headXLoc,headYLoc,headWidth,bodyHeight);
//left eye
fill(255,255,255);
LefteyeWidth = headWidth /4;
LefteyeHeight = bodyWidth /4;
LefteyeXloc = headXLoc +(headWidth / 4);
LefteyeYloc = headYLoc - (headHeight / 2);
ellipse (LefteyeXloc,LefteyeYloc,LefteyeWidth,LefteyeHeight);
//right eye
RighteyeWidth = headWidth /4;
RighteyeHeight = bodyWidth /4;
RighteyeXloc = headXLoc - (headWidth / 4);
RighteyeYloc = headYLoc - (headHeight / 2);
ellipse(RighteyeXloc,RighteyeYloc,RighteyeWidth,RighteyeHeight);
//left inner eye
fill(255,0,0);
LinnereyeWidth = headWidth / 7;
LinnereyeHeight = bodyWidth /9 ;
LinnereyeXloc = LefteyeXloc ;
LinnereyeYloc = LefteyeYloc + (LefteyeHeight / 4) ;
ellipse(LinnereyeXloc, LinnereyeYloc, LinnereyeWidth, LinnereyeHeight);
//right inner eye
RinnereyeWidth = headWidth / 7;
RinnereyeHeight = bodyWidth / 9;
RinnereyeXloc = RighteyeXloc;
RinnereyeYloc = RighteyeYloc + (RinnereyeHeight/4);
ellipse( RinnereyeXloc,RinnereyeYloc,RinnereyeWidth,RinnereyeHeight );
//red fill
fill(203,0,0);
//nose
noseWidth = headWidth / 4;
noseHeight = headHeight / 6 ;
noseXloc = headXLoc;
noseYloc = headYLoc - (noseHeight / 4);
ellipse(noseXloc,noseYloc,noseWidth,noseHeight);
//hair
hair1Width = headWidth / 6;
hair1Height = bodyWidth / 5 ;
hair1Xloc = RinnereyeXloc + (headWidth / 3);
hair1Yloc = RinnereyeYloc - (headHeight / 2);
ellipse(hair1Xloc,hair1Yloc,hair1Width,hair1Height) ;
//hair
hair2Width = headWidth / 6;
hair2Height = bodyWidth / 5;
hair2Xloc = RinnereyeXloc + (headWidth / 4);
hair2Yloc = RinnereyeYloc - (headHeight / 2);
ellipse(hair2Xloc,hair2Yloc,hair2Width,hair2Height) ;
//hair
hair3Width = headWidth /6 ;
hair3Height = bodyWidth / 5 ;
hair3Xloc = RinnereyeXloc + (headWidth / 6);
hair3Yloc = RinnereyeYloc - (headHeight / 2) ;
ellipse (hair3Xloc,hair3Yloc,hair3Width,hair3Height) ;
}
void move () {
bodyXLoc = bodyXLoc + speed;
bodyYLoc = bodyYLoc + speed2;
}
void Bounce() {
if(bodyXLoc > (width - (bodyWidth / 2))) {
speed = speed * -1;
}
if(headXLoc == (0 +(headWidth / 2))) {
speed = speed * -1;
}
if (bodyYLoc > (height - (bodyHeight / 1) - (headHeight / 2))) {
speed2 = speed2 * -1;
}
if (hair3Yloc < (0 + (hair3Height / 2 + 5 ))) {
speed2 = speed2* -1; //top bounce
}
//for background
}
} //end class
Here is the class file also
Alien myAlien;
void setup() {
size(1000, 1000);
myAlien = new Alien(200,240,100,200,2,2);
}
void draw() {
stroke(0);
fill(0,10);
rect (0,3, width, height); //(something random) motion blur effect
//ellipse(random(width), random(height),3,3); //(hide this for motion blur)
myAlien.drawBody();
myAlien.move();
myAlien.Bounce();
}
1