Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- # Initialize pygame
- pygame.init()
- # Screen dimensions
- WIDTH, HEIGHT = 640, 480
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- clock = pygame.time.Clock()
- # Colors
- LIGHT_BLUE = (173, 216, 230)
- DARK_GRAY = (64, 64, 64)
- BLACK = (0, 0, 0)
- # Updated Map layout
- MAP = [
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1],
- [1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
- [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1],
- [1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
- ]
- TILE_SIZE = 32
- MAP_WIDTH = len(MAP[0])
- MAP_HEIGHT = len(MAP)
- # Player settings
- player_x = TILE_SIZE * 1.5
- player_y = TILE_SIZE * 1.5
- player_angle = 0
- player_speed = 3
- # Raycasting settings
- FOV = math.pi / 3
- HALF_FOV = FOV / 2
- NUM_RAYS = 120
- MAX_DEPTH = 800
- DELTA_ANGLE = FOV / NUM_RAYS
- DISTANCE_TO_PROJ_PLANE = (WIDTH / 2) / math.tan(HALF_FOV)
- SCALE = WIDTH // NUM_RAYS
- # Load wall image
- try:
- woodwall_img = pygame.image.load('woodwall.png')
- except pygame.error as e:
- print(f"Error loading image: {e}")
- pygame.quit()
- exit()
- def check_collision(x, y):
- """Check if a position collides with a wall."""
- map_x = int(x / TILE_SIZE)
- map_y = int(y / TILE_SIZE)
- if 0 <= map_x < MAP_WIDTH and 0 <= map_y < MAP_HEIGHT:
- return MAP[map_y][map_x] == 1
- return False
- def draw_walls():
- for ray in range(NUM_RAYS):
- ray_angle = player_angle - HALF_FOV + ray * DELTA_ANGLE
- for depth in range(MAX_DEPTH):
- target_x = player_x + depth * math.cos(ray_angle)
- target_y = player_y + depth * math.sin(ray_angle)
- map_x = int(target_x / TILE_SIZE)
- map_y = int(target_y / TILE_SIZE)
- if MAP[map_y][map_x] == 1:
- depth *= math.cos(player_angle - ray_angle)
- wall_height = 21000 / (depth + 0.0001)
- wall_top = HEIGHT // 2 - wall_height // 2
- # Scale the wall image to fit the wall height
- scaled_wall_img = pygame.transform.scale(woodwall_img, (SCALE, int(wall_height)))
- # Draw the wall by tiling the image horizontally
- for i in range(ray * SCALE, (ray + 1) * SCALE, scaled_wall_img.get_width()):
- screen.blit(scaled_wall_img, (i, wall_top))
- # Draw the floor below the wall
- pygame.draw.rect(screen, DARK_GRAY,
- (ray * SCALE, wall_top + wall_height, SCALE, HEIGHT - (wall_top + wall_height)))
- break
- def move_player():
- global player_x, player_y, player_angle
- keys = pygame.key.get_pressed()
- dx = 0
- dy = 0
- if keys[pygame.K_w]:
- dx = player_speed * math.cos(player_angle)
- dy = player_speed * math.sin(player_angle)
- if keys[pygame.K_s]:
- dx = -player_speed * math.cos(player_angle)
- dy = -player_speed * math.sin(player_angle)
- new_x = player_x + dx
- new_y = player_y + dy
- if not check_collision(new_x, player_y):
- player_x = new_x
- if not check_collision(player_x, new_y):
- player_y = new_y
- if keys[pygame.K_a]:
- player_angle -= 0.05
- if keys[pygame.K_d]:
- player_angle += 0.05
- # Main loop
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- screen.fill((0, 0, 0))
- move_player()
- draw_walls()
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement