Game of Life
- 9 Devlogs
- 9 Total hours
Conway's Game of Life implementation in Pygame
Conway's Game of Life implementation in Pygame
Just added a new thing to allow shift+left clicking, because dragging right click wouldn’t work on my laptop. This kind of took a while to implement because Pygame is weird, but I got it and now it works on laptops too! I don’t know if this is specific to only mine, but it’s also just easier to play the game on a laptop now. Also, I figured out how to package it into an executable. Pyinstaller is a pain to work with, and it took me a while too, but I got it. At first it was 50 megabytes but I got it to ignore some of the libraries from Pygame so now it’s 15-25, which seems to be normal.
I think this is the final devlog! I added a (relatively) small message to show the keybinds. It’s on by default, for new users. I also made the text bigger and changed the statusline from “updates/sec” to “gens./sec.” (abbreviated).
Ok… The scope gets even bigger. I did A LOT of work this time, in very small but meaningful changes. Tl;dr: I improved the code, made the project look nicer, changed some of the keybinds, and made it much better to use.
Board class (board.py), one for the themes (themes.py), and then the main.py file stores the main while loop.s key toggles the statusline.n key runs the simulation for one generation.c key clears the board (did I mention that before?).+ and - keys have been unbound. I felt they weren’t that necessary.I’ve been telling myself that I am close to finishing the project, but I keep adding new things. I’m sure I’ll find a new feature to add.
Quite a bit done here, but a lot of progress, and I think I’m almost ready to ship.
g key can be pressed to toggle the grid (forgot to show it in the video, unfortunately).t key can be pressed to change the theme.Board.draw() method was already flexible, this was very straightforward. First, I got ChatGPT to generate a list of themes (nothing more). Then, I made a new variable to keep track of the current theme index. That variable gets increased when the t key is pressed, and then the keys of the theme are bassed to the Board.draw() method.To conclude, I think I am very close to finally shipping this small project! I will continue to make some more small changes and improvements.
This was fairly straightforward. I added a bunch of quality of life features for the simulation.
False as the default board.clock.tick() at the end of the main while loop.
False by default, and then I am checking if it’s True later.Next I am going to add a small menu that will display important information like the number of the generation, as well as the presets. I want it to be hideable so as to not block it, but I also want the board to be the whole screen, so it’s going to be a small window. Then, maybe, tapping into the flexibility of my custom draw() function and adding preset colorschemes?
P.S.: Here is a better recording that showcases the simulation… The last post didn’t do it very much justice…
I got the rules working! This was probably the hardest part yet, honestly.
Overall, I think the project is turning out nicely. Next, I will implement a pause feature so that I can draw stuff and see how it plays out instead of hardcoding it for testing.
I implemented clicking and dragging! This was somewhat more complicated than I thought it would be, but not hard in retrospect.
The clicking itself was quite easy. It was just a matter of listening for the click event, and then I implemented a function to find the position of the cell under the mouse position and toggle it.
The dragging was a bit harder, though. When I first got it working (shown in the second video), it was choppy. The main issues that I saw were:
Interestingly, the last one only seemed to happen when I was dragging my cursor and then moved the mouse out of the window but only on the right side.
I ended up solving the issue by adding some checks and making dragging only make the cells alive. This resulted in the first video, which is much cleaner.
I like how this is turning out!
I changed it to use a main class for the whole board.
The class contains a 2-dimensional array of booleans. This lets me access a specific cell from a coordinate like pygame’s coordinates, where x goes right and y goes down, as shown in the picture attached.
I also changed the draw function to be much more versatile. First, I am now using the rectangle function, which will let me color specific cells much easier. Also, I have some future customizability options with the colors of the borders, as well as either state of the cell.
This is my first time using pygame. I was able to get a very basic window with a grid. For the grid, I used a simple for loop that draws a horizontal line and a vertical line.