[text] jj

Viewer

  1. import pygame
  2. import sys
  3.  
  4. # Initialize pygame
  5. pygame.init()
  6.  
  7. # Constants
  8. WIDTH, HEIGHT = 800, 600
  9. CELL_SIZE = 40
  10. FPS = 60
  11. BLACK = (0, 0, 0)
  12. YELLOW = (255, 255, 0)
  13.  
  14. # Create the game window
  15. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  16. pygame.display.set_caption("Pac-Man")
  17.  
  18. # Load images
  19. pacman_image = pygame.image.load("pacman.png")
  20. pacman_rect = pacman_image.get_rect(center=(WIDTH // 2, HEIGHT // 2))
  21.  
  22. # Game loop
  23. clock = pygame.time.Clock()
  24. running = True
  25.  
  26. while running:
  27.     # Handle events
  28.     for event in pygame.event.get():
  29.         if event.type == pygame.QUIT:
  30.             running = False
  31.  
  32.     # Handle player input
  33.     keys = pygame.key.get_pressed()
  34.     if keys[pygame.K_LEFT]:
  35.         pacman_rect.x -= CELL_SIZE
  36.     elif keys[pygame.K_RIGHT]:
  37.         pacman_rect.x += CELL_SIZE
  38.     elif keys[pygame.K_UP]:
  39.         pacman_rect.y -= CELL_SIZE
  40.     elif keys[pygame.K_DOWN]:
  41.         pacman_rect.y += CELL_SIZE
  42.  
  43.     # Draw everything
  44.     screen.fill(BLACK)
  45.     screen.blit(pacman_image, pacman_rect)
  46.  
  47.     # Update display
  48.     pygame.display.flip()
  49.  
  50.     # Cap the frame rate
  51.     clock.tick(FPS)
  52.  
  53. # Quit pygame properly
  54. pygame.quit()
  55. sys.exit()
  56.  

Editor

You can edit this paste and save as new:


File Description
  • jj
  • Paste Code
  • 25 Apr-2024
  • 1.18 Kb
You can Share it: