How to redraw while in another method?

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:

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.

1 Like

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

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

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

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

1 Like

from time import sleep

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

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

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: :crazy_face:

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

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.

The right way to decrease a sketch’s FPS is via the frameRate() method: :face_with_monocle:
http://Py.Processing.org/reference/frameRate.html

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