Did you just learn how to program in Python and needs to practice a bit more? Are you looking for some mildly complex project to challenge your skills? In this Python 102 course, we'll implement a text-based pac-game together, from scratch. Throughout the videos, we'll go together through all the challenges of implementing a game (how to design the map, how to move the characters, how to understand that the game is over) while exercising all the basics of the Python language.
This entire course is basically a live coding session. You will see:
Did you just learn how to program in Python and needs to practice a bit more? Are you looking for some mildly complex project to challenge your skills? In this Python 102 course, we'll implement a text-based pac-game together, from scratch. Throughout the videos, we'll go together through all the challenges of implementing a game (how to design the map, how to move the characters, how to understand that the game is over) while exercising all the basics of the Python language.
This entire course is basically a live coding session. You will see:
How to create simple data structures to store things like the map and game points
How to write complex ifs to take the different game decisions
How to properly use loops, e.g., to control the main game loop
How to get data from the keyboard and react to it
How to test your program using basic automated unit testing
How to make mistakes, understand why your program is failing, and how to learn from the mistakes
I speak out loud every step I take, including my mistakes. In the end of every video, you are asked to implement the pac-man yourself. Did you get lost? No worries, you can find the source code of the game at every point in time in the resources of the course.
If you are looking for a nice way to reinforce your recently acquired Python skills, this is the course for you.
Get your IDE ready! If you want to use Pycharm like I do, just download it at JetBrains website.
Before starting to write any code, reflect with yourself:
How would you implement a Pac-Man game?
What are the most important features of a Pac-Man game to you?
How would you represent the map where the pac-man and ghosts and all the characters walk in?
Our first step in the code will be to model the map of the game.
WHAT TO DO AFTER THE LECTURE?
Create an empty project, add a pacman.py file and write up a map like I did.
Think about what would be the first function you would implement.
The first function we implement finds the coordinates of the pacman in a given map.
WHAT TO DO AFTER THE LECTURE?
Implement the find_pacman() function. This function receives a map (a data structure that follows the idea we discussed in the previous video) and returns x, y, the two coordinates pointing to where the pacman is. The function should return -1,-1 if the pacman is not in the map.
Try the function out a bit. Put the pacman in different positions and check if the function always returns the correct coordinates.
Let us now properly test the find_pacman() function we just created.
WHAT TO DO AFTER THE LECTURE?
It is time to write your first test using the unittest library. Create a pacman_tests.py file and write your first set of tests.
Go beyond the tests I wrote. What else would you test to make sure things work? Can you think of boundary tests or corner case scenarios that we may want to exercise?
Our next step is to actually move the pacman from one position to another.
WHAT TO DO AFTER THE LECTURE?
Implement the move_pacman() function. Feel free to go for a different implementation! As long as the goal of the function is the same (i.e., move the pacman from one coordinate to another), we should be fine!
Can you remove the redudancy that we have now in the test code? How can we avoid the map variable being copied so many times there?
Let's show the pacman game for the first time to the player. It's a very simplistic map, but that's how we start!
WHAT TO DO AFTER THE LECTURE?
Implement the ui_print() function. I suggest you start simple like I did, because we will make it better later!
We now move the pacman according to the key that the user pressed.
WHAT TO DO AFTER THE LECTURE?
Implement the play() and the next_position functions. If you want to try Pycharm’s refactoring options, do like me: code everything in the play() method, and then, call the Extract Method refactoring.
Write automated tests for the next_position function. I can see at least 5 tests: one for each direction, and one for a random key.
Moving is not that simple. The pacman should only move if a valid key was pressed or if the new position is within the borders of the map. Let's check that.
WHAT TO DO AFTER THE LECTURE:
Implement the illegal moves and the within borders check.
Write automated tests for the within_borders function. For this one, there are lots of test cases to think: one for each border of the map, for example. Also, remember that bugs often happen in corner cases! The pacman should be able to go to the left most position of the map (e.g., column “0”) but not more than that!
We now check if we hit a wall or if we hit a ghost. The play() function is getting more and more complicated.
WHAT TO DO AFTER THE LECTURE?
Implement the checks for wall and ghost.
As always, write tests!
The pacman wins the game if it collects all the pills. Let's find a way to count all the pills in the map.
WHAT TO DO AFTER THE LECTURE?
Implement the total_pills() function and plug it in the play() function. As always, write unit tests for the total_pills(). Two test cases should be enough: a map with pills, a map with zero pills.
The play() function returns three booleans now… It is getting complicated. Can you see a way of simplifying that?
It is time for the main loop of the game: we get the key from the user, we move the pacman, we repeat!
WHAT TO DO AFTER THE LECTURE?
Create the program.py file and write the first main loop there.
At the end of this video, the program.py file has a line of code that should not be there… It belongs to another one of our Python files. Can you identify what it is?
We now let the user know when s/he won or lost the game. The complex return of the play() function starts to pay off.
WHAT TO DO AFTER THE LECTURE?
Improve the main loop in the program.py.
Move what belongs to the UI module to the UI module! As a rule of thumb, make sure you always separate user interface from the rest!
The ghosts should move randomly in the map. How do we do it? We first find all the ghosts in the map, and then, randomly select a direction for them. In this video, you also see that copying and pasting code is really prone to errors!
WHAT TO DO AFTER THE LECTURE?
Create the move_ghosts() and the find_ghosts() functions.
Can you think of a way to extract the “ugly code” that moves the pac-man and the ghost to a single place? We don’t want that ugly code spread in many places!
The ghosts should also respect the rules of the game: they should not go through walls or eat the pills. Let us add those checks.
WHAT TO DO AFTER THE LECTURE?
Improve the move_ghosts() function to make sure it checks for all the required conditions before moving a ghost.
The so many is_a_X() functions are very similar to each other. Can you think of a way to reuse some code there?
Our UI is very simplistic. Let's make use of some ASCII art and make the UI a little bit more attractive.
WHAT TO DO AFTER THE LECTURE?
Implement the new UI. As an exceptional case, feel free to copy the code from my sources as typing the ASCII art drawings might be too boring.
Can you think of other things to improve in the UI? I sure can! For example, if you win the game, the program prints the message but does not update the map. Print the map again once the game is over.
It seems we are done! Let's revisit the code and discuss about our main coding decisions one more time.
WHAT TO DO AFTER THE LECTURE?
What would you do differently?
You can be bold and implement more features, such as:
A new pill appears in a random position of the map every 5 moves.
A new type of pill that enables pacman to eat ghosts for the next 10 moves.
Points and a leaderboard
Read the map from a file
A menu where the user can choose which map to play (the list of all the available maps can come from a directory)
I show you a repository where you can see different implementations of the pacman game, even using some object-oriented programming!
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.