We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello guys, I'm new here and also new on programming. I'm working on in a college project. I need the the first triangle patrol the environment for the duration of the program. I got it how to do it but now I don't know how to make it keep going.
here is my code. It's not perfect yet and with some unnecessary variable, I'ts because I'm still working on it. And of course if there are some improvements to do, feel free to tell me. Sorry guys.
int tx1, tx1a, ty1, ty1a, tx2, ty2, tx3, ty3; //"x" and "y" spots
int speed1, speed2, speed3, speed4;
int sz1x, sz1y, sz2;// size
int hgt1a, hgt1b, wdt1a, wdt1b, hgt2a, hgt2b, wdt2, hgt3, wdt3; // height and width
boolean cont = true;
void setup() {
size(500, 500);
background(255);
tx1 = 0;
tx1a = 0;
ty1 = 0;
ty1a = 500;
tx2 = 0;
ty2 = 0;
tx3 = 0;
ty3 = 0;
speed1 = 1;
speed2 = 1;
speed3 = 1;
speed4 = 1;
sz1x = 5;
sz1y = 6;
sz2 = 80;
hgt1a = 0;
hgt1b = 500-61;
wdt1a = 0;
wdt1b = 500-(2*39);
hgt2a = 63;// ponto mais alto do y
hgt2b = 231;
wdt2 = 52;// soma das duas diferenças de x, raio do objeto vezes dois
hgt3 = 110;
wdt3 = 36;
}
void draw() {
strokeWeight(1.5);
background(255);
pushMatrix();
tx1 = tx1 + speed1;
fill(0, 0, 255);
translate( tx1, 0);
translate( 0, ty1);
triangle(sz1x, sz1y + 51, sz1x + 34, sz1y, sz1x + (2*34), sz1y + 51);
if (tx1 >= wdt1b) {
tx1 = wdt1b;
}
if (tx1 == wdt1b) {
speed2++;
ty1 =+ speed2;
}
if (ty1 >= hgt1b) {
ty1 = hgt1b;
}
if (ty1 == hgt1b) {
speed1 =- (1);
}
if (tx1 <= wdt1a) {
tx1 = wdt1a;
}
if (tx1 == wdt1a) {
speed4++;
ty1 = hgt1b - speed4;
}
if (ty1 <= hgt1a) {
ty1 = hgt1a;
}
popMatrix();
pushMatrix();
tx2 = tx2 + speed3;
translate( tx2, 0);
fill(255, 0, 0);
triangle(sz2, 105, sz2+26, 63, sz2 +(2*26), 105);
if (tx2 >= hgt2b) {
speed3--;
}
if (tx2 <= hgt2a) {
speed3++;
}
popMatrix();
pushMatrix();
fill(0, 255, 0);
triangle(136, 145, 154, 112, 172, 145);
popMatrix();
line(1, 498, 1, 1);
line(1, 1, 498, 1);
line(498, 1, 498, 498);
line(498, 498, 1, 498);
line(75, 441, 75, 59);
line(75, 59, 423, 59);
line(423, 59, 423, 441);
line(423, 441, 75, 441);
line(134, 393, 134, 107);
line(134, 107, 367, 107);
line(367, 107, 367, 393);
line(367, 393, 134, 393);
}
Thanks a lot everyone.
Answers
Format your code. Edit post, select your code and hit ctrl+o. Ensure there is an empty line above and below your code block.
Kf
Done.. sorry I dind't know
Currently your section that controls the outside triangle lines 49-77. It is quite difficult to understand!
First:
For endless turning, you want to constantly be setting speed factors in two directions. Any time the triangle passes outside the bounding box, its speed changes (it turns). Here is an example of a method from other code that does this -- it only needs to check four things (too far left, right, up, or down) and changes the direction (horizontal and vertical speeds) accordingly. If speed is positive, the shape will move and turn clockwise. If speed is negative it will move and turn counter-clockwise.
Can you see how to make those if statements in your code work like this code?
Second:
In order to make this code easier for you to program, you need to learn how to create classes of objects. If you create a "Turner" class and make three objects from it then your draw code will be very simple and easy to read, understand, and modify.
cheers, Jeremy
Hello Jeremy,
Thanks a lot for your answer, Before your answer I came up with this code, much better than before, and much closer to yours.
And I'll put myself into the object and class. Thanks a lot...
@Crixi -- glad to hear that it was helpful. Good luck -- write back on this thread and post your new code if you have questions about the classes and objects process.
Hey Jeremy, me again, how are you??
Look I have a question about object and class.
I did a class void move with some objects.
This class has to be at the same Sketchbook or can I do it in a second Sketchbook (New Tab)? If a can how do I call this class?
I did in this way:
Thanks a lot
You can use a new tab - no problem
Your code shows a new function but not a class
See website, tutorials and read the tutorial about "objects"
https://www.processing.org/tutorials/objects/
@Crixi --
As @Chrisir says, new tabs are not separate sketchbooks -- you can organize your code in tabs and all of the tabs are part of the same sketchbook -- as if they were one long code document broken up into sections.
Please look at the object and class tutorials that I linked above linked above one more time.
void move(){}
is not a class - it is a function or method. In order to attach a move() method to a class you need to put that code inside inside a class, like this:a non working class example!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
my example shows the usage of a class
As you can see from my example, this requires new thinking
imagine a cookie maker and many cookies
the cookie maker is your class. It holds all we need to know of the triangles. The principle or blue print of a triangle.
The cookies are your triangles (at the moment only one triangle). They are made from the cookie maker in line 7 to 9 above where they get their individual values.
The values are copied into our new triangle object using the constructor.
So a new triangle doesn't require changes in the class but only in the setup (basically copying lines 7 to 9 above with different values)
To develop the class, eg giving the triangles an individual color, you need to make changes in the class. Those would effect all triangles.
But how many triangles you have and their individual properties are all outside the class.
To have many triangles (eg 3 or 4) you can also make an array of the class: An array whose type is TrianglePatrol:
TrianglePatrol[] = new TrianglePatrol[4];
Then you can use for-loops
I wrote non-working
The only part that is not working though is the turning on the corners
Otherwise the sketch runs and displays the triangle correctly
Guys thanks a lot, I'm very grateful for all your helps and explanation. I did my project great, after all those information, self search on internet, learning with my mistakes and trying a lot. I did an object-oriented programming
Thanks a lot one more time... After this triangle I did two more. Bob who has to make a random walk (that I working on it) Charlie who has to be remote controlled by the keyboard.
And advice for the random walking are welcoming
You can read The Nature of Code. It's a very nice book and has advice that you could use.