Created
April 18, 2023 22:22
-
-
Save EncodeTheCode/d44b1ef7eba5d2ea579c86e4415cee58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import math | |
# Define colors | |
WHITE = (255, 255, 255) | |
BLACK = (0, 0, 0) | |
RED = (255, 0, 0) | |
YELLOW = (255, 255, 0) | |
# Define constants | |
SCREEN_WIDTH = 600 | |
SCREEN_HEIGHT = 540 | |
WALL_THICKNESS = 10 | |
PLAYER_SPEED = 0.3 | |
PLAYER_ROTATION_SPEED = 4 | |
# Initialize Pygame | |
pygame.init() | |
# Set up the display | |
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) | |
pygame.display.set_caption("Pac-Man") | |
# Load the Pac-Man image | |
pac_man_surf_orig = pygame.image.load("pac_man.png").convert_alpha() | |
# Create the player Pac-Man | |
player_x = SCREEN_WIDTH // 2 | |
player_y = SCREEN_HEIGHT // 2 | |
player_angle = 0 | |
player_radius = 20 | |
player_color = YELLOW | |
# Create the walls | |
walls = [ | |
pygame.Rect(0, 0, SCREEN_WIDTH, WALL_THICKNESS), # Top wall | |
pygame.Rect(0, SCREEN_HEIGHT - WALL_THICKNESS, SCREEN_WIDTH, WALL_THICKNESS), # Bottom wall | |
pygame.Rect(0, WALL_THICKNESS, WALL_THICKNESS, SCREEN_HEIGHT - 2 * WALL_THICKNESS), # Left wall | |
pygame.Rect(SCREEN_WIDTH - WALL_THICKNESS, WALL_THICKNESS, WALL_THICKNESS, SCREEN_HEIGHT - 2 * WALL_THICKNESS) # Right wall | |
] | |
# Game loop | |
running = True | |
while running: | |
# Handle events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
# Get the current state of the arrow keys and WASD keys | |
keys = pygame.key.get_pressed() | |
move_up = keys[pygame.K_UP] or keys[pygame.K_w] | |
move_down = keys[pygame.K_DOWN] or keys[pygame.K_s] | |
move_left = keys[pygame.K_LEFT] or keys[pygame.K_a] | |
move_right = keys[pygame.K_RIGHT] or keys[pygame.K_d] | |
# Move the player | |
prev_player_pos = (player_x, player_y) | |
if move_up and not move_down: | |
player_y -= PLAYER_SPEED | |
elif move_down and not move_up: | |
player_y += PLAYER_SPEED | |
if move_left and not move_right: | |
player_x -= PLAYER_SPEED | |
elif move_right and not move_left: | |
player_x += PLAYER_SPEED | |
# Rotate the player | |
prev_player_angle = player_angle | |
if move_left: | |
player_angle += PLAYER_ROTATION_SPEED | |
elif move_right: | |
player_angle -= PLAYER_ROTATION_SPEED | |
player_angle %= 360 | |
# Draw the background | |
screen.fill(BLACK) | |
# Draw the walls | |
for wall in walls: | |
pygame.draw.rect(screen, RED, wall) | |
# Draw the player Pac-Man | |
pac_man_surf = pygame.transform.rotate(pac_man_surf_orig, player_angle) | |
pac_man_rect = pac_man_surf.get_rect(center=(player_x, player_y)) | |
screen.blit(pac_man_surf, pac_man_rect) | |
# Check for collisions with walls | |
for wall in walls: | |
if pac_man_rect.colliderect(wall): | |
player_x, player_y = prev_player_pos | |
player_angle = prev_player_angle | |
break | |
# Update the display | |
pygame.display.update() | |
# Quit Pygame | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment