python - Pygame - Moving Sprites at Different Speeds -


i working on mechanics of several sprites using. working on game involves collecting coins. have sprite image of spinning coin. created class coin sprite , wrote update function switches 1 image sprite next. coin spinning problem i'm having is spinning fast. how can slow dow spinning of coin? appreciated. thanks!

here coin class:

class coins (pygame.sprite.sprite): coin_spin_frames = [] def __init__(self, x, y):     pygame.sprite.sprite.__init__(self)     sprite_sheet = spritesheet('stuff/coins_1.png')      image = sprite_sheet.get_image(0, 0, 40, 42)     self.coin_spin_frames.append(image)     image = sprite_sheet.get_image(0, 42, 40, 42)     self.coin_spin_frames.append(image)     image = sprite_sheet.get_image(0, 84, 40, 42)     self.coin_spin_frames.append(image)     image = sprite_sheet.get_image(0, 130, 40, 42)     self.coin_spin_frames.append(image)      self.image = self.coin_spin_frames[0]      self.rect = self.image.get_rect()      self.rect.x = x     self.rect.y = y def update(self):     if self.image == self.coin_spin_frames[0]:         self.image = self.coin_spin_frames[1]     elif self.image == self.coin_spin_frames[1]:         self.image = self.coin_spin_frames[2]     elif self.image == self.coin_spin_frames[2]:         self.image = self.coin_spin_frames[3]     elif self.image == self.coin_spin_frames[3]:         self.image = self.coin_spin_frames[0] 

first, determine how fast want coin spin. let's want take 2 seconds make complete loop.

the update() function can call time.time(), , mod amount of time make loop, divide loop time. (123121213.2 % 2 = 1.2. 1.2 / 2 = 0.6). now, display image 60% of way through self.coin_spin_frames list. (int(len(self.coin_spin_frames) * 0.6))


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -