We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am studying IT at an university in the Netherlands. We got an assignment in which we need to create a run. In this run there will be two to fourteen triangles competing each other. The goal of this game is that all the triangles will move separately by using the dice. However, they move all at the same time. The second issue is that once all the triangles passed the finish line, there should come up a box with the overal score (from first up to the last place).
Is there anyone who could help me with these two issues?
Please find below the codes.
Thanking you in advance.
int numberOfTriangles;
int margin;
float y = 0;
boolean startGame = false;
void setup() {
// background(0);
size(600, 600);
}
void draw() {
background(0);
if (startGame) {
drawButton(" STOP", width-width/5, height/40, width/6, height/17);
drawLines();
printTexts();
moveTriangles();
} else {
drawButton(" START", width-width/5, height/40, width/6, height/17);
printTexts();
drawLines();
drawTriangles();
drawSliderNumbers(width/4, height/40, width/12, height/60, 15);
drawSliderMargin(width/20, height/40, width/12, height/60, width/3);
}
}
void moveTriangles() {
int p =width/120;
float space = margin * (numberOfTriangles+1);
float sidesNumbers = width- space;
float side = sidesNumbers /numberOfTriangles;
float x1=0;
float x2=0;
float x3=0;
float y1 = height-height/20 -p;
float y2 =height-height/20 -p;
float y3 = height-height/20-p-side;
float players[][] = {{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, };
int i = 0;
while (i< numberOfTriangles) {
players[i][0] =(side*i)+((i+1)*margin);
players[i][2] = players[i][0] + side;
players[i][4] = players[i][2] - (side/2);
i++;
}
if (side<25 ||side>70) {
fill(255);
textSize(width/30);
textAlign(CENTER, CENTER);
text("game can't start with this margin", width/2, height/2);
} else {
i=0;
while (i< numberOfTriangles) {
fill(random(255), random(255), random(255));
triangle(players[i][0], y1-y, players[i][2], y2-y, players[i][4], y3-y);
fill(random(255), random(255), random(255));
textAlign(CENTER, BOTTOM);
text(i+1, players[i][4], y1-y);
i++;
}
y++;
y=constrain(y, 0, (height/1.33));
println(mouseY);
}
}
void drawLines() {
stroke(255, 0, 0);
line(0, height/5, width, height/5); //finish
line(0, height - height/20, width, height - height/20); //start
}
void printTexts() {
theTexts("numberOfTriangles: " + numberOfTriangles, width/4, height/25);
theTexts("margin : " + margin, width/20, height/25);
theTexts("Finish Line", 0, height/5);
theTexts("Start Line", 0, height - height/20);
}
String theTexts(String text, int x, int y) {
fill(255);
textSize(width/40);
textAlign(LEFT, TOP);
text(text, x, y);
return text;
}
void drawButton(String notification,int x , int y, int Width , int Height) {
fill (#00FF00);
rect(x, y, Width, Height);
textSize(width/20);
fill (0);
textAlign(CENTER, CENTER);
text ( notification, width-width/8, height/20) ;
}
void mouseClicked() {
int x = width-width/5;
int y=height/40;
int Width = width/6;
int Height=height/17;
if ( mouseX > x && mouseX < x + Width
&& mouseY > y && mouseY< y + Height ) {
startGame = ! startGame;
}
}
void drawTriangles() {
int p =width/120;
float space = margin * (numberOfTriangles+1); //de lege afstand tussen de driehoeken
float sidesNumbers = width- space;
float side = sidesNumbers /numberOfTriangles;
float x1=0;
float x2=0;
float x3=0;
float y1 = height-height/20 -p;
float y2 =height-height/20 -p;
float y3 = height-height/20-p-side;
float players[][] = {{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3},
{x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, {x1, y1, x2, y2, x3, y3}, };
int i = 0;
while (i< numberOfTriangles) {
players[i][0] =(side*i)+((i+1)*margin);
players[i][2] = players[i][0] + side;
players[i][4] = players[i][2] - (side/2);
i++;
}
if (side<25 ||side>60) {
fill(255);
textSize(width/30);
textAlign(CENTER, CENTER);
text("game can't start with this margin", width/2, height/2);
// text("side = " +side, width/2, height/3);
} else {
i=0;
while (i< numberOfTriangles) {
fill(random(255), random(255), random(255));
triangle(players[i][0], y1, players[i][2], y2, players[i][4], y3);
fill(random(255), random(255), random(255));
textAlign(CENTER, BOTTOM);
text(i+1, players[i][4], y1);
i++;
}
}
}
void drawSliderNumbers(float x, float y, float Width, float Height, int nPosition) {
float cubeWidth= Width/nPosition;
int position ;
//println(floor((mouseX - x) / cubeWidth));
if (mouseX>=x && mouseX< x + Width && mouseY>= y && mouseY<=y +Height) {
position = floor(((mouseX -x) / cubeWidth));
numberOfTriangles = position ;
} else {
position = numberOfTriangles ;
numberOfTriangles =constrain(numberOfTriangles, 2, 14);
}
noStroke();
fill(255);
rect(x, y, Width, Height);
fill(#2257F0);
rect(x + position * cubeWidth, y, cubeWidth, Height);
}
void drawSliderMargin(float x, float y, float Width, float Height, int nPosition) {
float cubeWidth= Width/nPosition;
int position ;
//println(floor((mouseX - x) / blokjeBreedte)); // we maken Nposities om te bepalen hoeveel rect we willen,ipv delen door een getaal hier
if (mouseX>=x && mouseX< x + Width && mouseY>= y && mouseY<=y +Height ) {
position = floor((mouseX - x) / cubeWidth);
margin = position +2 ;
} else {
position = margin ;
margin=constrain(margin, 2, width/3);
}
noStroke();
fill(255);
rect(x, y, Width, Height);
fill(255, 0, 0);
rect(x + position * cubeWidth, y, cubeWidth, Height);
}
Answers
Edit post, highlight code, press Ctrl-o to format.
Then, maybe, we'll be able to see what you're trying to do...
thank you, I have edit it :)
but it's only a bit. where is the setup(), where is the draw()?
what is marge? what is aantalrobots? what is spelers[]?
runnable examples will get you better answers.
22 different errors when you cut and paste into PDE.
oof.
Ok, all the code is there now, thanks. I've taken out the tabs - they just make it harder to cut and paste.
i'm not sure i understand what you're trying to do.
however, i can tell you things that look wrong.
line 50
float players[][]
and line 153. why do you have two copies of the same thing? and they both have method scope so they don't exist outside of the method they are defined in. this means you can't remember where each triangle is because they keep getting recreated.this gives the triangle a different colour EVERY FRAME. which is why they flickrer.
moveTriangles doesn't appear to move any triangles. i'd expect it to add values to the position variables... (on a per triangle basis, not the same increment to all)
(on a per triangle basis, not the same increment to all) ?? This is what i am trying to do. If I do it in this way (see codes below) then all the triangles will move together. However, I want that all the triangles will move separately. It is like a simulation of a run (triangles are the players). So each player needs to use the dice and the triangle which belongs to that player will make the steps.
the Triangles will move all at the same time because I use one triangle command in for , I want that each triangle will move separately. for example a triangle move 2 pixels and the other one 5 pixels.
Secondly, I want that all the triangles will get a different colour each game. So the colours need to be given randomly to the triangles. Besides, I want that the triangles stop flickering. How can I fix that?
Thank you!
Look at classes, in the reference, Common Questions here and the tutorials on the processing site.
Basically everything that is different for each triangle needs to be a variable in your class, so position and colour in this case. Have an array (or ArrayList) of this class, make it global.
To move them just iterate through then adding a dice throw to each.
You CAN do all the above using separate arrays, given that you probably only need 2 (current score and colour), but a class is the proper way to do it. The important thing is that these arrays are global, defined before setup, initialised in setup and manipulated in draw.
(the other thing is that you don't need to store all the corners of each triangle - they never change shape so you only need to remember the centre. And then the x position is defined by how many triangles you have so you don't need to remember that. And the y position depends on the current score so you might as well just store the score instead. Score and colour, the only two things you need for each triangle.)
something like this
Thank you for your help it works now but I have another problem.
I want to see the order of which robot has arrived first to the finish line. I have this code and it works, it shows which robots has arrived first and then the order of the robots is not good anymore. This is my new code.
your code is hard to read.
You realize that one Triangle should be in a class? Player and colours are parallel arrays.
Please read this:
https://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
width and height
You don't need this:
breadth=width; highness=height;
just stick with width and height
Highscore
you could fill an ArrayList with the winners using add
Thanks for your answer but I am not allowed to use class and ArrayList From my teacher. Do you have another way for this?
Thanks
An array for the winners?
Yes it's possible but how to add the values to this new array?
You could have 2 parallel arrays: names and scores
when one triangle crossed target line fill in both arrays using indexWinner
Then indexWinner++;
display both arrays from zero up to indexWinner with a for-loop
I have tried but it does not works with me. Can you please write the code for me ?
Thanks
are you kidding? Why? Do you know the metaphor of giving a man a fish and teach them how to fish?
Show your entire code that is your attempt at this highscore so that I can see you followed each of my steps please
Then I'll see what I can do
I made aan array of order,
go on.
Where do you use
order
?This isn't your entire code.