[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Help : speed
Hi,
I am just playing with pygame trying to build a simple 2D platform game
with characters moving along the horizontal axis.
I want to be able to move them at different speeds.
How to do that ?
Each character has a self.speed attribute, which contains the number of
pixels per second.
I set up a global clock (called "counter") ranging from 0 to 1 second and
then resetting to 0.
Since I want to keep the animation as smooth as possible, I think movements
must be always by one pixel at a time, so the question is WHEN to move
characters going at different speeds within the time unit.
For example, suppose I have the following characters :
-"Hero" moving at 2 pixels per seconds => it moves by one pixel when the
clock is at 0.5 seconds (or 500 milliseconds) and when it is at 1 second
(or 1000 milliseconds).
-"Villain" with speed of 5 pixels per seconds => it moves by one pixel when
clock is at 0.2 - 0.4 - 0.6 - 0.8 - 1.00
How to translate this approach in Python ?
I tried the following code ("move" is a function of the class "character")
:
--------------------------------------------------
def move(self):
global counter
self.sp = counter*self.speed
if self.sp == int(self.sp) :
# clock has reached the proper value to move the
# character according to its speed
self.pos = self.pos.move(1, 0)
# moves by one pixel to the right
self.sp = 0
return
--------------------------------------
But it does not work.
Any help is appreciated. Thanks in advance.
Fabrizio
__________________________________________________________________
Tiscali ADSL SENZA CANONE:
Attivazione GRATIS, contributo adesione GRATIS, modem GRATIS,
50 ore di navigazione GRATIS. ABBONARTI TI COSTA SOLO UN CLICK!
http://point.tiscali.it/adsl/index.shtml