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