|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import pygame |
| 4 | + |
| 5 | +# --------------------------------------------------------------------- |
| 6 | + |
| 7 | +BLACK = ( 0, 0, 0) |
| 8 | +WHITE = (255,255,255) |
| 9 | + |
| 10 | +WIDTH = 300 |
| 11 | +HEIGHT = 200 |
| 12 | + |
| 13 | +# --------------------------------------------------------------------- |
| 14 | + |
| 15 | +# --- init --- |
| 16 | + |
| 17 | +pygame.init() |
| 18 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 19 | +screen_rect = screen.get_rect() |
| 20 | + |
| 21 | +# --- images/frames --- |
| 22 | + |
| 23 | +spritesheet = pygame.image.load('spritesheet008.jpg') |
| 24 | + |
| 25 | +images = [] |
| 26 | +images.append( spritesheet.subsurface(pygame.Rect( 0,75,127,56)) ) |
| 27 | +images.append( spritesheet.subsurface(pygame.Rect(127,75,127,56)) ) |
| 28 | +images.append( spritesheet.subsurface(pygame.Rect(254,75,127,56)) ) |
| 29 | +images.append( spritesheet.subsurface(pygame.Rect(381,75,127,56)) ) |
| 30 | +images.append( spritesheet.subsurface(pygame.Rect( 0,206,127,56)) ) |
| 31 | +images.append( spritesheet.subsurface(pygame.Rect(127,206,127,56)) ) |
| 32 | + |
| 33 | +frames_number = len(images) |
| 34 | +current_frame = 0 |
| 35 | + |
| 36 | +frame_rect = images[0].get_rect(center=screen_rect.center) |
| 37 | +move = 0 |
| 38 | + |
| 39 | +# --- mainloop --- |
| 40 | + |
| 41 | +fps = pygame.time.Clock() |
| 42 | + |
| 43 | +running = True |
| 44 | + |
| 45 | +while running: |
| 46 | + |
| 47 | + # --- events --- |
| 48 | + |
| 49 | + for event in pygame.event.get(): |
| 50 | + |
| 51 | + # --- global events --- |
| 52 | + |
| 53 | + if event.type == pygame.QUIT: |
| 54 | + running = False |
| 55 | + elif event.type == pygame.KEYDOWN: |
| 56 | + if event.key == pygame.K_ESCAPE: |
| 57 | + running = False |
| 58 | + |
| 59 | + # --- player events --- |
| 60 | + |
| 61 | + if event.type == pygame.KEYDOWN: |
| 62 | + if event.key == pygame.K_RIGHT: |
| 63 | + move += 1 |
| 64 | + elif event.key == pygame.K_LEFT: |
| 65 | + move -= 1 |
| 66 | + elif event.type == pygame.KEYUP: |
| 67 | + if event.key == pygame.K_RIGHT: |
| 68 | + move -= 1 |
| 69 | + elif event.key == pygame.K_LEFT: |
| 70 | + move += 1 |
| 71 | + |
| 72 | + # --- updates --- |
| 73 | + |
| 74 | + current_frame = (current_frame + move) % frames_number |
| 75 | + frame_rect.x = (frame_rect.x + move*5) % WIDTH |
| 76 | + |
| 77 | + # --- draws --- |
| 78 | + |
| 79 | + screen.fill(WHITE) |
| 80 | + |
| 81 | + screen.blit(images[current_frame], frame_rect) |
| 82 | + |
| 83 | + pygame.display.flip() |
| 84 | + |
| 85 | + fps.tick(10) |
| 86 | + |
| 87 | +# --- the end --- |
| 88 | + |
| 89 | +pygame.quit() |
| 90 | + |
0 commit comments