Guest User

Untitled

a guest
Jan 20th, 2025
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.92 KB | None | 0 0
  1. import curses
  2. import random
  3. import time
  4.  
  5. def main(stdscr):
  6.     # Initialize curses settings
  7.     curses.curs_set(0)
  8.     stdscr.nodelay(1)
  9.     stdscr.timeout(100)
  10.    
  11.     # Define the game map (original Pac-Man layout)
  12.     game_map = [
  13.         "############################",
  14.         "#............##............#",
  15.         "#.####.#####.##.#####.####.#",
  16.         "#o#  #.#   #.##.#   #.#  o#",
  17.         "#.####.#####.##.#####.####.#",
  18.         "#..........................#",
  19.         "#.####.##.########.##.####.#",
  20.         "#.####.##.########.##.####.#",
  21.         "#......##....##....##......#",
  22.         "######.##### ## #####.######",
  23.         "     #.##### ## #####.#     ",
  24.         "     #.##          ##.#     ",
  25.         "     #.## ###--### ##.#     ",
  26.         "######.## #      # ##.######",
  27.         "      .   #      #   .      ",
  28.         "######.## #      # ##.######",
  29.         "     #.## ######## ##.#     ",
  30.         "     #.##          ##.#     ",
  31.         "     #.## ######## ##.#     ",
  32.         "######.## ######## ##.######",
  33.         "#............##............#",
  34.         "#.####.#####.##.#####.####.#",
  35.         "#.####.#####.##.#####.####.#",
  36.         "#o..##................##..o#",
  37.         "###.##.##.########.##.##.###",
  38.         "###.##.##.########.##.##.###",
  39.         "#......##....##....##......#",
  40.         "#.##########.##.##########.#",
  41.         "#.##########.##.##########.#",
  42.         "#..........................#",
  43.         "############################"
  44.     ]
  45.  
  46.     # Convert map to list of lists for easy modification
  47.     game_map = [list(row) for row in game_map]
  48.    
  49.     # Initialize game state
  50.     pacman_pos = [14, 23]
  51.     ghosts = [
  52.         {'pos': [12, 13], 'dir': (0, 0)},
  53.         {'pos': [15, 13], 'dir': (0, 0)},
  54.         {'pos': [12, 15], 'dir': (0, 0)},
  55.         {'pos': [15, 15], 'dir': (0, 0)}
  56.     ]
  57.     score = 0
  58.     lives = 3
  59.     scared_timer = 0
  60.     current_dir = (0, 0)
  61.     dots_eaten = 0
  62.     total_dots = sum(row.count('.') + row.count('o') for row in game_map)
  63.  
  64.     # Main game loop
  65.     while True:
  66.         # Handle input
  67.         key = stdscr.getch()
  68.         if key == curses.KEY_UP:
  69.             new_dir = (0, -1)
  70.         elif key == curses.KEY_DOWN:
  71.             new_dir = (0, 1)
  72.         elif key == curses.KEY_LEFT:
  73.             new_dir = (-1, 0)
  74.         elif key == curses.KEY_RIGHT:
  75.             new_dir = (1, 0)
  76.         elif key == ord('q'):
  77.             break
  78.         else:
  79.             new_dir = current_dir
  80.  
  81.         # Move Pacman
  82.         if new_dir != (0, 0):
  83.             new_x = pacman_pos[0] + new_dir[0]
  84.             new_y = pacman_pos[1] + new_dir[1]
  85.            
  86.             # Handle tunnel wrap-around
  87.             if new_y < 0: new_y = len(game_map) - 1
  88.             elif new_y >= len(game_map): new_y = 0
  89.             if new_x < 0: new_x = len(game_map[new_y]) - 1
  90.             elif new_x >= len(game_map[new_y]): new_x = 0
  91.  
  92.             if game_map[new_y][new_x] != '#':
  93.                 pacman_pos = [new_x, new_y]
  94.                 current_dir = new_dir
  95.  
  96.                 # Handle dot eating
  97.                 if game_map[new_y][new_x] == '.':
  98.                     score += 10
  99.                     dots_eaten += 1
  100.                     game_map[new_y][new_x] = ' '
  101.                 elif game_map[new_y][new_x] == 'o':
  102.                     score += 50
  103.                     dots_eaten += 1
  104.                     scared_timer = 30
  105.                     game_map[new_y][new_x] = ' '
  106.  
  107.         # Move ghosts
  108.         for ghost in ghosts:
  109.             possible_dirs = [(0, -1), (0, 1), (-1, 0), (1, 0)]
  110.             random.shuffle(possible_dirs)
  111.             for d in possible_dirs:
  112.                 new_x = ghost['pos'][0] + d[0]
  113.                 new_y = ghost['pos'][1] + d[1]
  114.                
  115.                 # Handle tunnel wrap-around
  116.                 if new_y < 0: new_y = len(game_map) - 1
  117.                 elif new_y >= len(game_map): new_y = 0
  118.                 if new_x < 0: new_x = len(game_map[new_y]) - 1
  119.                 elif new_x >= len(game_map[new_y]): new_x = 0
  120.                
  121.                 if game_map[new_y][new_x] != '#':
  122.                     ghost['pos'] = [new_x, new_y]
  123.                     ghost['dir'] = d
  124.                     break
  125.  
  126.         # Check collisions
  127.         for ghost in ghosts:
  128.             if ghost['pos'] == pacman_pos:
  129.                 if scared_timer > 0:
  130.                     # Respawn ghost
  131.                     ghost['pos'] = [random.choice([12, 15]), 13]
  132.                     score += 200
  133.                 else:
  134.                     lives -= 1
  135.                     if lives <= 0:
  136.                         stdscr.addstr(len(game_map)+2, 0, "GAME OVER!")
  137.                         stdscr.refresh()
  138.                         time.sleep(2)
  139.                         return
  140.                     # Reset positions
  141.                     pacman_pos = [14, 23]
  142.                     for g in ghosts:
  143.                         g['pos'] = [random.choice([12, 15]), 13]
  144.  
  145.         # Update scared timer
  146.         if scared_timer > 0:
  147.             scared_timer -= 1
  148.  
  149.         # Check win condition
  150.         if dots_eaten >= total_dots:
  151.             stdscr.addstr(len(game_map)+2, 0, "YOU WIN!")
  152.             stdscr.refresh()
  153.             time.sleep(2)
  154.             return
  155.  
  156.         # Draw game state
  157.         stdscr.clear()
  158.         for y in range(len(game_map)):
  159.             for x in range(len(game_map[y])):
  160.                 stdscr.addch(y, x, game_map[y][x])
  161.        
  162.         # Draw Pacman
  163.         stdscr.addch(pacman_pos[1], pacman_pos[0], 'C')
  164.        
  165.         # Draw ghosts
  166.         for ghost in ghosts:
  167.             if scared_timer > 0:
  168.                 stdscr.addch(ghost['pos'][1], ghost['pos'][0], '@')
  169.             else:
  170.                 stdscr.addch(ghost['pos'][1], ghost['pos'][0], 'G')
  171.        
  172.         # Draw UI
  173.         stdscr.addstr(len(game_map)+1, 0, f"Score: {score}  Lives: {lives}")
  174.         stdscr.refresh()
  175.  
  176. if __name__ == "__main__":
  177.     curses.wrapper(main)
  178.  
Add Comment
Please, Sign In to add comment