Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import random
- import time
- def main(stdscr):
- # Initialize curses settings
- curses.curs_set(0)
- stdscr.nodelay(1)
- stdscr.timeout(100)
- # Define the game map (original Pac-Man layout)
- game_map = [
- "############################",
- "#............##............#",
- "#.####.#####.##.#####.####.#",
- "#o# #.# #.##.# #.# o#",
- "#.####.#####.##.#####.####.#",
- "#..........................#",
- "#.####.##.########.##.####.#",
- "#.####.##.########.##.####.#",
- "#......##....##....##......#",
- "######.##### ## #####.######",
- " #.##### ## #####.# ",
- " #.## ##.# ",
- " #.## ###--### ##.# ",
- "######.## # # ##.######",
- " . # # . ",
- "######.## # # ##.######",
- " #.## ######## ##.# ",
- " #.## ##.# ",
- " #.## ######## ##.# ",
- "######.## ######## ##.######",
- "#............##............#",
- "#.####.#####.##.#####.####.#",
- "#.####.#####.##.#####.####.#",
- "#o..##................##..o#",
- "###.##.##.########.##.##.###",
- "###.##.##.########.##.##.###",
- "#......##....##....##......#",
- "#.##########.##.##########.#",
- "#.##########.##.##########.#",
- "#..........................#",
- "############################"
- ]
- # Convert map to list of lists for easy modification
- game_map = [list(row) for row in game_map]
- # Initialize game state
- pacman_pos = [14, 23]
- ghosts = [
- {'pos': [12, 13], 'dir': (0, 0)},
- {'pos': [15, 13], 'dir': (0, 0)},
- {'pos': [12, 15], 'dir': (0, 0)},
- {'pos': [15, 15], 'dir': (0, 0)}
- ]
- score = 0
- lives = 3
- scared_timer = 0
- current_dir = (0, 0)
- dots_eaten = 0
- total_dots = sum(row.count('.') + row.count('o') for row in game_map)
- # Main game loop
- while True:
- # Handle input
- key = stdscr.getch()
- if key == curses.KEY_UP:
- new_dir = (0, -1)
- elif key == curses.KEY_DOWN:
- new_dir = (0, 1)
- elif key == curses.KEY_LEFT:
- new_dir = (-1, 0)
- elif key == curses.KEY_RIGHT:
- new_dir = (1, 0)
- elif key == ord('q'):
- break
- else:
- new_dir = current_dir
- # Move Pacman
- if new_dir != (0, 0):
- new_x = pacman_pos[0] + new_dir[0]
- new_y = pacman_pos[1] + new_dir[1]
- # Handle tunnel wrap-around
- if new_y < 0: new_y = len(game_map) - 1
- elif new_y >= len(game_map): new_y = 0
- if new_x < 0: new_x = len(game_map[new_y]) - 1
- elif new_x >= len(game_map[new_y]): new_x = 0
- if game_map[new_y][new_x] != '#':
- pacman_pos = [new_x, new_y]
- current_dir = new_dir
- # Handle dot eating
- if game_map[new_y][new_x] == '.':
- score += 10
- dots_eaten += 1
- game_map[new_y][new_x] = ' '
- elif game_map[new_y][new_x] == 'o':
- score += 50
- dots_eaten += 1
- scared_timer = 30
- game_map[new_y][new_x] = ' '
- # Move ghosts
- for ghost in ghosts:
- possible_dirs = [(0, -1), (0, 1), (-1, 0), (1, 0)]
- random.shuffle(possible_dirs)
- for d in possible_dirs:
- new_x = ghost['pos'][0] + d[0]
- new_y = ghost['pos'][1] + d[1]
- # Handle tunnel wrap-around
- if new_y < 0: new_y = len(game_map) - 1
- elif new_y >= len(game_map): new_y = 0
- if new_x < 0: new_x = len(game_map[new_y]) - 1
- elif new_x >= len(game_map[new_y]): new_x = 0
- if game_map[new_y][new_x] != '#':
- ghost['pos'] = [new_x, new_y]
- ghost['dir'] = d
- break
- # Check collisions
- for ghost in ghosts:
- if ghost['pos'] == pacman_pos:
- if scared_timer > 0:
- # Respawn ghost
- ghost['pos'] = [random.choice([12, 15]), 13]
- score += 200
- else:
- lives -= 1
- if lives <= 0:
- stdscr.addstr(len(game_map)+2, 0, "GAME OVER!")
- stdscr.refresh()
- time.sleep(2)
- return
- # Reset positions
- pacman_pos = [14, 23]
- for g in ghosts:
- g['pos'] = [random.choice([12, 15]), 13]
- # Update scared timer
- if scared_timer > 0:
- scared_timer -= 1
- # Check win condition
- if dots_eaten >= total_dots:
- stdscr.addstr(len(game_map)+2, 0, "YOU WIN!")
- stdscr.refresh()
- time.sleep(2)
- return
- # Draw game state
- stdscr.clear()
- for y in range(len(game_map)):
- for x in range(len(game_map[y])):
- stdscr.addch(y, x, game_map[y][x])
- # Draw Pacman
- stdscr.addch(pacman_pos[1], pacman_pos[0], 'C')
- # Draw ghosts
- for ghost in ghosts:
- if scared_timer > 0:
- stdscr.addch(ghost['pos'][1], ghost['pos'][0], '@')
- else:
- stdscr.addch(ghost['pos'][1], ghost['pos'][0], 'G')
- # Draw UI
- stdscr.addstr(len(game_map)+1, 0, f"Score: {score} Lives: {lives}")
- stdscr.refresh()
- if __name__ == "__main__":
- curses.wrapper(main)
Add Comment
Please, Sign In to add comment