# How to redraw while in another method?

**URL:** https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222
**Category:** Processing.py
**Created:** [June 24, 2018, 7:40pm UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222 "2018-06-24T19:40:29Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![benjaminnow](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@benjaminnow](https://discourse.processing.org/u/benjaminnow)
#### Post date: [June 24, 2018, 7:40pm UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/1 "2018-06-24T19:40:29Z")

</div>

I’m making a programming game for kids to learn to program using processing and python. It involves a wombat which picks up leaves in tiles and there may be some obstacles like rocks. This might be familiar to you if you did something in Greenfoot. I am making it in python though, and have little experience with Processing.

The wombatgame file runs the game. The leaves, rocks, world, and wombat are objects. A world is created with a wombat and some leaves. I then have a player class which is where the kids will program the wombat to do certain tasks. In the code(main method in player class) included I have a wombat which makes 4 steps in the direction it is facing.

The problem is that the wombat will move(x and y variables are changing for it) but it won’t display every movement until the end. It teleports instead of showing the user every step graphically. I commented out the world.play() user code in the draw() function because that is not where I want to put it. It teleports because it can only update the screen though world.display() when the world.play() method has finished. I call redraw in the wombat class after a movement which should run the world.display() method but it doesn’t do the code in the draw() function(which has world.display()) until world.play() is finished. I need to figure out how to redraw while running world.play()

Here is the code:

> <https://gist.github.com/benjaminnow/544fb3fb091206656464be2caaa91d80>
>
> There are more than three files. show original

Any suggestions on how to improve the code or organization of the game are welcome.

---

<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: [June 25, 2018, 12:06am UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/2 "2018-06-25T00:06:02Z")

</div>

> [@benjaminnow](#):
>
> Any suggestions on how to improve the code or organization of the game are welcome.

1st thing I’ve noticed is that you have **loadImage()** inside your classes.

It means that the very same image resource is gonna be unnecessarily reloaded & stored as another PImage object each time a new instance is created!

As a rule of thumb, load all resources in **setup()** and store them in global arrays.

Your classes can then access those resources directly or request them in their constructor.

---

<div class="post-metadata">

### Author: ![benjaminnow](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@benjaminnow](https://discourse.processing.org/u/benjaminnow)
#### Post date: [June 25, 2018, 12:17am UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/3 "2018-06-25T00:17:25Z")

</div>

> [@GoToLoop](#):
>
> It means that the very same image resource is gonna be unnecessarily reloaded & stored as another PImage object each time a new instance is created!

I can see how that could add a lot of overhead if I were to spawn a lot of objects. Thanks for the suggestion.

---

<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: [June 25, 2018, 12:20am UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/4 "2018-06-25T00:20:27Z")

</div>

I’ve also noticed you’ve got no “.pyde” file in your posted gist! 😨

Seems like your “wombatgame.py” should be renamed to “wombatgame.pyde” so your sketch can run in Python Mode. 🤓

Of course, all your files need to be inside a folder which matches the name of that “.pyde” file. 🤠

---

<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: [June 25, 2018, 12:37am UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/5 "2018-06-25T00:37:55Z")

</div>

> `from time import sleep`

Seems like you’re applying everywhere something which pauses the sketch’s “Animation Thread”! 😮

But when we do so, it also pauses the rate on which the sketch’s renders the main canvas to the window! 😱

If you want, I’ve written a class Countdown which sets its _done_ property to `True` when a specified _delay_ has transpired after we invoke its **start()** method: 🤪

- [Countdown Class library for Java, JS & Python - Processing 2.x and 3.x Forum](https://Forum.Processing.org/two/discussion/27733/countdown-class-library-for-java-js-python)

There are 2 versions of it for “Python Mode” though. Take your pick: 😊

> <https://gist.github.com/GoToLoop/a476ac6a01d18700240853fed33c0e57>

> <https://gist.github.com/GoToLoop/893dc18856a7842b76a92d8aea837da1>

---

<div class="post-metadata">

### Author: ![benjaminnow](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@benjaminnow](https://discourse.processing.org/u/benjaminnow)
#### Post date: [June 25, 2018, 12:43am UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/6 "2018-06-25T00:43:06Z")

</div>

> [@GoToLoop](#):
>
> Seems like you’re applying everywhere something which pauses the sketch’s “Animation Thread”! 😮
> 
> But when we do so, it also pauses the rate on which the sketch’s renders the main canvas to the window! 😱
> 
> If you want, I’ve written a class Countdown which sets its _done_ property to `True` when a specified _delay_ has transpired after we invoke its **start()** method: 🤪

I know that time.sleep() is a blocking command but I want it to block because then the animation will be slower right? If I didn’t have time.sleep() it would pretty much teleport because it wouldn’t have to wait in between walk commands.

---

<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: [June 25, 2018, 12:53am UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/7 "2018-06-25T00:53:32Z")

</div>

> [@benjaminnow](#):
>
> … but I want it to block because then the animation will be slower right?

The right way to decrease a sketch’s FPS is via the **frameRate()** method: 🧐

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

---

<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: [June 25, 2018, 1:33am UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/8 "2018-06-25T01:33:47Z")

</div>

> **[r/processing - Need help with image animation](https://www.reddit.com/r/processing/comments/8tinw2/need_help_with_image_animation/)**
>
> 6 votes and 3 comments so far on Reddit

---

<div class="post-metadata">

### Author: ![benjaminnow](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@benjaminnow](https://discourse.processing.org/u/benjaminnow)
#### Post date: [June 25, 2018, 4:52pm UTC](https://discourse.processing.org/t/how-to-redraw-while-in-another-method/1222/9 "2018-06-25T16:52:16Z")

</div>

Thanks, I’ll keep trying to clean up the code and debug 🙂
