Skip to content

Commit 744d0f6

Browse files
committed
spritesheet
1 parent 1f6249c commit 744d0f6

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

pygame/spritesheet/main.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+

pygame/spritesheet/spritesheet008.jpg

13.1 KB
Loading

tkinter/matplotlib-canvas/example-1/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
# create figure
4242
fig = matplotlib.pyplot.Figure()
4343

44-
# create matplotlib canvas using `fig` and assign to window `top`
44+
# create matplotlib canvas using `fig` and assign to widget `top`
4545
canvas = FigureCanvasTkAgg(fig, top)
46-
# get canvas as tkinter widget and put in window
46+
47+
# get canvas as tkinter widget and put in widget `top`
4748
canvas.get_tk_widget().pack()
4849

4950
# create toolbar

0 commit comments

Comments
 (0)