Skip to content

Commit c571f42

Browse files
committed
Improve
1 parent eba7479 commit c571f42

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

content/2024/11/24-making-particle-systems-for-fun-and-robotics/24-making-particle-systems-for-fun-and-robotics.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ I have a lifelong fascination with Particle systems, complementing my robotics.
88

99
Today I am going to express my love of particle systems in Python. This might be a long ride, with a few programs in a series of posts. I hope you'll join me, with some demonstrations in how much fun these can be. It would be helpful if you've done a little Python before, but this aims to be a beginner-friendly course.
1010

11-
This series will focus on programming with lots of visuals. We'll write it using PyGame letting us get a lot on the screen. Beyond being handy in robotics, this is fun for some simple visual effects, games and simulations.
11+
This series will focus on programming with lots of visuals. You'll write it using PyGame letting us get a lot on the screen. Beyond being handy in robotics, this is fun for some simple visual effects, games and simulations.
1212

1313
## Getting prepared
1414

@@ -24,7 +24,7 @@ pip install pygame
2424

2525
## One dot
2626

27-
The simplest particle system is one dot. We'll use this to get up and running. This is the least interesting particle system, but we can build on it to something more fun.
27+
The simplest particle system is one dot. You'll use this to get up and running. This is the least interesting particle system, but you can build on it to something more fun.
2828

2929
```python
3030
import pygame
@@ -63,27 +63,25 @@ Run it and you should see this:
6363

6464
![One dot on the screen](/2024/11/24-making-particle-systems-for-fun-and-robotics/one-dot.png)
6565

66-
We start with importing [PyGame](https://www.pygame.org/), a Python gaming library for drawing 2D games.
66+
The code starts with importing [PyGame](https://www.pygame.org/), a Python gaming library for drawing 2D games. It then sets up some parameters for your program. It defines the display size with `WIDTH` and `HEIGHT`, and uses `FRAME_RATE` so the program runs at a consistent speed.
6767

68-
We then set up some parameters for our program. We define the display size with `WIDTH` and `HEIGHT`. We use FRAME_RATE so the program runs at a consistent speed.
68+
You then set up some colours. A background colour, and a colour for your dot, followed by a dot size. putting these things in parameters makes them easy to change later.
6969

70-
We then set up some colours. A background colour, and a colour for our dot, followed by a dot size. putting these things in parameters makes them easy to change later.
71-
72-
We then define a list with two numbers, the x and y position of the dot. This is our particle, followed by a function to draw our particle, a circle on the screen. In PyGame, the top left corner is 0,0, so the bottom of the screen is HEIGHT.
70+
Your code then defines a list with two numbers, the x and y position of the dot. This is your particle, followed by a function to draw our particle, a circle on the screen. In PyGame, the top left corner is 0,0, so the bottom of the screen is HEIGHT.
7371

7472
![PyGame Coordinate System](/2024/11/24-making-particle-systems-for-fun-and-robotics/pygame-coordinates.png)
7573

76-
After this we initialise pygame, with a screen and a clock and enter a main loop. The main loop starts with a running variable, so the system can be told when to exit. The code looks for a QUIT event, triggered when you close the window to ensure it shuts down. This is a common pattern in PyGame.
74+
After this you initialise pygame, with a screen and a clock and enter a main loop. The main loop starts with a running variable, so the system can be told when to exit. The code looks for a QUIT event, triggered when you close the window to ensure it shuts down. This is a common pattern in PyGame.
7775

78-
The next part of the loop fills the screen with background colour. We then use the `draw` function to draw the dot. We must call `pygame.display.flip` as pygame draws everything on a hidden buffer, which we swap with the visible screen.
76+
The next part of the loop fills the screen with background colour. You then use the `draw` function to draw the dot. You must call `pygame.display.flip` as pygame draws everything on a hidden buffer, which you swap with the visible screen.
7977

80-
Finally we tick the clock to keep the framerate the same. The last line of the program is `pygame.quit` to ensure everything is cleaned up.
78+
Finally you tick the clock to keep the framerate the same. The last line of the program is `pygame.quit` to ensure everything is cleaned up.
8179

8280
This is drawn at 400, 400 which is the middle of the screen. I suggest you try a few values between 0 and the WIDTH to see how it changes. I guarantee you won't like my colour choices, so you can also try different colour names, or even RGB values (like `(255, 0, 0)` for red).
8381

8482
## Making it a bit random
8583

86-
A key concept in a particle system is randomness. We can make the one dot less boring by making it random.
84+
A key concept in a particle system is randomness. You can make the one dot less boring by making it random.
8785

8886
At the top of the file, lets import the random module above pygame:
8987

@@ -92,29 +90,29 @@ import random
9290
import pygame
9391
```
9492

95-
Now we can make it show the dot at a random place every time we run it. Update the one_dot line to be:
93+
Now you can make it show the dot at a random place every time you run it. Update the one_dot line to be:
9694

9795
```python
9896
one_dot = [random.randint(0, WIDTH), random.randint(0, HEIGHT)]
9997
```
10098

10199
## Movement
102100

103-
We have a particle, but particles have a lifecycle:
101+
You have a particle, but it's not doing much. You can add a little movement to our particle.
102+
103+
Particles have a lifecycle:
104104

105105
- They are created
106106
- They update and change over time
107107
- They may also die
108108

109-
We can add a little movement to our particle. Let's introduce a speed constant and update our particle with it.
110-
111-
In the constants add the following:
109+
Let's introduce a speed constant and update our particle with it. In the constants add the following:
112110

113111
```python
114112
SPEED = 2
115113
```
116114

117-
We can add an update function, which can be called in the main loop:
115+
You then add an `update` function, which can be called in the main loop:
118116

