Skip to content

Commit 7cdb613

Browse files
committed
update sphinx and image doc
1 parent df8dfa1 commit 7cdb613

File tree

14 files changed

+328
-250
lines changed

14 files changed

+328
-250
lines changed

docs/1_intro/intro.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,6 @@ that does a bouncing ball animation::
291291

292292
.. image:: intro6.png
293293

294+
:download:`ball.gif<ball.gif>`
295+
294296
Try to understand what the program does. Then try to modify it's parameters.

docs/3_image/bird.png

11.5 KB
Loading

docs/3_image/image.rst

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ images. The method ``load()`` loads an image from the file system
99
and returns a Surface object. The method ``convert()`` optimizes the
1010
image format and makes drawing faster::
1111

12-
img = pygame.image.load('../animals/bird-icon.png')
12+
img = pygame.image.load('bird.png')
1313
img.convert()
1414

15+
Download the image ``bird.png`` to the same folder where your program resides:
16+
17+
:download:`bird.png<bird.png>`
18+
1519
The method ``get_rect()`` returns a Rect object from an image.
1620
At this point only the size is set and position is placed at (0, 0).
1721
We set the center of the Rect to the center of the screen::
@@ -37,8 +41,7 @@ Move the image with the mouse
3741
-----------------------------
3842

3943
At the beginning of the programm we set a boolean variable ``moving`` to False.
40-
When the mouse button is pressed, and if the mouse position is within the boundaries
41-
of the image we set it to True::
44+
Only when the mouse button is pressed, and when the mouse position is within the image (collidepoint) we set it to True::
4245

4346
elif event.type == MOUSEBUTTONDOWN:
4447
if rect.collidepoint(event.pos):
@@ -50,7 +53,7 @@ When the mouse button is released, we set it to False again::
5053
moving = False
5154

5255
When the mouse moves, and the flag ``moving`` is True, then we move the image
53-
by the amount of relative movement::
56+
by the amount of relative movement (event.rel)::
5457

5558
elif event.type == MOUSEMOTION and moving:
5659
rect.move_ip(event.rel)
@@ -65,17 +68,33 @@ This is the whole code:
6568
Rotate and Scale the image
6669
--------------------------
6770

68-
The ``pygame.transform`` module provides methods for scaling, rotating and
69-
flipping images.
71+
The ``pygame.transform`` module provides methods for **scaling, rotating and flipping**
72+
images. As we are going to modify the image **img** we keep the original image
73+
in a variable called **img0**::
74+
75+
img0 = pygame.image.load(path)
76+
img0.convert()
7077

71-
First we define scale and angle::
78+
In order to show the image rectangle, we add a green border to the original image::
79+
80+
rect0 = img0.get_rect()
81+
pygame.draw.rect(img0, GREEN, rect0, 1)
82+
83+
Then we place the place the image in the center of the screen::
84+
85+
center = w//2, h//2
86+
img = img0
87+
rect = img.get_rect()
88+
rect.center = center
89+
90+
First we define the global variables **scale** and **angle**::
7291

7392
angle = 0
7493
scale = 1
7594

7695
We use the R key to increment rotation by 10 degrees and
7796
(decrement if the SHIFT key is pressed). The function ``rotozoom()`` allows to combine
78-
rotation and scaling. We always transform the original image. Repeated rotation or scaling of
97+
rotation and scaling. We always transform the orignial image (img0). Repeated rotation or scaling of
7998
an image would degrade its quality::
8099

81100
if event.type == KEYDOWN:
@@ -84,7 +103,7 @@ an image would degrade its quality::
84103
angle -= 10
85104
else:
86105
angle += 10
87-
img = pygame.transform.rotozoom(original, angle, scale)
106+
img = pygame.transform.rotozoom(img0, angle, scale)
88107

