Creating a game like Doom using Pygame is a complex task, as it involves implementing a 2.5D ray-casting engine, player movement, collision detection, rendering, and more. Below is a simplified version of such a game. It includes a basic ray-casting engine, player movement, aiming with the mouse and keyboard, a start menu, and a scoring system.
This code is a starting point and can be expanded upon to create a more complete game.
Full Code for a Basic Doom-like Game in Pygame
import pygame
import math
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
HALF_WIDTH = SCREEN_WIDTH // 2
HALF_HEIGHT = SCREEN_HEIGHT // 2
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (100, 100, 100)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Game settings
FOV = math.pi / 3 # Field of view (60 degrees)
NUM_RAYS = 120
MAX_DEPTH = 800
DELTA_ANGLE = FOV / NUM_RAYS
SCALE = SCREEN_WIDTH // NUM_RAYS
PLAYER_SPEED = 3
PLAYER_ROT_SPEED = 0.03
MOUSE_SENSITIVITY = 0.002
RUN_MULTIPLIER = 2
# Map settings
MAP = [
"########",
"#......#",
"#..#...#",
"#..#...#",
"#......#",
"########",
]
TILE_SIZE = 100
MAP_WIDTH = len(MAP[0]) * TILE_SIZE
MAP_HEIGHT = len(MAP) * TILE_SIZE
# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Doom-like Game")
# Clock for controlling frame rate
clock = pygame.time.Clock()
# Player class
class Player:
def __init__(self, x, y, angle):
self.x = x
self.y = y
self.angle = angle
self.score = 0
self.speed = PLAYER_SPEED
def move(self, keys):
sin_a = math.sin(self.angle)
cos_a = math.cos(self.angle)
dx, dy = 0, 0
if keys[pygame.K_w]:
dx += self.speed * cos_a
dy += self.speed * sin_a
if keys[pygame.K_s]:
dx -= self.speed * cos_a
dy -= self.speed * sin_a
if keys[pygame.K_a]:
dx += self.speed * sin_a
dy -= self.speed * cos_a
if keys[pygame.K_d]:
dx -= self.speed * sin_a
dy += self.speed * cos_a
# Running
if keys[pygame.K_LSHIFT]:
dx *= RUN_MULTIPLIER
dy *= RUN_MULTIPLIER
# Collision detection
if not self.wall_collision(self.x + dx, self.y):
self.x += dx
if not self.wall_collision(self.x, self.y + dy):
self.y += dy
def wall_collision(self, x, y):
map_x = int(x // TILE_SIZE)
map_y = int(y // TILE_SIZE)
return MAP[map_y][map_x] == "#"
def rotate(self, keys, mouse_dx):
if keys[pygame.K_LEFT]:
self.angle -= PLAYER_ROT_SPEED
if keys[pygame.K_RIGHT]:
self.angle += PLAYER_ROT_SPEED
# Mouse aiming
self.angle += mouse_dx * MOUSE_SENSITIVITY
self.angle %= 2 * math.pi
# Ray-casting function
def ray_casting(player):
rays = []
start_angle = player.angle - FOV / 2
for ray in range(NUM_RAYS):
angle = start_angle + ray * DELTA_ANGLE
sin_a = math.sin(angle)
cos_a = math.cos(angle)
for depth in range(MAX_DEPTH):
x = player.x + depth * cos_a
y = player.y + depth * sin_a
map_x = int(x // TILE_SIZE)
map_y = int(y // TILE_SIZE)
if MAP[map_y][map_x] == "#":
depth *= math.cos(player.angle - angle) # Fix fish-eye effect
proj_height = TILE_SIZE / (depth + 0.0001) * 500
color = GRAY if (map_x + map_y) % 2 else WHITE
rays.append((ray * SCALE, HALF_HEIGHT - proj_height // 2, SCALE, proj_height, color))
break
return rays
# Draw function
def draw(player, rays):
screen.fill(BLACK)
# Draw rays (walls)
for ray in rays:
pygame.draw.rect(screen, ray[4], ray[:4])
# Draw HUD
pygame.draw.rect(screen, RED, (10, 10, 200, 30))
pygame.draw.rect(screen, BLUE, (10, 10, player.score * 2, 30))
pygame.draw.rect(screen, WHITE, (10, 10, 200, 30), 2)
# Draw score
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {player.score}", True, WHITE)
screen.blit(score_text, (10, 50))
# Main menu
def main_menu():
font = pygame.font.Font(None, 74)
title_text = font.render("Doom-like Game", True, WHITE)
start_text = font.render("Press ENTER to Start", True, WHITE)
while True:
screen.fill(BLACK)
screen.blit(title_text, (HALF_WIDTH - title_text.get_width() // 2, HALF_HEIGHT - 100))
screen.blit(start_text, (HALF_WIDTH - start_text.get_width() // 2, HALF_HEIGHT + 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
return
pygame.display.flip()
clock.tick(60)
# Main game loop
def main():
player = Player(150, 150, 0)
mouse_dx = 0
# Center the mouse
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
while True:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEMOTION:
mouse_dx = event.rel[0]
player.move(keys)
player.rotate(keys, mouse_dx)
rays = ray_casting(player)
draw(player, rays)
pygame.display.flip()
clock.tick(60)
# Run the game
if __name__ == "__main__":
main_menu()
main()
Features Implemented:
- Ray-Casting Engine: A simple ray-casting algorithm to render walls.
- Player Movement: WASD for movement, Shift to run, and Space for jumping (jumping is not implemented in this version but can be added).
- Mouse and Keyboard Aiming: Rotate the player using the mouse or arrow keys.
- Start Menu: A basic start menu with a title and instructions.
- Score System: A placeholder score system displayed on the HUD.
How to Run:
- Install Pygame:
pip install pygame.
- Save the code to a file, e.g.,
doom_game.py.
- Run the file:
python doom_game.py.
Notes:
- This is a basic implementation and lacks advanced features like enemies, weapons, textures, and sound effects.
- The map is hardcoded, but you can expand it or load it from a file.
- The score system is a placeholder and can be tied to gameplay events like killing enemies or collecting items.
Feel free to expand and improve this code!