Skip to content

Instantly share code, notes, and snippets.

@iminurnamez
Created January 31, 2015 00:53
Show Gist options
  • Save iminurnamez/f590cc4110be938a8722 to your computer and use it in GitHub Desktop.
Save iminurnamez/f590cc4110be938a8722 to your computer and use it in GitHub Desktop.
import itertools as it
import pygame as pg
#IMAGE LOADING
#a list of all image names
image_names = ["EnemyMoverRed", "EnemyMoverBlue", "BossMoverRed"]
#Make a dict to hold the images so they're only loaded once.
#a dict comprehension, similar to using a for loop to add to a dictionary
IMAGES = {name: pg.image.load("{}.png".format(name)).convert() for name in image_names}
#SIMPLE ANIMATION: using itertools.cycle and a tick count
#Make a separate class that inherits from Enemy for each specific kind of enemy
class RegularEnemy(Enemy):
images = [IMAGES["EnemyMoverRed"], IMAGES["EnemyMoverBlue"]]
def __init__(self, pos):
super(RegularEnemy, self).__init__(pos) #whatever args Enemy.__init__ needs
#create a cycle (an endless iterator)
self.image_cycle = it.cycle(images)
self.image = next(self.image_cycle)
self.ticks = 0
def update(self):
self.ticks += 1
if self.ticks % 6 == 0:
self.image = next(self.image_cycle)
def draw(self, surface):
surface.blit(self.image, self.rect)
#create an Enemy instance
enemy = RegularEnemy((50, 50))
#once per frame in main loop
for enemy in enemies:
enemy.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment