@@ -9,9 +9,13 @@ images. The method ``load()`` loads an image from the file system
99and returns a Surface object. The method ``convert() `` optimizes the
1010image 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+
1519The method ``get_rect() `` returns a Rect object from an image.
1620At this point only the size is set and position is placed at (0, 0).
1721We set the center of the Rect to the center of the screen::
@@ -37,8 +41,7 @@ Move the image with the mouse
3741-----------------------------
3842
3943At 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
5255When 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:
6568Rotate 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
7695We 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
7998an 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
89108We use the S key to increment the scale by 10% (decrease if the SHIFT key
90109is 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
99118As the image is transformed the bounding rectangle changes size. It must be
100119recalulated and placed at the center again::
@@ -109,7 +128,7 @@ Reset the image to the original
109128We 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
155178from the center of the image.
156179We 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
0 commit comments