# I need help for a school project

**URL:** https://discourse.processing.org/t/i-need-help-for-a-school-project/17227
**Category:** Coding Questions
**Tags:** homework
**Created:** [January 21, 2020, 12:58pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227 "2020-01-21T12:58:57Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![linnd](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnd](https://discourse.processing.org/u/linnd)
#### Post date: [January 21, 2020, 12:58pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/1 "2020-01-21T12:58:57Z")

</div>

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 21, 2020, 1:29pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/2 "2020-01-21T13:29:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![linnd](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnd](https://discourse.processing.org/u/linnd)
#### Post date: [January 21, 2020, 1:32pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/3 "2020-01-21T13:32:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 21, 2020, 1:37pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/4 "2020-01-21T13:37:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![linnd](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnd](https://discourse.processing.org/u/linnd)
#### Post date: [January 21, 2020, 1:46pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/5 "2020-01-21T13:46:50Z")

</div>

i would come up with something like this:

```auto
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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 21, 2020, 1:57pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/6 "2020-01-21T13:57:21Z")

</div>

> [@linnd](#):
>
> **x += xSpeed;**  
> if (x \> width || x \< 0) {  
> xSpeed \*= -1;  
> }
> 
> y += ySpeed;  
> if (y \> height || y \< 0) {  
> ySpeed \*= -1;  
> }
> 
> ellipse(x,y,50,50 );  
> }

All this belongs in draw

You copied it wrong

See your 2nd initial sketch

Also hit ctrl-t to get better indents

---

<div class="post-metadata">

### Author: ![linnd](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnd](https://discourse.processing.org/u/linnd)
#### Post date: [January 21, 2020, 2:59pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/7 "2020-01-21T14:59:10Z")

</div>

```auto
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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 21, 2020, 3:02pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/8 "2020-01-21T15:02:35Z")

</div>

> [@linnd](#):
>
> ellipse(x, y, 50, 50 );  
> }

You want to get rid of this }

---

<div class="post-metadata">

### Author: ![linnd](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnd](https://discourse.processing.org/u/linnd)
#### Post date: [January 21, 2020, 3:13pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/9 "2020-01-21T15:13:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 21, 2020, 3:14pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/10 "2020-01-21T15:14:48Z")

</div>

> [@linnd](#):
>
> float x = 400;  
> float y = 300;

Yes you forgot these lines from the initial sketch

---

<div class="post-metadata">

### Author: ![linnd](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnd](https://discourse.processing.org/u/linnd)
#### Post date: [January 21, 2020, 3:17pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/11 "2020-01-21T15:17:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 21, 2020, 3:19pm UTC](https://discourse.processing.org/t/i-need-help-for-a-school-project/17227/12 "2020-01-21T15:19:36Z")

</div>

Of course it’s possible
