# Draw function not working properly

**URL:** <https://discourse.processing.org/t/draw-function-not-working-properly/39086>\
**Category:** Processing.py\
**Created:** [October 4, 2022, 7:27pm UTC](https://discourse.processing.org/t/draw-function-not-working-properly/39086 "2022-10-04T19:27:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben0981](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Ben0981](https://discourse.processing.org/u/Ben0981)\
**Post date:** [October 4, 2022, 7:27pm UTC](https://discourse.processing.org/t/draw-function-not-working-properly/39086/1 "2022-10-04T19:27:23Z")

</div>

Hey, so I am new to processing and am currently just trying to move 2 rectangles by a given velocity but whenever I try actually moving them nothing gets drawn at all but if I just keep them stationary they get drawn just fine.

```
def draw():
  background(255)
  fill(0)
  rect(c1x, 200, 100, 100)
  rect(c2x, 250, 50, 50)

  c1x += v1
  c2x += v2

```

This is my draw function which won’t work when I keep the two last lines in, but works perfectly fine if I remove them. As far as I know, there shouldn’t be anything wrong with this bit’s syntax or logic. Thanks in advance for helping.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 4, 2022, 7:32pm UTC](https://discourse.processing.org/t/draw-function-not-working-properly/39086/2 "2022-10-04T19:32:00Z")

</div>

> **[Globals \\ Language (API)](https://py.processing.org/reference/globals.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [October 4, 2022, 7:33pm UTC](https://discourse.processing.org/t/draw-function-not-working-properly/39086/3 "2022-10-04T19:33:55Z")

</div>

To add to @GoToLoop’s response – you overwrite the `c1x` value, so you’ll need a `global c1x` in the `draw()` –

```python
c1x = 10
v1 = 5

def draw():
  global c1x
  background(255)
  fill(0)
  rect(c1x, 200, 100, 100)
  c1x += v1

```

The same goes for `c2x`.

_(you don’t reassign any values to `v1` and `v2`, so it’s not necessary to insert a `global` for those)_

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [October 4, 2022, 7:33pm UTC](https://discourse.processing.org/t/draw-function-not-working-properly/39086/4 "2022-10-04T19:33:57Z")

</div>

Hello, @Ben0981, and welcome to Processing Foundation Discourse!

Where were those four variables in the last two lines initialized?

Are they meant to be global variables?

---

<div class="post-metadata">

**Author:** ![Ben0981](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Ben0981](https://discourse.processing.org/u/Ben0981)\
**Post date:** [October 4, 2022, 7:36pm UTC](https://discourse.processing.org/t/draw-function-not-working-properly/39086/5 "2022-10-04T19:36:06Z")

</div>

Thank you all for the quick help, they were globals that I had defined at the start but since I never really used globals in normal python I forgot that I had to add the global statement in the function to modify them.
