Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a dumb example which alernates between two first images of the spritesheet when you press left/right:</p> <pre><code>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() </code></pre> <p>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.</p> <p>To be more complete I should wait some time before updating, e.g. control that I update only 60 times per second.</p> <p>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 <code>update()</code> and <code>draw()</code> which would automatically do the work of selecting the next frame, blitting the sprite and so on.</p> <p>Pygame seems to provide a base class for that purpose: <a href="http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite" rel="nofollow noreferrer">link text</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload