Draw function not working properly

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.

2 Likes

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

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)

2 Likes

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?

1 Like

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.

3 Likes