so I am really new to programming in general. Right now I am using processing/Java to do stuff.
Ive got an assignment to program a small video game and I am stuck for a few hours now, kinda frustrated so I decided to try to get help here.
So right now I have made a class called Gesichter where I defined properties and functions. Now I can draw the Object I created by defining the function in my main program, then running it.
problem is... I want to have this object drawn multiple times but with slightly different heights(yPos). I can do it by hand and just rename and readjust the object, then putting it under the draw function. But I have to do it in a more efficient way for the assignment. My teacher said it can be done by using an array but I am at this moment just confused what to do and I am not sure at all how to do it. preferably I would like to have about 20 "Gesichter" with different heights.
Also how do I get to use the whEllipse variable from my class Gesichter in the main program?
Here is the code so far:
Gesichter rot;
void settings(){
size(700,700);
}
void setup(){
rot = new Gesichter(random(-whEllipse,height),random(-whEllipse,height));
}
void draw(){
background(0);
rot.paint();
rot.move();
}
so this is my main.
this is the "Gesichter" class so far.
public class Gesichter {
//Eigenschaften
float whEllipse= 200;
float yPos = random(-whEllipse,height);
float xPos = random(-whEllipse,height);
float xSpeed = 4;
float P = 4;
Gesichter(float yPos,float xPos) {
this.yPos = yPos;
this.xPos = xPos;
yPos = random(-whEllipse,height);
xPos = random(-whEllipse/2,width);
}
//Methoden/Funktionen
void move() {
xPos = xSpeed;
yPos = (P*sin(radians(xPos)) 1); // 1 macht dass sich das Gesicht langsam runter bewegt.
if (xPos>= width whEllipse/2) {
// P = P 1; Maybe falls ich größere Amplitude will
xPos = -whEllipse/2;
}
if (yPos >= height whEllipse/2) {
yPos = random(-whEllipse, height/2);
xPos = random(-200, whEllipse/2);
}
}
void paint() {
fill(255, 0, 0);
circle(xPos, yPos, whEllipse);
}
}
I hope someone can help me out :// would be so greatful.
CodePudding user response:
This should get you started. You'll have to do something similar in your draw() method.
Gesichter[] rots = new Gesichter[20];
void setup() {
for (int i = 0; i < rots.length; i ) {
rots[i] = new Gesichter(random(-whEllipse, height), random(-whEllipse, height));
}
}
Regarding your Gesichter constructor: those last two lines are actually useless. You're reassigning the constructor parameters to those new values, essentially throwing them away. It should look like this.
Gesichter(float yPos, float xPos) {
this.yPos = yPos;
this.xPos = xPos;
}
Now, I don't know much about German, but... isn't that class a single face? Would Gesicht be more appropriate?
Regarding your whEllipse problem: you need to either pass it in from the outside:
public Gesichter(float yPos, float xPos, float whEllipse) {
...
}
void setup() {
for (int i = 0; i < rots.length; i ) {
rots[i] = new Gesichter(random(-200, height), random(-200, height), 200);
}
}
Or make it static and reference it by the class name:
public class Gesichter {
public static final float WH_ELLIPSE = 200;
...
}
void setup() {
for (int i = 0; i < rots.length; i ) {
rots[i] = new Gesichter(random(-Gesichter.WH_ELLIPSE, height), random(-Gesichter.WH_ELLIPSE, height));
}
}