views:

1196

answers:

4

I've just started learning how to use pygame yesterday. I was read this one book that was super helpful and followed all its tutorials and examples and stuff. I wanted to try making a really simple side scroller/platforming game but the book sorta jumped pretty fast into 3D modeling with out instructing how to make changing sprites for movement of up down left and right and how to cycle through animating images.

I've spent all today trying to get a sprite to display and be able to move around with up down left and right. But because of the simple script it uses a static image and refuses to change.

Can anyone give me some knowledge on how to change the sprites. Or send me to a tutorial that does?

Every reference and person experimenting with it ha always been using generated shapes so I'm never able to work with them.

Any help is very appreciated.

Added: before figuring out how to place complex animations in my scene I'd like to know how I can make my 'player' change to unmoving images in regards to my pressing up down left or right. maybe diagonal if people know its something really complicated.

Add: This is what I've put together so far. http://animania1.ca/ShowFriends/dev/dirmove.rar would there be a possibility of making the direction/action set the column of the action and have the little column setting code also make it cycle down in a loop to do the animation? (or would that be a gross miss use of efficiency?)

A: 

It's been awhile since I used pygame, but I had some luck with this tutorial:

Hulk want animation!

and this class:

animsprite.py

Out of curiosity, which book are you using? I eventually gave up due to lack of time and good materials.

Chris Doggett
Thats sorta funny. I found that hulk experiment thing too. I tried to make it used up, down, left, right to move but I never got it to.Book: McGugan - Beginning Game Development with Python and PygameI made this with it: http://animania1.ca/ShowFriends/antgame.rarimages included
RamonLion
A: 

I don't know much about Pygame, but I've used SDL (on which Pygame is based).

If you use Surface.blit(): link text

You can use the optional area argument to select which part of the surface to draw. So if you put all the images that are part of the animation inside a single file, you can select which image will be drawn. It's called "clipping".

I guess you will have a game loop that will update the game state (changing the current image of the sprite if necessary), then draw the sprites using their state.

Bastien Léonard
so you're saying I'd use 1 image with the entire sprite sheet and have it blit one sprite section over every tick? How would the code look like if the sprite is 150p tall and 100px wide?
RamonLion
+1  A: 

Here is a dumb example which alernates between two first images of the spritesheet when you press left/right:

import pygame

quit = False
pygame.init()
display = pygame.display.set_mode((640,480))
sprite_sheet = pygame.image.load('sprite.bmp').convert()

# by default, display the first sprite
image_number = 0

while quit == False:
    event = pygame.event.poll()
    no_more_events = True if event == pygame.NOEVENT else False

    # handle events (update game state)
    while no_more_events == False:
        if event.type == pygame.QUIT:
            quit = True
            break
        elif event.type == pygame.NOEVENT:
            no_more_events = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                image_number = 0
            elif event.key == pygame.K_RIGHT:
                image_number = 1

        event = pygame.event.poll()

    if quit == False:
        # redraw the screen
        display.fill(pygame.Color('white'))
        area = pygame.Rect(image_number * 100, 0, 100, 150)
        display.blit(sprite_sheet, (0,0), area)
        pygame.display.flip()

I've never really used Pygame before so maybe this code shoudln't really be taken as an example. I hope it shows the basics though.

To be more complete I should wait some time before updating, e.g. control that I update only 60 times per second.

It would also be handy to write a sprite class which would simplify your work. You would pass the size of a sprite frame in the constructor, and you'd have methodes like update() and draw() which would automatically do the work of selecting the next frame, blitting the sprite and so on.

Pygame seems to provide a base class for that purpose: link text.

Bastien Léonard
fantastic. that worked perfectly. now. I was thinking. if that were the case. Would I have it so it basically runs as a grid type system. left and right on the sheet is the specific action and then it loops a certain amount of frames down? I'd manually set the amount of frames down per action.
RamonLion
Sorry but I didn't understand your question.
Bastien Léonard
well the code just says that every change in the number is how ever many sprite spaces over. and so in my code (linked above) I have it for running and action frames. but they aren't animated. would there be a way to have another code cycle downward after the sprite number has been set. ...
RamonLion
in order to make an animation of that one action that was put. For example. lets say '1' is run left. and the animation is a loop of 4 frames. so after '1' is set it cycles through 3 sprites down and back to the first until another column is designated.
RamonLion
A: 

dude the only thing you have to do is offcourse

import pygame and all the other stuff needed type code and stuff..........then when it comes to you making a spri

class .your class nam here. (pygame.sprite.Sprite): def init(self): pygame.sprite.Sprite.init(self) self.image=pygame.image.load(your image and path) self.rect=self.image.get_rect() x=0 y=0

      any thing else is what you want like posistion and other variables
 def update(self):
      self.rect.move_ip((x,y))

and thats it!!!! but thats not the end. if you do this you will ony have made the sprite to move it you need

aj