Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Intro to Python: Learn while developing a pac-man game
Highest Rated
Rating: 4.9 out of 5(49 ratings)
1,986 students

Intro to Python: Learn while developing a pac-man game

Get more fluent in Python by coding a simple game
Last updated 3/2023
English

What you'll learn

  • Get more fluent with Python through a series of coding lectures
  • Understand and use the main language constructors, such as ifs, for and while loops
  • Develop and manipulate more complex data structures to support the game
  • Learn the basics of automated testing
  • Understand how a professional programmers thinks when programming

Course content

2 sections17 lectures2h 33m total length
  • An introduction to Python 102!2:15
    • 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 decision: the game map10:39

    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.

  • Finding the pacman in the map8:08

    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.

  • Our first automated test6:18

    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?

  • Moving the pacman to another position16:35

    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?

  • Our first interface with the user6:50

    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!

  • Move the pacman based on the keys of the keyboard11:12

    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.

  • Checking for illegal moves, part 111:35

    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!

  • Checking for illegal moves, part 28:31

    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!

  • Did we win the game?7:13

    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?

  • The main loop, part 14:47

    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?

  • The main loop, part 25:12

    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!

  • Moving the ghosts, part 117:45

    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!

  • Moving the ghosts, part 28:46

    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?

  • Improving the user interface a bit10:28

    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.

Requirements

  • Basic knowledge of Python.

Description

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!

Who this course is for:

  • Beginner Python developers that want to get more fluent with programming