119117
```python
120118
def update():
@@ -123,9 +121,9 @@ def update():
123121
one_dot[1] = 0
124122
```
125123

126-
This will add the speed to the second element (the Y coordinate) of the one_dot list. If the dot reaches the bottom of the screen, we reset it to the top.
124+
This will add the speed to the second element (the Y coordinate) of the one_dot list. If the dot reaches the bottom of the screen, you reset it to the top.
127125

128-
We then call this in the main loop before we start drawing things:
126+
You then call `update` in the main loop before you start drawing things:
129127

130128
```python
131129
running = True
@@ -141,17 +139,19 @@ while running:
141139
clock.tick(FRAME_RATE)
142140
```
143141

144-
Run this. You can adjust the speed by changing the SPEED constant, or increasing the FRAME_RATE, however, note that above a certain frame rate value we may be being slowed by the speed of the program. With high SPEED values, you may see the dot jump on the screen.
142+
Run this. You can adjust the speed by changing the SPEED constant, or increasing the FRAME_RATE, however, note that above a certain frame rate value your frames may be being slowed by the speed of the program. With high SPEED values, you may see the dot jump on the screen.
145143

146144
This dot has a lifecycle:
147145

148146
- It is created at a random position
149147
- It moves down the screen
150148
- When it reaches the bottom, it is moved back to the top
151149

150+
What happens if you make the SPEED constant negative?
151+
152152
## Multiple dots
153153

154-
We can make this more interesting again by having multiple dots, like a rain storm. We can do this by having a list of dots.
154+
You can make this more interesting again by having multiple dots, like a rain storm. You can do this by having a list of dots.
155155

156156
Swap our one dot for this:
157157

@@ -170,7 +170,7 @@ def populate():
170170
)
171171
```
172172

173-
We then need to update the draw function to draw all the dots:
173+
You then need to update the draw function to draw all the dots:
174174

175175
```python
176176
def draw(surface):
@@ -188,7 +188,7 @@ def update():
188188
raindrop[1] = 0
189189
```
190190

191-
Finally, we need to call the populate function to create the dots, while we initialise the program:
191+
Finally, you need to call the populate function to create the dots while you initialise the program:
192192

193193
```python
194194
pygame.init()
@@ -209,11 +209,9 @@ These dot's all have the same lifecycle as our one dot!
209209

210210
## Adjusting the lifecycle
211211

212-
We can change this particle system in a few interesting ways. You might have noticed the raindrops loop around in a repeating pattern.
213-
214-
We can fix this by changing the lifecycle. Instead of just wrapping the raindrop, we can pretend this raindrop has reached the end of the lifecycle and that we are creating a new one. However, we can do something sneaky and reset the x to a random value when we do this.
212+
You can change this particle system in a few interesting ways. You might have noticed the raindrops loop around in a repeating pattern.
215213

216-
We only need to modify the `update` function:
214+
You can fix this by changing the lifecycle. Instead of just wrapping the raindrop, you can pretend this raindrop has reached the end of the lifecycle and that your are creating a new one. However, you can do something sneaky and reset the x to a random value when you do this. You only need to modify the `update` function:
217215

218216
```python
219217
def update():
@@ -228,16 +226,16 @@ You shouldn't be able to see a repeating pattern any more.
228226

229227
## Random speeds
230228

231-
We can add a little depth by adding a further parameter to our raindrops. For this we will change two parts of the lifecycle - the add function and the update function.
232-
We'll also adjust the parameters above.
229+
You can add a little depth by adding a further parameter to our raindrops. For this you will change two parts of the lifecycle - the add function and the update function.
230+
You'll also adjust the parameters above. Extend the constants after the POPULATION_SIZE:
233231

234232
```python
235233
POPULATION_SIZE = 200
236234
MIN_SPEED = 2
237235
MAX_SPEED = 6
238236
```
239237

240-
In the populate function, lets make a random speed:
238+
You can then modify the populate function to generate a random speed:
241239

242240
```python
243241
def populate():
@@ -251,7 +249,7 @@ def populate():
251249
)
252250
```
253251

254-
We can then update using this stored speed:
252+
You can then update using this stored speed:
255253

256254
```python
257255
def update():
@@ -262,7 +260,7 @@ def update():
262260
raindrop[0] = random.randint(0, WIDTH)
263261
```
264262

265-
However, we made an assumption in draw that the raindrop was only 2 numbers - the coordinates of the drop. With 3, we need to filter them:
263+
However, the code made an assumption in draw that the raindrop was only 2 numbers - the coordinates of the drop. With 3, you need to filter them:
266264

267265
```python
268266
def draw(surface):
@@ -274,7 +272,7 @@ If you run this, you can now see raindrops falling at different speeds.
274272

275273
## Checkpoint - raindrops
276274

277-
We've built a small particle system, transforming a single static dot into a rainstorm with raindrops at different speeds. Here's the full code:
275+
You've built a small particle system, transforming a single static dot into a rainstorm with raindrops at different speeds. Here's the full code:
278276

279277
```python
280278
import random
@@ -345,6 +343,6 @@ You've built a simple particle system, raindrops, using Python and PyGame. You'v
345343

346344
You've also seen how particles have a lifecycle.
347345

348-
Over the coming for posts, we can explore what other ways we can use particle systems, some variations on this theme, and some quite different.
346+
Over the coming for posts, we can explore what other ways you can use particle systems, some variations on this theme, and some quite different.
349347

350348
I've built this inspired by the Kingston University Coder Dojo where I mentor Python, and will have other particle systems inspired by research I've done for my books.

0 commit comments

Comments
 (0)