1
I’m pretty new with java and I’m working on the project that I have to draw a triangle (which I have to call Alice) that is located in the upper left corner of the screen and it has to move in a straight line to the upper right corner, then move to the lower right, then to the lower left and back to the upper left starting point and it has to walk around the corners of the screen until I close and window.
I’m having trouble with my if structures. I don’t know why it leaves the starting point (upper right), goes up to the upper left and then, when it goes down to the lower left, it doesn’t stop at the edge of the screen, and from there I don’t know if it doesn’t continue its trajectory.
If anyone can help me I thank you from my heart.
My code is like this:
import processing.core.PApplet;
public class Alice{
int colour;
float xSpeed;
PApplet parent;
float x1Pos;
float y1Pos;
float x2Pos;
float y2Pos;
float x3Pos;
float y3Pos;
public Alice(PApplet p, int colour, float x1Pos, float y1Pos, float x2Pos, float y2Pos, float x3Pos, float y3Pos, float xSpeed){
parent = p;
this.colour = colour;
this.x1Pos = x1Pos;
this.y1Pos = y1Pos;
this.x2Pos = x2Pos;
this.y2Pos = y2Pos;
this.x3Pos = x3Pos;
this.y3Pos = y3Pos;
this.xSpeed = xSpeed;
}
public void display(){
parent.rectMode(parent.CENTER);
parent.fill(colour);
parent.triangle(x1Pos, y1Pos, x2Pos, y2Pos, x3Pos, y3Pos);
}
private void goLeft(){
this.x1Pos = this.x1Pos - this.xSpeed;
this.x2Pos = this.x2Pos - this.xSpeed;
this.x3Pos = this.x3Pos - this.xSpeed;
}
private void goRight(){
this.x1Pos = this.x1Pos + xSpeed;
this.x2Pos = this.x2Pos + xSpeed;
this.x3Pos = this.x3Pos + xSpeed;
}
private void goUp(){
this.y1Pos = this.y1Pos - xSpeed;
this.y2Pos = this.y2Pos - xSpeed;
this.y3Pos = this.y3Pos - xSpeed;
}
private void goDown(){
this.y1Pos = this.y1Pos + xSpeed;
this.y2Pos = this.y2Pos + xSpeed;
this.y3Pos = this.y3Pos + xSpeed;
}
public void driveCorner(){
if(x1Pos <= parent.width - 30){
goRight();
}
if(x1Pos >= parent.width - 30){
goDown();
}
if(y1Pos <= parent.height - 20){
goLeft();
}
if(x1Pos <= 0){
goUp();
}
}
THANK YOU VERY MUCH. IS WORKING PERFECTLY
– Wagner Gualberto