Merging two sketches (for a school project)

I have been working on two codes that I would now like to have in the same frame, one over the other. I am asking for advice or just straight up the full solutions to merging these two codes. I have tried everything that I know and have yet to come up with the results that I want, especially since there are many repeated lines on either code (if that makes sense). Code #1 is the background, code #2 the foreground;
CODE #1 (background)

float x, y;

float xStep = 50;

float yStep = 50;

float a, a_;

int num = 328;

void setup() {

size(200, 200);

}

void draw () {

background(15, 20, 30);

strokeWeight(xStep);

strokeCap(CORNER);

int n=8;

while (n<num) {

stroke (255-255cos(radians (a)),255cos(radians(a)),255-255sin(radians(a)), 255-255sin(radians(a)));

line (x, y, x, y+yStep);

x+=xStep;

if (x>width) {

x=xStep/2;

y+=yStep;

}

if (y>=height) {

y=0;

a=0;

}

n++;

a+=a_;

}

a_+=0.1;

}

CODE #2 (foreground)

float angle;
float x, y;
float xStep = 50;
float yStep = 50;
float a, a_;
int num = 328;

void setup() {
size(200, 200);
surface.setLocation(175,0);
strokeWeight (5);
}
void draw () {
{
background(15, 20, 30);
strokeWeight(xStep);
strokeCap(CORNER);
int n=8;
while (n<num) {
stroke (255-255cos(radians (a)),255cos(radians(a)),255-255sin(radians(a)), 255-255sin(radians(a)));
line (x, y, x, y+yStep);
x+=xStep;
if (x>width) {
x=xStep/2;
y+=yStep;
}
if (y>=height) {
y=0;
a=0;
}
n++;
a+=a_;
}
a_+=0.1;
}
background (300);
translate(width/2, height/2);
for (int i=0; i < 10; i ++){
fill (i90, 300-i90, 400-i*800);
scale (0.99);
rotate(radians(angle));
rect (0, 0, 75, 75);
angle+=0.03;
}
}

Thank you for reading this post, any advice can help as I am still a beginner!

Hello,

Welcome to the forum.

Our Homework Policy:
https://discourse.processing.org/faq#homework

It clearly states:

  • Do not ask for complete code solutions.
  • Do not offer complete code solutions.

Please format your code:
https://discourse.processing.org/faq#format-your-code

:)

1 Like

As noted we can not write your code for you, but we can give you some hints on how to achieve your goal.

  1. I would start with two separate sketches and make certain that each is error free. You have some errors in yours to begin with; those have to be fixed first (more on that later). After each is working independently you will be ready to combine them.
  2. Compare the two sketches side by side (its probably a good idea to work off of a copy of each so you don’t lose the originals). Then decide which one is going to be the donor and which one is going to be the recipient (final merged file).
  3. Compare the two line by line. If you see a piece of code in the donor that you don’t have in the recipient, copy/paste it over into the latter.
  4. When you have everything copied over hit run and it should work without error if you have done it correctly.

Fixing errors:

  1. When you want to multiply two numbers computers usually require an asterisk(*) between the two, eg x*124. ( Formatting your posted code may reinsert them.)
  2. Your merged file will call background() twice; the second will erase the first if you leave both of them in. I would comment out the second one so that you only call it once.

There are some other smaller problems but those can be discussed later. Your project is certainly doable.

1 Like

Thanks for your help!