Question

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?)

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top