- Saved searches
- Use saved searches to filter your results more quickly
- lylant/PacMan-Pygame
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Make Pacman Game In Python With Code
- Making Pacman Game Using Python
- 1. Install and setup python
- 2. Install freegames python library
- 3. Python code for pacman game
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
a Pac-Man style game in Python
lylant/PacMan-Pygame
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This project aims to practice building a Pac-Man style game using Python.
- The Essencial: to make Pac-Man eat all available pellets, while avoiding collisions with ghosts chasing him.
- Ghosts: Ghosts always start at their home base. All ghosts move toward random direction.
- Game Levels: All levels will be generated based on the map data from the resource files.
Main Tech-Stacks for the Project
Following packages are required to install the game. Please prepare following packages installed on your machine before the installation. Most recent version is recommended. Tkinter and Pygame can be installed using pip .
As the product is not using any game framework or engine, the installation process is simple copy and paste.
The map generator will read a map file in /resource directory. The file name should be level#.txt or level##.txt , ## refers to the number of the level. The file contains 32×32 characters, each character indicates a block/item in the game field.
- _ passage (empty)
- # wall
- $ ghost spawn point
- . pellet
- @ Pac-Man (starting point)
- & free ghost
- % caged ghost
############################ #. ##. # #.####.#####.##.#####.####.# #.####.#####.##.#####.####.# #.####.#####.##.#####.####.# #. # #.####.##.########.##.####.# #.####.##.########.##.####.# #. ##. ##. ##. # ######.#####_##_#####.###### ######.#####_##_#####.###### ######.##____&_____##.###### ######.##_###_####_##.###### ######.##_###_####_##.###### ______.___###$####___.______ ######.##_###%####_##.###### ######.##_########_##.###### ######.##__________##.###### ######.##_########_##.###### ######.##_########_##.###### #. ##. # #.####.#####.##.#####.####.# #.####.#####.##.#####.####.# #. ##. @. ##. # ###.##.##.########.##.##.### ###.##.##.########.##.##.### #. ##. ##. ##. # #.##########.##.##########.# #.##########.##.##########.# #. # ############################ ############################
Make Pacman Game In Python With Code
Last updated June 8, 2023 by Jarvis Silva Today in this tutorial we will make pacman game in python, pacman is one of the most popular games, I used to play this game when I was a kid, It is very simple game you can still play this game if you have Google play games it comes preinstalled on it. If you don’t have much knowledge on python game development then it is going to be difficult for you but don’t worry I will provide you with the python code and explain you how the code works.
Making Pacman Game Using Python
To create this pacman game in python, I will use the freegames python library. It has a collection of free python games, you can use this module to create games in python. Now let’s see step by step how to make pacman game in python. I will provide you with the python code for pacman game, so read till the end.
1. Install and setup python
If you have python installed, skip this. The first step is to install python, so go to the official python website and download the latest python version. After download start the python installer and complete the setup it will install python on your computer.
2. Install freegames python library
Now you need to install freegames python library first create a new folder for this project and open a command prompt in the project location and paste the below command
It will install the freegames python module in your project, so now create a python file and go to the next step
3. Python code for pacman game
from random import choice from turtle import * from freegames import floor, vector state = path = Turtle(visible=False) writer = Turtle(visible=False) aim = vector(5, 0) pacman = vector(-40, -80) ghosts = [ [vector(-180, 160), vector(5, 0)], [vector(-180, -160), vector(0, 5)], [vector(100, 160), vector(0, -5)], [vector(100, -160), vector(-5, 0)], ] # fmt: off tiles = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,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on def square(x, y): """Draw square using path at (x, y).""" path.up() path.goto(x, y) path.down() path.begin_fill() for count in range(4): path.forward(20) path.left(90) path.end_fill() def offset(point): """Return offset of point in tiles.""" x = (floor(point.x, 20) + 200) / 20 y = (180 - floor(point.y, 20)) / 20 index = int(x + y * 20) return index def valid(point): """Return True if point is valid in tiles.""" index = offset(point) if tiles[index] == 0: return False index = offset(point + 19) if tiles[index] == 0: return False return point.x % 20 == 0 or point.y % 20 == 0 def world(): """Draw world using path.""" bgcolor('black') path.color('blue') for index in range(len(tiles)): tile = tiles[index] if tile > 0: x = (index % 20) * 20 - 200 y = 180 - (index // 20) * 20 square(x, y) if tile == 1: path.up() path.goto(x + 10, y + 10) path.dot(2, 'white') def move(): """Move pacman and all ghosts.""" writer.undo() writer.write(state['score']) clear() if valid(pacman + aim): pacman.move(aim) index = offset(pacman) if tiles[index] == 1: tiles[index] = 2 state['score'] += 1 x = (index % 20) * 20 - 200 y = 180 - (index // 20) * 20 square(x, y) up() goto(pacman.x + 10, pacman.y + 10) dot(20, 'yellow') for point, course in ghosts: if valid(point + course): point.move(course) else: options = [ vector(5, 0), vector(-5, 0), vector(0, 5), vector(0, -5), ] plan = choice(options) course.x = plan.x course.y = plan.y up() goto(point.x + 10, point.y + 10) dot(20, 'red') update() for point, course in ghosts: if abs(pacman - point) < 20: return ontimer(move, 100) def change(x, y): """Change pacman aim if valid.""" if valid(pacman + vector(x, y)): aim.x = x aim.y = y setup(420, 420, 370, 0) hideturtle() tracer(False) writer.goto(160, 160) writer.color('white') writer.write(state['score']) listen() onkey(lambda: change(5, 0), 'Right') onkey(lambda: change(-5, 0), 'Left') onkey(lambda: change(0, 5), 'Up') onkey(lambda: change(0, -5), 'Down') world() move() done()
- The code starts by importing necessary modules, including the choice function from the random module and the Turtle class from the turtle module.
- It defines a dictionary named state to keep track of the game score and initializes it with a score of 0.
- The game area is represented by a grid of tiles. The tiles list stores the configuration of the grid, where 0 represents walls, 1 represents pellets, and 2 represents eaten pellets.
- Several helper functions are defined:
- square(x, y) : Draws a square at the given coordinates (x, y) using the Turtle graphics.
- offset(point) : Converts a point in the game world to an index in the tiles list.
- valid(point) : Checks if a given point is a valid position in the game world, considering walls and boundaries.
- world() : Draws the game world by iterating through the tiles list and calling square() to draw each tile.
To run this game you can use an online python compiler or run it on your machine, when you run this program below is the image output you will get.
Use the arrow keys to move the player, if you touch the enemies the game will be over. The game has some bugs.
This was the tutorial on how to make pacman game, I hope you found this tutorial helpful and useful, do share it with your friends who are intrested in creating games in python
Here are more python games tutorial you may find interesting:
I hope you found what you were looking for from this tutorial and if you want more python tutorials like this, do join our Telegram channel for future updates.
Thanks for reading, have a nice day 🙂