Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The way to go here, is to improve your goblin ?(and other characters) handling in your program from functions to proper classes, so they can have internal states. </p> <p>Each such class them can have variables which are updated at every game frame, and which trigger an action when the count gets to a value.</p> <p>This solution is cleanr than adding concurrent threads just for timing accounting, and properly done, can lead to an application design wich will be a bliss to develop, expand and maintain. </p> <p>A short example of the style you could adopt, preserving the functionality in your code. (Note that you can use the pygame.sprite.Sprite classes to have the "update" method called authomaticaly for each object group - I will do it from scratch bellow)</p> <p>frame_delay = 20</p> <pre><code>class Goblin(object): def __init__(self): self.x = 1 #or whatever self.y = 1 #or whatever self.movement_count = 0 self.move_at_each = 10 def move(self): self.movement_count += 1 if self.movement_count &lt; self.move_at_each: return self.movement_count = 0 self.x += 32 </code></pre> <p>In your main loop, you instantiate a goblin doing:</p> <p><code>goblin = Goblin()</code>, before entering the while loop, and call <code>goblin.move()</code> at each frame. This way you can have a proper goblin class, which will allow you to have multiple "goblins" in your game - while the code on your example would require you to hard code each character. And you simply call the "goblin.move" method at each frame of your game. The example above is tunned to move each 10 frames, but you can change that (and can change that for each individual goblin you instantiate).</p> <p>As I said, pygame offers the "Sprite" and "sprite groups" classes as a framework for this kind of mechanics already, but rolling your own in a some smaller game will help you to understand better the needs and what pygame sprites already offer, so you can use them in later projects (or a later stage of this project)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload