Alien Bounces then Vanishes
in
Programming Questions
•
1 year ago
Part of my coursework is to create a moving alien. i have completed this 99.9% for some reason when it bounces at a certain point the alien vanishes from the screen. please help me this is the last thing i need to do.
Here is my two pieces of code (save the first one as Alien and the second as DemoAlien:
class Alien {
int bodyXLoc;
int bodyYLoc;
int bodyWidth;
int bodyHeight;
int eyeWidth;
int eyeHeight;
int eyeXLoc;
int eyeYLoc;
int eyeballWidth;
int eyeballHeight;
int eyeballXloc;
int eyeballYloc;
int earleftWidth;
int earleftHeight;
int earleftXloc;
int earleftYloc;
int earrightWidth;
int earrightHeight;
int earrightXloc;
int earrightYloc;
int mouthXloc;
int mouthYloc;
int mouthHeight;
int mouthWidth;
int noseXloc;
int noseYloc;
int noseHeight;
int noseWidth;
int eyebrowXloc;
int eyebrowYloc;
int eyebrowHeight;
int eyebrowWidth;
int speedXLoc;
int speedYLoc;
Alien(int tempX, int tempY, int tempWidth, int tempHeight, int tempXSpeed, int tempYSpeed) {
bodyXLoc = tempX;
bodyYLoc = tempY;
bodyWidth = tempWidth;
bodyHeight = tempHeight;
speedXLoc = tempXSpeed;
speedYLoc = tempYSpeed;
}
void drawleftear() {
fill(116,250,8);
earleftXloc = bodyXLoc - 50;
earleftYloc = bodyYLoc - 50;
earleftWidth = bodyWidth / 5;
earleftHeight = bodyHeight - 50;
ellipse(earleftXloc,earleftYloc,earleftWidth,earleftHeight);
}
void drawrightear() {
earrightWidth = bodyWidth / 4;
earrightHeight = bodyHeight - 50;
earrightXloc = bodyXLoc + 50;
earrightYloc = bodyYLoc - 50;
ellipse(earrightXloc,earrightYloc,earrightWidth,earrightHeight);
}
void drawbody() {
fill(255);
ellipse(bodyXLoc,bodyYLoc,bodyWidth,bodyHeight);
}
void draweye() {
int offSet = bodyWidth / 10;
eyeXLoc = bodyXLoc - offSet;
eyeYLoc = bodyYLoc - (bodyHeight / 4);
eyeWidth = bodyHeight / 5;
eyeHeight = bodyWidth / 5;
ellipse(eyeXLoc,eyeYLoc,eyeWidth,eyeHeight);
}
void draweyebrow() {
fill(0);
eyebrowXloc = bodyXLoc - 70;
eyebrowYloc = bodyYLoc - 85;
eyebrowWidth = bodyWidth - 150;
eyebrowHeight = bodyHeight / 25;
rect(eyebrowXloc,eyebrowYloc,eyebrowWidth,eyebrowHeight);
}
void draweyeball() {
fill(0);
int eyeballoffset = bodyWidth / 10;
eyeballXloc = bodyXLoc - eyeballoffset;
eyeballXloc = bodyXLoc - 25;
eyeballYloc = bodyYLoc - 50;
eyeballWidth = bodyHeight / 10;
eyeballHeight = bodyWidth / 10;
ellipse(eyeballXloc,eyeballYloc,eyeballWidth,eyeballHeight);
}
void drawmouth() {
fill(0);
mouthXloc = bodyXLoc;
mouthYloc = bodyYLoc + 75;
mouthWidth = bodyWidth - 100;
mouthHeight = bodyHeight - 150;
ellipse(mouthXloc,mouthYloc,mouthWidth,mouthHeight);
}
void drawnose() {
fill(255,3,3);
noseXloc = bodyXLoc - 50;
noseYloc = bodyYLoc + 50;
noseWidth = bodyWidth - 100;
noseHeight = bodyHeight - 100;
ellipse(noseXloc,noseYloc,noseWidth,noseHeight);
}
int bodyXLoc;
int bodyYLoc;
int bodyWidth;
int bodyHeight;
int eyeWidth;
int eyeHeight;
int eyeXLoc;
int eyeYLoc;
int eyeballWidth;
int eyeballHeight;
int eyeballXloc;
int eyeballYloc;
int earleftWidth;
int earleftHeight;
int earleftXloc;
int earleftYloc;
int earrightWidth;
int earrightHeight;
int earrightXloc;
int earrightYloc;
int mouthXloc;
int mouthYloc;
int mouthHeight;
int mouthWidth;
int noseXloc;
int noseYloc;
int noseHeight;
int noseWidth;
int eyebrowXloc;
int eyebrowYloc;
int eyebrowHeight;
int eyebrowWidth;
int speedXLoc;
int speedYLoc;
Alien(int tempX, int tempY, int tempWidth, int tempHeight, int tempXSpeed, int tempYSpeed) {
bodyXLoc = tempX;
bodyYLoc = tempY;
bodyWidth = tempWidth;
bodyHeight = tempHeight;
speedXLoc = tempXSpeed;
speedYLoc = tempYSpeed;
}
void drawleftear() {
fill(116,250,8);
earleftXloc = bodyXLoc - 50;
earleftYloc = bodyYLoc - 50;
earleftWidth = bodyWidth / 5;
earleftHeight = bodyHeight - 50;
ellipse(earleftXloc,earleftYloc,earleftWidth,earleftHeight);
}
void drawrightear() {
earrightWidth = bodyWidth / 4;
earrightHeight = bodyHeight - 50;
earrightXloc = bodyXLoc + 50;
earrightYloc = bodyYLoc - 50;
ellipse(earrightXloc,earrightYloc,earrightWidth,earrightHeight);
}
void drawbody() {
fill(255);
ellipse(bodyXLoc,bodyYLoc,bodyWidth,bodyHeight);
}
void draweye() {
int offSet = bodyWidth / 10;
eyeXLoc = bodyXLoc - offSet;
eyeYLoc = bodyYLoc - (bodyHeight / 4);
eyeWidth = bodyHeight / 5;
eyeHeight = bodyWidth / 5;
ellipse(eyeXLoc,eyeYLoc,eyeWidth,eyeHeight);
}
void draweyebrow() {
fill(0);
eyebrowXloc = bodyXLoc - 70;
eyebrowYloc = bodyYLoc - 85;
eyebrowWidth = bodyWidth - 150;
eyebrowHeight = bodyHeight / 25;
rect(eyebrowXloc,eyebrowYloc,eyebrowWidth,eyebrowHeight);
}
void draweyeball() {
fill(0);
int eyeballoffset = bodyWidth / 10;
eyeballXloc = bodyXLoc - eyeballoffset;
eyeballXloc = bodyXLoc - 25;
eyeballYloc = bodyYLoc - 50;
eyeballWidth = bodyHeight / 10;
eyeballHeight = bodyWidth / 10;
ellipse(eyeballXloc,eyeballYloc,eyeballWidth,eyeballHeight);
}
void drawmouth() {
fill(0);
mouthXloc = bodyXLoc;
mouthYloc = bodyYLoc + 75;
mouthWidth = bodyWidth - 100;
mouthHeight = bodyHeight - 150;
ellipse(mouthXloc,mouthYloc,mouthWidth,mouthHeight);
}
void drawnose() {
fill(255,3,3);
noseXloc = bodyXLoc - 50;
noseYloc = bodyYLoc + 50;
noseWidth = bodyWidth - 100;
noseHeight = bodyHeight - 100;
ellipse(noseXloc,noseYloc,noseWidth,noseHeight);
}
void move() {
bodyXLoc = bodyXLoc + speedXLoc - 1;
bodyYLoc = bodyYLoc + speedYLoc - 1;
}
void checkForBounce() {
if(bodyXLoc > width - (bodyWidth / 2 )) {
speedXLoc = speedXLoc * -1;
}
if(bodyXLoc < 0 + (bodyWidth / 2 )) {
speedXLoc = speedXLoc * -1;
}
if(bodyYLoc > height - (bodyHeight / 2 )) {
speedYLoc = speedYLoc * -1;
}
if(bodyYLoc < 0 + (bodyHeight / 2 )) {
speedYLoc = speedYLoc * -1;
}
}
}//endclass
bodyXLoc = bodyXLoc + speedXLoc - 1;
bodyYLoc = bodyYLoc + speedYLoc - 1;
}
void checkForBounce() {
if(bodyXLoc > width - (bodyWidth / 2 )) {
speedXLoc = speedXLoc * -1;
}
if(bodyXLoc < 0 + (bodyWidth / 2 )) {
speedXLoc = speedXLoc * -1;
}
if(bodyYLoc > height - (bodyHeight / 2 )) {
speedYLoc = speedYLoc * -1;
}
if(bodyYLoc < 0 + (bodyHeight / 2 )) {
speedYLoc = speedYLoc * -1;
}
}
}//endclass
2nd piece of code:
Alien myAlien;
void setup(){
size(700,700);
myAlien = new Alien(300,175,250,250,3,3);
}
size(700,700);
myAlien = new Alien(300,175,250,250,3,3);
}
void draw(){
background(75);
stroke(0);
myAlien.drawleftear();
myAlien.drawrightear();
myAlien.drawbody();
myAlien.draweye();
myAlien.draweyebrow();
myAlien.draweyeball();
myAlien.drawmouth();
myAlien.drawnose();
myAlien.move();
myAlien.checkForBounce();
myAlien.drawrightear();
myAlien.drawbody();
myAlien.draweye();
myAlien.draweyebrow();
myAlien.draweyeball();
myAlien.drawmouth();
myAlien.drawnose();
myAlien.move();
myAlien.checkForBounce();
}
1