python 2.7 - Fish swim in pygame -
i want use python pygame module make 2d "fishtank".
basically load jpg image background , load gif animated image depicting fish swimming , make move.
i know how make static image move how make gif image move while image animated.
i have pil installed, sure if need use it.
if not work, can break apart gif file several static frames , cyclically make each of them show on screen. when latter 1 posted, former 1 needs removed, how remove ?
here class used in name pygame animation work:
class animation(pygame.sprite.sprite): def __init__(self, img, fps = 6): # call parent class (sprite) constructor pygame.sprite.sprite.__init__(self) # slice source image array of images self.images = self.loadsliced(img) # track time started, , time between updates. # can figure out when have switch image. self._start = pygame.time.get_ticks() self._delay = 1000 / fps self._last_update = 0 self._frame = 0 self.image = self._images[self._frame] def loadsliced(w, h, filename): """ pre-conditions: master can height sprites frames must same width master width must len(frames)*frames.width arguments: w -- width of frame in pixels h -- height of frame in pixels filename -- master image animation """ images = [] if fileexists( img, "animation master image"): master_image = pygame.image.load(filename).convert_alpha() master_width, master_height = master_image.get_size() in range(int(master_width/w)): images.append(master_image.subsurface((i*w, 0, w, h))) return images def update(self, t): # note doesn't work if it's been more self._delay # time between calls update(); update image once # then. should updated twice if t - self._last_update > self._delay: self._frame += 1 if self._frame >= len(self._images): self._frame = 0 self.image = self._images[self._frame] self._last_update = t self.image = self._images[self._frame] self._last_update = t def getframe(self, screen): # update frame , display sprite self.update(pygame.time.get_ticks()) return self.image
Comments
Post a Comment