So I was coding a simple python game the other day using pygame, and I thought this might be helpful for someone:
class Test(pygame.sprite.Sprite):
# Constructor function
def __init__(self,x,y):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Set init speed
self.change_x=0
self.change_y=0
self.facing = 0
# Set height, width
self.image = pygame.image.load('baddie.png')
self.down = [pygame.image.load('baddie.png').convert_alpha(),
pygame.image.load('baddie1.png').convert_alpha(),
pygame.image.load('baddie2.png').convert_alpha(),
pygame.image.load('baddie3.png').convert_alpha(),
pygame.image.load('baddie2.png').convert_alpha(),
pygame.image.load('baddie1.png').convert_alpha()]
self.left = [pygame.image.load('baddiel.png').convert_alpha(),
pygame.image.load('baddiel1.png').convert_alpha(),
pygame.image.load('baddiel2.png').convert_alpha(),
pygame.image.load('baddiel3.png').convert_alpha(),
pygame.image.load('baddiel2.png').convert_alpha(),
pygame.image.load('baddiel1.png').convert_alpha()]
self.right = [pygame.image.load('baddier.png').convert_alpha(),
pygame.image.load('baddier1.png').convert_alpha(),
pygame.image.load('baddier2.png').convert_alpha(),
pygame.image.load('baddier3.png').convert_alpha(),
pygame.image.load('baddier2.png').convert_alpha(),
pygame.image.load('baddier1.png').convert_alpha()]
self.up = [pygame.image.load('baddieu.png').convert_alpha(),
pygame.image.load('baddieu1.png').convert_alpha(),
pygame.image.load('baddieu2.png').convert_alpha(),
pygame.image.load('baddieu3.png').convert_alpha(),
pygame.image.load('baddieu2.png').convert_alpha(),
pygame.image.load('baddieu1.png').convert_alpha()]
self.imgcounter = 0
self.imgvar = 0
# Make our top-left corner a required input for the class
self.rect = self.image.get_rect()
self.rect.topleft = [x,y]
# Change the speed of the player
def changespeed(self,x,y):
self.change_x+=x
self.change_y+=y
# Find a new position for the player
def update(self):
# Get the old position, in case we need to go back to it
old_x=self.rect.topleft[0]
old_y=self.rect.topleft[1]
# Update position according to our speed (vector)
new_x=old_x+self.change_x
new_y=old_y+self.change_y
# Put the player in the new spot
self.rect.topleft = (new_x,new_y)
if self.change_x == 0 and self.change_y == 0:
#self.imgvar = 0
if self.facing == 3:
self.image = self.left[0]
if self.facing == 2:
self.image = self.down[0]
if self.facing == 4:
self.image = self.right[0]
if self.facing == 1:
self.image = self.up[0]
if self.change_x < 0:
self.facing = 3
if self.imgcounter == 0:
self.image = self.left[0]
#self.imgcounter =+ 1
self.imgcounter += 1
self.image = self.left[self.imgvar]
if self.imgcounter > 1:
self.imgcounter = 0
self.imgvar += 1
if self.imgvar > len(self.left)-1:
self.imgvar = 0
#self.image = self.left[self.imgvar]
if self.change_x > 0:
self.facing = 4
if self.imgcounter == 0:
self.image = self.right[0]
#self.imgcounter =+ 1
self.imgcounter += 1
self.image = self.right[self.imgvar]
if self.imgcounter > 1:
self.imgcounter = 0
self.imgvar += 1
if self.imgvar > len(self.right)-1:
self.imgvar = 0
if self.change_y < 0:
self.facing = 1
if self.imgcounter == 0:
self.image = self.up[0]
#self.imgcounter =+ 1
self.imgcounter += 1
self.image = self.up[self.imgvar]
if self.imgcounter > 1:
self.imgcounter = 0
self.imgvar += 1
if self.imgvar > len(self.up)-1:
self.imgvar = 0
if self.change_y > 0:
self.facing = 2
if self.imgcounter == 0:
self.image = self.down[0]
#self.imgcounter =+ 1
self.imgcounter += 1
self.image = self.down[self.imgvar]
if self.imgcounter > 1:
self.imgcounter = 0
self.imgvar += 1
if self.imgvar > len(self.down)-1:
self.imgvar = 0
It's a simple sprite created to help test some animations I created. Keeps track of directions and the update function flips through an array of images. You can easily adjust this to pull in images from a sprite sheet instead of using several image files.
It doesn't really do much else, but it's useful for testing out sprite animations.
-Newt
No comments:
Post a Comment