There are a number of ways to have python move game objects around the screen. Up until recently I hadn't really tried much other than tying keys to a function that would move a rectangle object by an offset.
Something like this:
*** in the sprite class***
def change_pos(self, x, y):
self.change_x += x
self.change_y += y
*** in the main loop ***
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_UP:
player.change_pos(0, -PS)
I'd set up a global for player speed (PS) that I could hand to the change_pos function as a positive or negative value based on the direction the player wanted to go.
This way works fine, but it's not really all that flexible. What if you wanted to have the player sprite keep moving a bit after the player releases the movement keys? For that you'd need to use a bit of physics. There are a ton of different ways to do something like this, and I've read through some tutorials on it. Since I'm not very good with math though, most of it was way over my head. I came up with a very simplistic approach to the whole thing.
I created a sprite object with an attribute to store a list of forces that will act as inertia to be applied to the object. Then I took out the change_pos() function and replaced it with this:
def apply_force():
for i in self.forces:
i -= friction
if self.up:
self.forces[0] += speed
if self.down:
self.forces[1] +=speed
if self.left:
self.forces[2] +=speed
if self.right:
self.forces[3] +=speed
In the player sprite's update function, I can say:
def update():
self.apply_force()
self.rect.top += (self.forces[1] - self.forces[0])
self.rect.left += (self.forces[3] - self.forces[2])
You can play around with the values that you use for speed, and friction. Speed is how fast your player object gains speed and friction is how fast the object is slowed. You may want to add something to the end of your apply_force() function to cap the forces. I used something like this:
for i in self.forces:
if i > 200:
i=200
if i < 0:
i=0
Also, if you want, you can have your main loop use actual time to update instead of FPS which can be affected by the power of the system you're running on. Up until recently I had been using the following in my main loop:
clock = pygame.time.Clock()
while 1:
clock.tick(60)
It actually makes more sense to use the following:
FPS = 60
clock = pygame.time.Clock()
while 1:
milliseconds = clock.tick(FPS)
seconds = milliseconds / 1000.0
Then change the player class update function to take seconds as an argument. This will give you a much more consistent movement:
self.rect.top += (self.forces[1] - self.forces[0]) * seconds
self.rect.left += (self.forces[3] - self.forces[2]) * seconds
There are of course, more accurate ways to produce the same effect, but I found this pretty simple to understand and easy to implement.
-Newt
Showing posts with label Animation. Show all posts
Showing posts with label Animation. Show all posts
Tuesday, May 14, 2013
Wednesday, April 10, 2013
Game Flair
I know I'm deviating a bit from that last couple posts about designing a simple game with pygame libraries. But I stumbled upon a video that got me thinking about some of the little things you can add to a game to make them more interesting. If you're interested this is the video I'm referring to.
One really simple idea that they had was to use tweening or easing. Instead of just placing a new sprite or game object on the screen, we have it drop into place or instead of game objects just vanishing, you can use this to slowly scale them out of existence.
The way they did this was using something like:
x += (target-x) *.1
This is probably the most simple approach to tweening. Basically we give the sprite or rectangle a target and a starting point. Then we move the x by 10 percent until it gets to the destination or target. Simple right?
And it provides a really nice effect to the game for very little cost.
That's the easiest implementation of tweening/easing. If you want to get fancier you have to do a bit more math.
Here's a basic function to get you started
def linearTween(time, startPoint, change, endPoint):
return change * time / endPoint + startPoint
time is the current time in frames, steps, whatever counter you want to use. As long as it is a percentage of the endPoint
startPoint is the value where we begin
change is the difference between the beginning and destination values
endPoint is the end time or frame or whatever for your tween.
Basically it's the same thing as x += (target-x) *.1 since you have a start, a target, diff, and percentage of change. However in this way we have a number of variables we can use in a bunch of different ways.
Check out this site: http://www.timotheegroleau.com/
He has a generator for easing functions that you can use for all sorts of fun stuff. In fact, Tim Groleau has a collection of material on the subject if you're interested.
-Newt
One really simple idea that they had was to use tweening or easing. Instead of just placing a new sprite or game object on the screen, we have it drop into place or instead of game objects just vanishing, you can use this to slowly scale them out of existence.
The way they did this was using something like:
x += (target-x) *.1
This is probably the most simple approach to tweening. Basically we give the sprite or rectangle a target and a starting point. Then we move the x by 10 percent until it gets to the destination or target. Simple right?
And it provides a really nice effect to the game for very little cost.
That's the easiest implementation of tweening/easing. If you want to get fancier you have to do a bit more math.
Here's a basic function to get you started
def linearTween(time, startPoint, change, endPoint):
return change * time / endPoint + startPoint
time is the current time in frames, steps, whatever counter you want to use. As long as it is a percentage of the endPoint
startPoint is the value where we begin
change is the difference between the beginning and destination values
endPoint is the end time or frame or whatever for your tween.
Basically it's the same thing as x += (target-x) *.1 since you have a start, a target, diff, and percentage of change. However in this way we have a number of variables we can use in a bunch of different ways.
Check out this site: http://www.timotheegroleau.com/
He has a generator for easing functions that you can use for all sorts of fun stuff. In fact, Tim Groleau has a collection of material on the subject if you're interested.
-Newt
Wednesday, April 3, 2013
Pygame Sprite Animation
Okay, animation. Not just moving rectangles either, that stuff is covered by a number of other tutorials freely available on the interwebz. I posted an earlier version of this class a while back, but I noticed some weirdness with the way I was changing images. This is a revised version. Let's just jump right into the code and I'll explain as best I can after.
class Player(pygame.sprite.Sprite):
# initialize the player object
# requires x and y coordinates
def __init__(self,x,y):
# initialize the sprite (parent class)
pygame.sprite.Sprite.__init__(self)
# Set initial variables
#change_x and y are used to move the object around the screen
self.change_x=0
self.change_y=0
#facing keeps track of the direction
#this is important so we know which set of images to use
self.facing = 0
#here are the images we're going to loop over depending on the direction
#start by setting the image to the image to start with
self.image = pygame.image.load('hero.png')
self.down = [pygame.image.load('hero.png').convert_alpha(),
pygame.image.load('hero1.png').convert_alpha(),
pygame.image.load('hero2.png').convert_alpha(),
pygame.image.load('hero3.png').convert_alpha(),
pygame.image.load('hero2.png').convert_alpha(),
pygame.image.load('hero1.png').convert_alpha()]
self.left = [pygame.image.load('herol.png').convert_alpha(),
pygame.image.load('herol1.png').convert_alpha(),
pygame.image.load('herol2.png').convert_alpha(),
pygame.image.load('herol3.png').convert_alpha(),
pygame.image.load('herol2.png').convert_alpha(),
pygame.image.load('herol1.png').convert_alpha()]
self.right = [pygame.image.load('heror.png').convert_alpha(),
pygame.image.load('heror1.png').convert_alpha(),
pygame.image.load('heror2.png').convert_alpha(),
pygame.image.load('heror3.png').convert_alpha(),
pygame.image.load('heror2.png').convert_alpha(),
pygame.image.load('heror1.png').convert_alpha()]
self.up = [pygame.image.load('herou.png').convert_alpha(),
pygame.image.load('herou1.png').convert_alpha(),
pygame.image.load('herou2.png').convert_alpha(),
pygame.image.load('herou3.png').convert_alpha(),
pygame.image.load('herou2.png').convert_alpha(),
pygame.image.load('herou1.png').convert_alpha()]
# give the object a rectangle using image size
self.rect = self.image.get_rect()
# set the topleft location to the x and y coordinates
self.rect.topleft = [x,y]
# Used to move the object
def changespeed(self,x,y):
self.change_x+=x
self.change_y+=y
# This is where we update the images and location of the player object
def update(self):
# 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)
# This next bit is where we change images
# First we see if the object is stationary. If both change coordinates are 0,
# we're not moving and we can set the image to the 0 element in the array
if self.change_x == 0 and self.change_y == 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]
# The rest is just repeated code changing only the direction the player
# object is moving. In this case < 0 means we're moving left
if self.change_x < 0:
# set facing to 3 or left
self.facing = 3
# if imgvar is 0, we just started moving
if self.imgvar == 0:
# set image to the first element
self.image = self.left[0]
self.imgvar +=1
# if imgvar is anything else, we've been moving
else:
# make sure imgvar isn't greater than the length of the image array
if self.imgvar > len(self.left)-1:
self.imgvar = 0
# update imgvar and change the image
self.imgvar += 1
self.image = self.left[self.imgvar]
Okay, that's a lot to take in (that's what she said), if you're not accustomed to pygame. First you'll notice that my Player class implements a pygame parent class. This can be seen in the first line:
class Player(pygame.sprite.Sprite):
I utilize the sprite class for a number of reasons. Collision detection is one thing that sprites do very well. There is a complete list of sprite functions here: http://www.pygame.org/docs/ref/sprite.html
In any case, the important bits are the class attributes and functions. The class attributes to take note of are the change_x and y, as well as the facing and image array attributes.
change_x and change_y are used to move the object around the screen. They do this by utilizing the changespeed function. This function changes these values, which are later used in the update function to move the player. The first thing that update does is it moves the player rectangle to the new coordinates by adding the change_x and change_y values to the current x/y values. Again, I won't go into too much detail because there are plenty of better tutorials out there on moving objects.
Next we have the facing attribute. This is just an integer that keeps track of the directions your character is facing. With the setup shown above we only have animation images for 4 directions, but you could easily add 4 more for diagonal movements. You'll notice this also gets changed in the update function based on the change_x and change_y values. If change_x is a positive number we'd say facing is right, or whatever number we're using to designate moving right.
The image arrays (up,down,left, and right) are where we store the images we have to loop through to get our animation. The current image is stored under the (you guessed it) image variable. All we do to animate our character is move along to the next image in our array, setting it as the current image.
Within the update function, the comments kind of explain what's happening. We move our rectangle, then check the change_x and y variables to determine which way we're facing and which image from which array we need to be on. Just keep in mind that I only put in the left direction. You have to repeat that chunk of code to include the other directions.
Now you're saying, "This is all well and good, but how do we actually do anything with this class?" and "Why didn't you go into the other features of pygame, like the load.image function you used?" and "God this guy is retarded"
The short answer is 1)that you create an object within your game loop, use pygame events to utilize the changespeed function, and call the object's update function each loop. 2)I think there are much better explanations in the pygame documentation. 3)You're right, but there's nothing I can do about that.
I'll get more into how to use the sprite next week. We can plug it into the surface demo I did last week and we'll see how it works out.
-Newt
class Player(pygame.sprite.Sprite):
# initialize the player object
# requires x and y coordinates
def __init__(self,x,y):
# initialize the sprite (parent class)
pygame.sprite.Sprite.__init__(self)
# Set initial variables
#change_x and y are used to move the object around the screen
self.change_x=0
self.change_y=0
#facing keeps track of the direction
#this is important so we know which set of images to use
self.facing = 0
#here are the images we're going to loop over depending on the direction
#start by setting the image to the image to start with
self.image = pygame.image.load('hero.png')
self.down = [pygame.image.load('hero.png').convert_alpha(),
pygame.image.load('hero1.png').convert_alpha(),
pygame.image.load('hero2.png').convert_alpha(),
pygame.image.load('hero3.png').convert_alpha(),
pygame.image.load('hero2.png').convert_alpha(),
pygame.image.load('hero1.png').convert_alpha()]
self.left = [pygame.image.load('herol.png').convert_alpha(),
pygame.image.load('herol1.png').convert_alpha(),
pygame.image.load('herol2.png').convert_alpha(),
pygame.image.load('herol3.png').convert_alpha(),
pygame.image.load('herol2.png').convert_alpha(),
pygame.image.load('herol1.png').convert_alpha()]
self.right = [pygame.image.load('heror.png').convert_alpha(),
pygame.image.load('heror1.png').convert_alpha(),
pygame.image.load('heror2.png').convert_alpha(),
pygame.image.load('heror3.png').convert_alpha(),
pygame.image.load('heror2.png').convert_alpha(),
pygame.image.load('heror1.png').convert_alpha()]
self.up = [pygame.image.load('herou.png').convert_alpha(),
pygame.image.load('herou1.png').convert_alpha(),
pygame.image.load('herou2.png').convert_alpha(),
pygame.image.load('herou3.png').convert_alpha(),
pygame.image.load('herou2.png').convert_alpha(),
pygame.image.load('herou1.png').convert_alpha()]
# give the object a rectangle using image size
self.rect = self.image.get_rect()
# set the topleft location to the x and y coordinates
self.rect.topleft = [x,y]
# Used to move the object
def changespeed(self,x,y):
self.change_x+=x
self.change_y+=y
# This is where we update the images and location of the player object
def update(self):
# 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)
# This next bit is where we change images
# First we see if the object is stationary. If both change coordinates are 0,
# we're not moving and we can set the image to the 0 element in the array
if self.change_x == 0 and self.change_y == 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]
# The rest is just repeated code changing only the direction the player
# object is moving. In this case < 0 means we're moving left
if self.change_x < 0:
# set facing to 3 or left
self.facing = 3
# if imgvar is 0, we just started moving
if self.imgvar == 0:
# set image to the first element
self.image = self.left[0]
self.imgvar +=1
# if imgvar is anything else, we've been moving
else:
# make sure imgvar isn't greater than the length of the image array
if self.imgvar > len(self.left)-1:
self.imgvar = 0
# update imgvar and change the image
self.imgvar += 1
self.image = self.left[self.imgvar]
Okay, that's a lot to take in (that's what she said), if you're not accustomed to pygame. First you'll notice that my Player class implements a pygame parent class. This can be seen in the first line:
class Player(pygame.sprite.Sprite):
I utilize the sprite class for a number of reasons. Collision detection is one thing that sprites do very well. There is a complete list of sprite functions here: http://www.pygame.org/docs/ref/sprite.html
In any case, the important bits are the class attributes and functions. The class attributes to take note of are the change_x and y, as well as the facing and image array attributes.
change_x and change_y are used to move the object around the screen. They do this by utilizing the changespeed function. This function changes these values, which are later used in the update function to move the player. The first thing that update does is it moves the player rectangle to the new coordinates by adding the change_x and change_y values to the current x/y values. Again, I won't go into too much detail because there are plenty of better tutorials out there on moving objects.
Next we have the facing attribute. This is just an integer that keeps track of the directions your character is facing. With the setup shown above we only have animation images for 4 directions, but you could easily add 4 more for diagonal movements. You'll notice this also gets changed in the update function based on the change_x and change_y values. If change_x is a positive number we'd say facing is right, or whatever number we're using to designate moving right.
The image arrays (up,down,left, and right) are where we store the images we have to loop through to get our animation. The current image is stored under the (you guessed it) image variable. All we do to animate our character is move along to the next image in our array, setting it as the current image.
Within the update function, the comments kind of explain what's happening. We move our rectangle, then check the change_x and y variables to determine which way we're facing and which image from which array we need to be on. Just keep in mind that I only put in the left direction. You have to repeat that chunk of code to include the other directions.
Now you're saying, "This is all well and good, but how do we actually do anything with this class?" and "Why didn't you go into the other features of pygame, like the load.image function you used?" and "God this guy is retarded"
The short answer is 1)that you create an object within your game loop, use pygame events to utilize the changespeed function, and call the object's update function each loop. 2)I think there are much better explanations in the pygame documentation. 3)You're right, but there's nothing I can do about that.
I'll get more into how to use the sprite next week. We can plug it into the surface demo I did last week and we'll see how it works out.
-Newt
Wednesday, March 20, 2013
Games with Python
Over the last year and some change, I've been trying to teach myself to code. I started with Python for no real reason other than there were a number of tutorial sites out there that seemed to be pretty complete.
After going through the tutorials and Python documentation (several times in some cases) I found myself wondering how to get in some badly needed practice. After all, in order to code something you need to have some sort of use case in mind. I had found a number of forums offering up ideas for projects, but most were either way above my head or so simple I completed them in minutes.
Longer projects seemed to only hold my interest if there were some actual need for the functionality. Like parsing proxy logs or cmdb data at work. For everything else, I had trouble keeping focused till the completion of the code.
One day, by chance, I found this: http://inventwithpython.com/
At the time Albert Sweigart had only written the first book, but it was enough to renew my interest in learning the language. It's written so that even those with no working knowledge of programming can start creating games and learning python. It goes through a few command-line based games, into graphics based games teaching skills that can easily be translated into scripting and app development.
If you are interested in simple game development, or Python, I would definitely recommend reading his book either by download (for free) or purchasing a hardcopy . The second book is also now available and goes into even more detail with several new game types and development concepts.
I know this sounds like some sort of plug, but I am in no way being compensated for this endorsement. I am merely a grateful reader of his work.
-newt
After going through the tutorials and Python documentation (several times in some cases) I found myself wondering how to get in some badly needed practice. After all, in order to code something you need to have some sort of use case in mind. I had found a number of forums offering up ideas for projects, but most were either way above my head or so simple I completed them in minutes.
Longer projects seemed to only hold my interest if there were some actual need for the functionality. Like parsing proxy logs or cmdb data at work. For everything else, I had trouble keeping focused till the completion of the code.
One day, by chance, I found this: http://inventwithpython.com/
At the time Albert Sweigart had only written the first book, but it was enough to renew my interest in learning the language. It's written so that even those with no working knowledge of programming can start creating games and learning python. It goes through a few command-line based games, into graphics based games teaching skills that can easily be translated into scripting and app development.
If you are interested in simple game development, or Python, I would definitely recommend reading his book either by download (for free) or purchasing a hardcopy . The second book is also now available and goes into even more detail with several new game types and development concepts.
I know this sounds like some sort of plug, but I am in no way being compensated for this endorsement. I am merely a grateful reader of his work.
-newt
Monday, December 10, 2012
Hopefully Helpful
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
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
Subscribe to:
Posts (Atom)