Code me a fully fonctionnal pacman game with menus and differents maps in python
- User
Code me a fully fonctionnal pacman game with menus and differents maps in python
- Assistant
Code me a fully fonctionnal pacman game with menus and differents maps in python
Certainly! Creating a fully functional Pac-Man game with menus and different maps in Python is an exciting project. Below is a simplified version of such a game using the pygame library. This example includes a main menu, level selection, and basic gameplay mechanics. You can expand upon this foundation to add more features and complexity.
Prerequisites:
Before running the code, make sure you have installed pygame:
pip install pygame
pacman_game.pyimport pygame
import sys
from levels import Level
from menu import MainMenu, LevelMenu
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 560
SCREEN_HEIGHT = 620
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Pac-Man")
# Game states
STATE_MAIN_MENU = 'main_menu'
STATE_LEVEL_MENU = 'level_menu'
STATE_PLAYING = 'playing'
def main():
clock = pygame.time.Clock()
state = STATE_MAIN_MENU
main_menu = MainMenu(SCREEN)
level_menu = LevelMenu(SCREEN)
level = None
while True:
if state == STATE_MAIN_MENU:
option = main_menu.display()
if option == 'play':
state = STATE_LEVEL_MENU
elif option == 'quit':
pygame.quit()
sys.exit()
elif state == STATE_LEVEL_MENU:
selected_level = level_menu.display()
if selected_level is not None:
level = Level(SCREEN, f'level{selected_level}.txt')
state = STATE_PLAYING
else:
state = STATE_MAIN_MENU
elif state == STATE_PLAYING:
level.play()
state = STATE_MAIN_MENU
clock.tick(60)
if __name__ == '__main__':
main()
menu.pyimport pygame
import sys
class MainMenu:
def __init__(self, screen):
self.screen = screen
self.font = pygame.font.Font(None, 74)
def display(self):
while True:
self.screen.fill((0, 0, 0))
title = self.font.render("Pac-Man", True, (255, 255, 0))
play_text = self.font.render("Play", True, (255, 255, 255))
quit_text = self.font.render("Quit", True, (255, 255, 255))
self.screen.blit(title, (180, 100))
self.screen.blit(play_text, (250, 250))
self.screen.blit(quit_text, (250, 350))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
return 'play'
if event.key == pygame.K_q:
return 'quit'
class LevelMenu:
def __init__(self, screen):
self.screen = screen
self.font = pygame.font.Font(None, 74)
def display(self):
while True:
self.screen.fill((0, 0, 0))
select_level_text = self.font.render("Select Level", True, (255, 255, 255))
level1_text = self.font.render("1", True, (255, 255, 255))
level2_text = self.font.render("2", True, (255, 255, 255))
back_text = self.font.render("Back", True, (255, 255, 255))
self.screen.blit(select_level_text, (150, 100))
self.screen.blit(level1_text, (270, 200))
self.screen.blit(level2_text, (270, 300))
self.screen.blit(back_text, (250, 400))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
return 1
if event.key == pygame.K_2:
return 2
if event.key == pygame.K_b:
return None
levels.pyimport pygame
import sys
class Level:
def __init__(self, screen, level_file):
self.screen = screen
self.level_file = level_file
self.load_level()
self.pacman = Pacman(280, 310)
def load_level(self):
with open(self.level_file, 'r') as file:
self.map_data = [line.strip() for line in file]
def draw_map(self):
for y, row in enumerate(self.map_data):
for x, tile in enumerate(row):
if tile == '1':
pygame.draw.rect(self.screen, (0, 0, 255), (x*20, y*20, 20, 20))
def play(self):
clock = pygame.time.Clock()
running = True
while running:
self.screen.fill((0, 0, 0))
self.draw_map()
self.pacman.update(self.map_data)
self.pacman.draw(self.screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(60)
class Pacman:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 20, 20)
self.dx = 0
self.dy = 0
def update(self, map_data):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.dx = -2
self.dy = 0
elif keys[pygame.K_RIGHT]:
self.dx = 2
self.dy = 0
elif keys[pygame.K_UP]:
self.dx = 0
self.dy = -2
elif keys[pygame.K_DOWN]:
self.dx = 0
self.dy = 2
# Collision Detection
next_rect = self.rect.move(self.dx, self.dy)
if not self.collides_with_wall(next_rect, map_data):
self.rect = next_rect
def collides_with_wall(self, rect, map_data):
x, y = rect.topleft
x = x // 20
y = y // 20
if map_data[y][x] == '1':
return True
return False
def draw(self, screen):
pygame.draw.circle(screen, (255, 255, 0), self.rect.center, 10)
Create two level files named level1.txt and level2.txt in the same directory. These files define the map layout.
level1.txt:
11111111111111111111
10000000000000000001
10111110111111011101
10000000100001000001
11101110101111011111
10001000000000010001
10111011111111011101
10001010000001010001
10111010111101011101
10000010000001000001
11111111111111111111
level2.txt:
11111111111111111111
10000011110011100001
10111010010010011101
10001010111110100001
11101010000010101111
10001011101110100001
10111000001000011101
10000011101110100001
11101110000011101111
10000010000001000001
11111111111111111111
Save all the code snippets into their respective .py files (pacman_game.py, menu.py, levels.py) and the level data into level1.txt and level2.txt.
Ensure all files are in the same directory.
Run the main game script:
python pacman_game.py
In the game:
MainMenu class): Displays the game title and options to play or quit.LevelMenu class): Allows selection between different levels.Level class): Handles loading the map data from text files, drawing the map, and running the game loop for that level.Pacman class): Controls Pac-Man's movement, collision detection with walls, and rendering.Feel free to expand upon this foundation to create a more complete and engaging Pac-Man game. Happy coding!
Not yet included in a published release.