Create a raycast 3D game using pygame and add drunk effect (animation) for the camera,…
- User
Create a raycast 3D game using pygame and add drunk effect (animation) for the camera, provide full code
- Assistant
Create a raycast 3D game using pygame and add drunk effect (animation) for the camera, provide full code
Creating a simple raycasting 3D game using Pygame with a "drunk" camera effect involves several steps. Below is a complete Python script that accomplishes this. The game features a basic map, player movement, raycasting for 3D rendering, and a camera shake effect to simulate being drunk.
pip install pygame)import pygame
import math
import sys
import random
from pygame.locals import *
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
HALF_HEIGHT = SCREEN_HEIGHT // 2
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (100, 100, 100)
BLUE = (0, 0, 255)
DARKGRAY = (110, 110, 110)
RED = (255, 0, 0)
# Map settings
MAP_WIDTH = 24
MAP_HEIGHT = 24
world_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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
]
# Player settings
player_x = 12.0
player_y = 12.0
player_angle = 0.0
player_speed = 0.05
rot_speed = 0.03
# Drunk effect settings
drunk_intensity = 0.02 # Max pixel shift for the drunk effect
drunk_frequency = 0.05 # Chance to change the shake direction
current_offset = [0, 0]
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Raycasting 3D Game with Drunk Effect")
clock = pygame.time.Clock()
def draw_floor_ceiling():
# Draw floor and ceiling
ceiling_color = DARKGRAY
floor_color = GRAY
pygame.draw.rect(screen, ceiling_color, (0, 0, SCREEN_WIDTH, HALF_HEIGHT))
pygame.draw.rect(screen, floor_color, (0, HALF_HEIGHT, SCREEN_WIDTH, HALF_HEIGHT))
def cast_rays():
# Raycasting parameters
fov = math.pi / 3 # 60 degrees Field of View
num_rays = SCREEN_WIDTH
delta_angle = fov / num_rays
max_depth = 20
textures = {
1: BLUE
}
start_angle = player_angle - fov / 2
for ray in range(num_rays):
ray_angle = start_angle + ray * delta_angle
sin_a = math.sin(ray_angle)
cos_a = math.cos(ray_angle)
# DDA algorithm
depth = 0
hit = False
while not hit and depth < max_depth:
depth += 0.05
target_x = player_x + depth * cos_a
target_y = player_y + depth * sin_a
# Check for wall collision
if (int(target_x) >= MAP_WIDTH or int(target_y) >= MAP_HEIGHT or
int(target_x) < 0 or int(target_y) < 0):
hit = True
depth = max_depth
elif world_map[int(target_y)][int(target_x)] > 0:
hit = True
# Correct the fish-eye effect
depth *= math.cos(player_angle - ray_angle)
# Calculate wall height
if depth == 0:
depth = 0.0001 # Prevent division by zero
proj_height = min(int(200 / depth), SCREEN_HEIGHT)
# Determine the color
color = textures.get(world_map[int(target_y)][int(target_x)], WHITE)
shade = 255 / (1 + depth * depth * 0.1)
color = (color[0] * shade / 255, color[1] * shade / 255, color[2] * shade / 255)
# Draw the vertical line
pygame.draw.line(screen, color, (ray, HALF_HEIGHT - proj_height // 2),
(ray, HALF_HEIGHT + proj_height // 2))
def apply_drunk_effect():
global current_offset
# Randomly change direction with a certain frequency
if random.random() < drunk_frequency:
current_offset[0] = random.randint(-int(drunk_intensity*100), int(drunk_intensity*100)) / 100
current_offset[1] = random.randint(-int(drunk_intensity*100), int(drunk_intensity*100)) / 100
return current_offset
def main():
global player_x, player_y, player_angle
running = True
while running:
clock.tick(60) # Limit to 60 FPS
for event in pygame.event.get():
if event.type == QUIT:
running = False
# Movement
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
running = False
if keys[K_w]:
new_x = player_x + math.cos(player_angle) * player_speed
new_y = player_y + math.sin(player_angle) * player_speed
# Collision detection
if world_map[int(new_y)][int(new_x)] == 0:
player_x = new_x
player_y = new_y
if keys[K_s]:
new_x = player_x - math.cos(player_angle) * player_speed
new_y = player_y - math.sin(player_angle) * player_speed
if world_map[int(new_y)][int(new_x)] == 0:
player_x = new_x
player_y = new_y
if keys[K_a]:
player_angle -= rot_speed
if keys[K_d]:
player_angle += rot_speed
# Apply drunk effect
offset = apply_drunk_effect()
# Render
screen.fill(BLACK)
# Create a new surface for rendering to apply offset
render_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
render_surface.fill(BLACK)
# Draw floor and ceiling on render_surface
ceiling_color = DARKGRAY
floor_color = GRAY
pygame.draw.rect(render_surface, ceiling_color, (0, 0, SCREEN_WIDTH, HALF_HEIGHT))
pygame.draw.rect(render_surface, floor_color, (0, HALF_HEIGHT, SCREEN_WIDTH, HALF_HEIGHT))
# Cast rays and draw walls
fov = math.pi / 3 # 60 degrees Field of View
num_rays = SCREEN_WIDTH
delta_angle = fov / num_rays
max_depth = 20
textures = {
1: BLUE
}
start_angle = player_angle - fov / 2
for ray in range(num_rays):
ray_angle = start_angle + ray * delta_angle
sin_a = math.sin(ray_angle)
cos_a = math.cos(ray_angle)
# DDA algorithm
depth = 0
hit = False
while not hit and depth < max_depth:
depth += 0.05
target_x = player_x + depth * cos_a
target_y = player_y + depth * sin_a
# Check for wall collision
if (int(target_x) >= MAP_WIDTH or int(target_y) >= MAP_HEIGHT or
int(target_x) < 0 or int(target_y) < 0):
hit = True
depth = max_depth
elif world_map[int(target_y)][int(target_x)] > 0:
hit = True
# Correct the fish-eye effect
depth *= math.cos(player_angle - ray_angle)
# Calculate wall height
if depth == 0:
depth = 0.0001 # Prevent division by zero
proj_height = min(int(200 / depth), SCREEN_HEIGHT)
# Determine the color
color = textures.get(world_map[int(target_y)][int(target_x)], WHITE)
shade = 255 / (1 + depth * depth * 0.1)
color = (color[0] * shade / 255, color[1] * shade / 255, color[2] * shade / 255)
# Draw the vertical line
pygame.draw.line(render_surface, color, (ray, HALF_HEIGHT - proj_height // 2),
(ray, HALF_HEIGHT + proj_height // 2))
# Apply camera shake by blitting with offset
screen.blit(render_surface, (offset[0]*SCREEN_WIDTH, offset[1]*SCREEN_HEIGHT))
# Display update
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
Initialization:
world_map represents the game map where 1 is a wall and 0 is empty space.Player Settings:
player_x, player_y) is initialized at the center of the map.player_angle) and movement speeds are defined.Drunk Effect Settings:
drunk_intensity controls the maximum shake amount.drunk_frequency determines how often the shake direction changes.current_offset stores the current camera shake offset.Rendering Functions:
draw_floor_ceiling(): Draws the floor and ceiling colors.cast_rays(): Implements the raycasting algorithm to render walls. (Note: In the final code, this function was replaced by inline raycasting within the main() loop to apply the drunk effect correctly.)Drunk Effect Implementation:
apply_drunk_effect(): Randomly changes the camera offset based on drunk_frequency and drunk_intensity. This simulates the camera shaking.Main Loop:
W, A, S, D keys).Execution:
main() function runs the game loop, updating the display and handling events until the user quits the game.Install Pygame: Ensure you have Pygame installed. You can install it using pip:
pip install pygame
Save the Script:
Save the above Python script to a file, e.g., raycasting_game.py.
Run the Game: Execute the script using Python:
python raycasting_game.py
Controls:
W: Move ForwardS: Move BackwardA: Rotate LeftD: Rotate RightESC: Quit the gameThis is a basic implementation to get you started. You can enhance it further by:
Feel free to customize and expand upon this foundation to create a more immersive and feature-rich game!
Not yet included in a published release.