We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I'm new to processing. I'm just having another issue with my mini fishing game. I'm trying to get it so that when the fish is clicked on and restarts, it duplicates that fish each time. I've tried adding the shape again under the if statement in mousepressed(). But it's just doing the same thing. I've been trying to research online about it, but I can't find much on duplicating pshapes. So I don't know how to go about it. Thank you.
PShape fish1, body1, tail1, eye1, fish2, body2, tail2, eye2, fish3, body3, tail3, eye3, fish4, body4, tail4, eye4;
int xPos1 = 800;
int xPos2 = 800;
int xPos3 = 800;
int xPos4 = 800;
float per1 = sin(radians(10 * frameCount));
float per2 = sin(radians(20 * frameCount));
float per3 = sin(radians(30 * frameCount));
float per4 = sin(radians(frameCount));
float r = random(0, 600);
float yPos1 = random(0, 400);
float yPos2 = random(0, 400);
float yPos3 = random(0, 400);
float yPos4 = random(0, 400);
boolean qPressed = false;
boolean wPressed = false;
boolean ePressed = false;
boolean rPressed = false;
void setup() {
size(800, 700);
background(80, 208, 232);
smooth();
noStroke();
//Creating the fish as a group of shapes parented together
//fish 1
fish1 = createShape(GROUP);
tail1 = createShape (TRIANGLE, 22, 0, 60, -15 + per2, 60, 15 + per2);
tail1.setFill(color(252, 193, 97));
body1 = createShape (ELLIPSE, 0, 0, 70, 50);
body1.setFill(color(252, 193, 97));
eye1 = createShape (ELLIPSE, -15, -5, 10, 10);
eye1.setFill(color(0));
fish1.addChild(tail1);
fish1.addChild(body1);
fish1.addChild(eye1);
//fish 2
fish2 = createShape(GROUP);
tail2 = createShape (TRIANGLE, 50, 0, 65, -30 + per2, 65, 30 + per2);
tail2.setFill(color(160, 159, 158));
body2 = createShape (ELLIPSE, 0, 0, 100, 35);
body2.setFill(color(160, 159, 158));
eye2 = createShape(ELLIPSE, -30, -5, 10, 10);
eye2.setFill(color(0));
fish2.addChild(tail2);
fish2.addChild(body2);
fish2.addChild(eye2);
//fish 3
fish3 = createShape(GROUP);
tail3 = createShape (TRIANGLE, 20, 0, 50, -20 + per3, 50, 20 + per3);
tail3.setFill(color(227, 156, 214));
body3 = createShape (ELLIPSE, 0, 0, 60, 80);
body3.setFill(color(227, 156, 214));
eye3 = createShape (ELLIPSE, -20, -5, 10, 10);
eye3.setFill(color(0));
fish3.addChild(tail3);
fish3.addChild(body3);
fish3.addChild(eye3);
//fish 4
fish4 = createShape(GROUP);
tail4 = createShape (TRIANGLE, 15, 0, 25, -10 + per3, 25,10 + per3);
tail4.setFill(color(195, 247, 234));
body4 = createShape (ELLIPSE, 0, 0, 40, 20);
body4.setFill(color(195, 247, 234));
eye4 = createShape (ELLIPSE, -10, 0, 5, 5);
eye4.setFill(color(0));
fish4.addChild(tail4);
fish4.addChild(body4);
fish4.addChild(eye4);
}
void draw() {
//draw a looping fish swimming horizontally across the screen in random y coordinate each time it reaches the end.
background(80, 208, 232);
//fish 1 loop
shape(fish1, xPos1, yPos1);
xPos1 = xPos1-3;
if (xPos1<-200) {
xPos1 = 800;
yPos1 = random(0, 400);
}
//fish 2 loop
shape(fish2, xPos2, yPos2);
xPos2 = xPos2-4;
if (xPos2<-200) {
xPos2 = 800;
yPos2 = random(0, 400);
}
//fish 3 loop
shape(fish3, xPos3, yPos3);
xPos3 = xPos3-2;
if (xPos3<-200) {
xPos3 = 800;
yPos2 = random(0, 400);
}
//fish 4 loop
shape(fish4, xPos4, yPos4);
xPos4 = xPos4 - 5;
if (xPos4<-200) {
xPos4 = 800;
yPos4 = random(0, 400);
}
//Boxes that have the bait in them
noFill();
// Q
if(qPressed == true) {
stroke(203, 0, 0);
strokeWeight(3);
} else {
stroke(1);
strokeWeight(1);
}
rect(250, 620, 75, 75);
// W
if(wPressed == true) {
stroke(203, 0, 0);
strokeWeight(3);
} else {
stroke(1);
strokeWeight(1);
}
rect(330, 620, 75, 75);
// E
if(ePressed == true) {
stroke(203, 0, 0);
strokeWeight(3);
} else {
stroke(1);
strokeWeight(1);
}
rect(410, 620, 75, 75);
// R
if(rPressed == true) {
stroke(203, 0, 0);
strokeWeight(3);
} else {
stroke(1);
strokeWeight(1);
}
rect(490, 620, 75, 75);
}
void keyPressed() {
//When 'q' or 'Q' key is pressed, the first box will turn red. When another key is pressed it will go back to black.
if (key == 'q' || key == 'Q') {
qPressed = true;
wPressed = false;
ePressed = false;
rPressed = false;
}
//When 'w' or 'W' key is pressed, the second box will turn red. When another key is pressed it will go back to black.
else if (key == 'w' || key == 'W') {
qPressed = false;
wPressed = true;
ePressed = false;
rPressed = false;
}
//When 'e' or 'E' key is pressed, the third box will turn red. When another key is pressed it will go back to black.
else if (key == 'e' || key == 'E') {
qPressed = false;
wPressed = false;
ePressed = true;
rPressed = false;
}
//When 'r' or 'R' key is pressed, the fourth box will turn red. When another key is pressed it will go back to black.
else if (key == 'r' || key == 'R') {
qPressed = false;
wPressed = false;
ePressed = false;
rPressed = true;
}
}
void mousePressed(){
//When q is pressed (first box selected) and mouse is pressed on fish 1, that fish goes back to the beginning
if (qPressed == true && dist(xPos1, yPos1, mouseX, mouseY) < 40){
shape(fish1, xPos1, yPos1);
xPos1 = 1000;
}
else if (qPressed == false && dist(xPos1, yPos1, mouseX, mouseY) < 40){
shape(fish1, xPos1, yPos1);
}
if (wPressed == true && dist(xPos2, yPos2, mouseX, mouseY) < 30){
shape(fish2, xPos2, yPos2);
xPos2 = 1200;
}
else if (wPressed == false && dist(xPos2, yPos2, mouseX, mouseY) < 30){
shape(fish2, xPos2, yPos2);
}
if (ePressed == true && dist(xPos3, yPos3, mouseX, mouseY) < 40){
shape(fish3, xPos3, yPos3);
xPos3 = 1000;
}
else if (ePressed == false && dist(xPos3, yPos3, mouseX, mouseY) < 40){
shape(fish3, xPos3, yPos3);
}
if (rPressed == true && dist(xPos4, yPos4, mouseX, mouseY) < 20){
shape(fish4, xPos4, yPos4);
xPos4 = 1300;
}
else if (qPressed == false && dist(xPos4, yPos4, mouseX, mouseY) < 20){
shape(fish4, xPos4, yPos4);
}
}
Answers
look for array in the tutorials
I've done some research on arrays and I'm really struggling to wrap my noobie head around it. Here below is my attempt. Why does the custom fish shapes not work in the class? Sorry if I sound silly.
You need to make the fish shape in the constructor of the class
See tutorials | objects
This:
PShape fish1, body1, tail1, eye1, fish2, body2, tail2, eye2, fish3, body3, tail3, eye3, fish4, body4, tail4, eye4;
you have to get rid of
body, tail eye should be part of the class and since you have an array of that class now, you need only one tail and eye etc. inside the class
Alright updated code. I've got it working with some help from a friend. Only small problem I have left is how do I get rid of the stroke on the duplicate fish?
Also thank you very much for the help!
The function MoveFish belongs into the class
You don't have to pass a parameter to it then
In line 27 just say f.MoveFish();
Make some changes in the function MoveFish
As for the line you mentioned
I am not at home so I can't test it - sorry!
Can you find out where the line comes from?? What is causing the line?
Suggestions:
You could swap lines 222 and 223
You could use noStroke in line 213 /216: // wrong
Eg. tail.noStroke(); // wrong, see below
Also you use a lot of shape() in mousePressed which I don't think you need // correct
Chrisir
[EDITED]
I tested a few things
The Lines
this let's draw you all fishs without the black line (the outline)
use setStroke(false); for shapes (and not noStroke()) :
Parameters:
Also, when you pass numbers to the class' constructor, that are the same for every fish, always, just store them into the class instead of passing them.
I mean those:
More variety please
Also, you don't have much variety in your fish
consider
you can then shorten the lines 122 to 162 above when you use DuplicateFish()
The per1, per2... Variables
Let's consider the per1, per2... Variables.
in setup:
frameCount doesn't make sense here, it's probably always 0 in setup
So I wrote this:
only per2 is in use by the way.
So consider this:
your setup is then (note that per is now getPer() and we have a random eye color):
(you can also use this
getPer()
inDuplicateFish()
)I leave it here.
Have fun, come back for more questions.
Best, Chrisir ;-)
if (rPressed == true) {
same as
if (rPressed) {
str
is not in use in the classThat was such a big help thank you! And yer the only reason why there was only one fish type before was because I just wanted to get the first fish right before I added the rest. I've now added the other 3 types of fish to go with the bait. Also the per was only there originally to make the tails move up and down. That's back when I first made the fish and had them in draw. But when I needed to move them to setup they stopped working and I didn't bother to change them cause it wasn't high priority haha. I've fixed that all up now thanks to your help :)
Great!
Do you want me to show my code of it?