I need help for a school project

Hi guys,

For my school project we have to do an assignment with the processing program. I have made two things but I want to combine them but I’m not advanced enough to do so since I’ve just started a few weeks ago.

This is my first code:

static final int NUM_LINES = 10;
float t;

void setup() {
  background(70);
  size(800,600);
 }

void draw() {
  background(255);
  stroke(100, 10, 100);
  strokeWeight(6);
  
  translate(width/2, height/2);
  
  for (int i = 0; i < NUM_LINES; i++) {
  line(x1(t+i), y1(t+i), x2(t+i), y2(t+i));
  }
  
  t += 0.7;
 }
 
float x1(float t) {
  return sin(t / 10) * 100 + sin(t / 5) * 20;
  }
  
float y1(float t) {
  return sin(t / 10)  * 100 ;
  }
 
 float x2(float t) {
  return sin(t / 10) * 200 + sin(t) * 2;
  }
  
float y2(float t) {
  return cos(t / 20)  * 200 + cos(t / 12) * 20;
  }

I want to combine this code with a bouncing ball that I’ve also made:

float x = 400;
float y = 300;

float xSpeed = 5;
float ySpeed = 5;
void setup () {
  size(800,600);
}

void draw () {
  background(0);
  
  x += xSpeed;
 if (x > width || x < 0) {
   xSpeed *= -1;
   }
   
  y += ySpeed;
 if (y > height || y < 0) {
   ySpeed *= -1;
   }
   
 ellipse(x,y,50,50 );
}

Is this possible??? Please help me hahaha I don’t know what to do. And besides, can I also combine the first code with itself? So that I have that one two times in the same code but a bit different.

you can merge the 2 codes.

  • make sure you have only one setup and one draw (and in setup only one size and in draw only one background)

  • before setup have the global vars of both sketches.

done.

But which part of the code should I copy then? because I get errors with everything I try…

Well one function setup

One draw with elements from previous draw 1 and previous draw 2 together in one function draw()

The the other functions like x1, y1…

Place brackets correctly

Show your attempt of the new merged sketch

i would come up with something like this:

static final int NUM_LINES = 10;
float t;
PImage img;
float xSpeed = 5;
float ySpeed = 5;

void setup() {
  background(0);
  size(800,600);
 }

void draw() {
  img = loadImage("finland.jpg");
  background(img);
  stroke(0, random(200), random(50));
  strokeWeight(6);
  
  translate(width/2, height/2);
  
  for (int i = 0; i < NUM_LINES; i++) {
  line(x1(t+i), y1(t+i), x2(t+i), y2(t+i));
  }
  
  t += 0.7;
 }
 
float x1(float t) {
  return sin(t / 10) * 100 + sin(t / 5) * 20;
  }
  
float y1(float t) {
  return sin(t / 10)  * 100 ;
  }
 
 float x2(float t) {
  return sin(t / 10) * 200 + sin(t) * 2;
  }
  
float y2(float t) {
  return cos(t / 20)  * 200 + cos(t / 12) * 20;
  }
 
**x += xSpeed;**
if (x > width || x < 0) {
xSpeed *= -1;
}

y += ySpeed;
if (y > height || y < 0) {
ySpeed *= -1;
}

ellipse(x,y,50,50 );
}

but it get troubles with the xspeed

All this belongs in draw

You copied it wrong

See your 2nd initial sketch

Also hit ctrl-t to get better indents

static final int NUM_LINES = 10;
float t;
PImage img;
float xSpeed = 5;
float ySpeed = 5;

void setup() {
  background(0);
  size(800, 600);
}

void draw() {
  img = loadImage("finland.jpg");
  background(img);
  stroke(0, random(200), random(50));
  strokeWeight(6);
  x += xSpeed;
  if (x > width || x < 0) {
    xSpeed *= -1;
  }

  y += ySpeed;
  if (y > height || y < 0) {
    ySpeed *= -1;
  }

  ellipse(x, y, 50, 50 );
}

translate(width/2, height/2);

for (int i = 0; i < NUM_LINES; i++) {
  line(x1(t+i), y1(t+i), x2(t+i), y2(t+i));
}

t += 0.7;
}

float x1(float t) {
  return sin(t / 10) * 100 + sin(t / 5) * 20;
}

float y1(float t) {
  return sin(t / 10) * 100 ;
}

float x2(float t) {
  return sin(t / 10) * 200 + sin(t) * 2;
}

float y2(float t) {
  return cos(t / 20) * 200 + cos(t / 12) * 20;
}

okay so i put the small part below draw but then i get the error that i’m mixing static and active mode

You want to get rid of this }

okay that helps but then again the xSpeed is the problem. It says : ‘x cannot be resolved to a variable’.

Yes you forgot these lines from the initial sketch

yes it workss!! thank you!! is it also possible to then add another line? The first part from the initial code but then with different values vor x1 etc

Of course it’s possible