I propose to introduce Python classes to implement the Game Loop pattern. Using these tools, I show how to refactor the code in the previous post to get a more robust and readable program.
This post is part of the Discover Python and Patterns series
Create a class with PythonIn this post, I introduc…
Thanks to the Pygame library we installed in the previous post, we can draw 2D graphics. In this post, I propose to introduce controls with the keyboard as well as some improvements like window centering and frame rate handling.
This post is part of the Discover Python and Patterns series
Keyboard e…In the previous posts, I introduced enough of Python and patterns to start creating games with graphics using the Pygame library.
This post is part of the Discover Python and Patterns series
Install PygamePygame is a popular library for creating 2D games. Like the random library we used before, the …
The time has come to see our first design pattern: the Game Loop Pattern! This pattern can give us many good ideas to refactor our game in a very effective way.
This post is part of the Discover Python and Patterns series
The Game Loop patternThere is several version of the Game Loop pattern. Here I…
It is now time to organize/refactor our code! When you begin, this is a strange process since the final code does the same as before. However, refactoring is the only way to create a code easy to maintain and expand.
This post is part of the Discover Python and Patterns series
Define a functionIn th…
In the previous post, the magic number is always the same. I propose now to introduce random numbers to change the magic number every time we launch the game. I also present imports and more on integers.
This post is part of the Discover Python and Patterns series
Library importIn the previous progr…
In previous posts, I only used strings and booleans. In this one, I use a new data type called “int” (for integer) that allows the storage and computation of natural numbers (numbers without a decimal part). I use them to create the famous “guess the number” game!
This post is part of the Discover P…
The previous game only allows one try: you have to restart it to propose another word. In this post, I introduce loops, and I use them to repeat the game until the player finds the magic word.
This post is part of the Discover Python and Patterns series
While statementThe while statement allows to r…
Programs in the previous post always lead to the same result. Here, I show you the basics of branching, or how to change the flow of a program depending on cases.
If statementIf we want to display a message when the player types a specific word, we can use the following syntax:
word = input("Enter t…
In the first post, we saw how to install an IDE and how to display a message; it is time to add some interaction with the player.
Wait for a keyCreate a new python program and type the following line:
input("Press Enter to continue...")
If you run the program, you get the fol…