89108
We use the S key to increment the scale by 10% (decrease if the SHIFT key
90109
is pressed)::
@@ -94,7 +113,7 @@ is pressed)::
94113
scale /= 1.1
95114
else:
96115
scale *= 1.1
97-
img = pygame.transform.rotozoom(original, angle, scale)
116+
img = pygame.transform.rotozoom(img0, angle, scale)
98117

99118
As the image is transformed the bounding rectangle changes size. It must be
100119
recalulated and placed at the center again::
@@ -109,7 +128,7 @@ Reset the image to the original
109128
We use the O key to reset the image to its original state::
110129

111130
elif event.key == K_o:
112-
img = original
131+
img = img0
113132
angle = 0
114133
scale = 1
115134

@@ -151,7 +170,11 @@ At the beginning we import the ``math`` module::
151170

152171
import math
153172

154-
We store the mouse position as ``mouse`` and calculate the **x, y** coordinates
173+
At the beginning we store the initial mouse position::
174+
175+
mouse = pygame.mouse.get_pos()
176+
177+
When the mouse moves we update the mouse position ``mouse`` and calculate the **x, y** coordinates
155178
from the center of the image.
156179
We also calculate the center-mouse distance **d** ::
157180

@@ -167,7 +190,7 @@ scale argument::
167190

168191
angle = math.degrees(-math.atan2(y, x))
169192
scale = abs(5 * d / w)
170-
img = pygame.transform.rotozoom(original, angle, scale)
193+
img = pygame.transform.rotozoom(img0, angle, scale)
171194
rect = img.get_rect()
172195
rect.center = center
173196

docs/3_image/image1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
screen = pygame.display.set_mode((w, h))
1212
running = True
1313

14-
img = pygame.image.load('animals/bird-icon.png')
14+
img = pygame.image.load('bird.png')
1515
img.convert()
1616
rect = img.get_rect()
1717
rect.center = w//2, h//2

docs/3_image/image2.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@
1515

1616
module = sys.modules['__main__']
1717
path, name = os.path.split(module.__file__)
18-
path = os.path.join(path, '../animals/bird-icon.png')
18+
path = os.path.join(path, 'bird.png')
1919

20-
original = pygame.image.load(path)
21-
original.convert()
20+
img0 = pygame.image.load(path)
21+
img0.convert()
2222

23-
rect0 = original.get_rect()
24-
pygame.draw.rect(original, GREEN, rect0, 1)
23+
# draw a green border around img0
24+
rect0 = img0.get_rect()
25+
pygame.draw.rect(img0, GREEN, rect0, 1)
2526

26-
img = original
2727
center = w//2, h//2
28-
mouse = center
29-
28+
img = img0
3029
rect = img.get_rect()
3130
rect.center = center
3231

3332
angle = 0
3433
scale = 1
3534

35+
mouse = pygame.mouse.get_pos()
36+
3637
while running:
3738
for event in pygame.event.get():
3839
if event.type == QUIT:
@@ -44,17 +45,17 @@
4445
angle -= 10
4546
else:
4647
angle += 10
47-
img = pygame.transform.rotozoom(original, angle, scale)
48+
img = pygame.transform.rotozoom(img0, angle, scale)
4849

4950
elif event.key == K_s:
5051
if event.mod & KMOD_SHIFT:
5152
scale /= 1.1
5253
else:
5354
scale *= 1.1
54-
img = pygame.transform.rotozoom(original, angle, scale)
55+
img = pygame.transform.rotozoom(img0, angle, scale)
5556

5657
elif event.key == K_o:
57-
img = original
58+
img = img0
5859
angle = 0
5960
scale = 1
6061

@@ -81,7 +82,7 @@
8182

8283
angle = math.degrees(-math.atan2(y, x))
8384
scale = abs(5 * d / w)
84-
img = pygame.transform.rotozoom(original, angle, scale)
85+
img = pygame.transform.rotozoom(img0, angle, scale)
8586
rect = img.get_rect()
8687
rect.center = center
8788

0 commit comments

Comments
 (0)