Creating a simple parody of DOOM using Pygame involves several components, including displaying graphics, handling player input, and implementing basic game mechanics. For this example, I'll walk you through a simple setup where you navigate a 2D grid as a character, with basic movement and an enemy object to avoid or "defeat." This will be a very simplified and minimalistic version for demonstration purposes.
Setting Up Pygame
First, ensure you have Pygame installed:
pip install pygame
Now, here's a basic framework for a DOOM parody game using Pygame:
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 640, 480
PLAYER_SIZE = 20
ENEMY_SIZE = 20
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
PLAYER_SPEED = 5
ENEMY_SPEED = 3
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mini DOOM Parody")
# Define the player and enemy classes
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((PLAYER_SIZE, PLAYER_SIZE))
self.image.fill(WHITE)
self.rect = self.image.get_rect(center=(WIDTH // 2, HEIGHT // 2))
def update(self, keys):
if keys[pygame.K_LEFT]:
self.rect.x -= PLAYER_SPEED
if keys[pygame.K_RIGHT]:
self.rect.x += PLAYER_SPEED
if keys[pygame.K_UP]:
self.rect.y -= PLAYER_SPEED
if keys[pygame.K_DOWN]:
self.rect.y += PLAYER_SPEED
# Keep player on the screen
self.rect.clamp_ip(screen.get_rect())
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((ENEMY_SIZE, ENEMY_SIZE))
self.image.fill(RED)
self.rect = self.image.get_rect(center=(random.randint(0, WIDTH), random.randint(0, HEIGHT)))
def update(self):
direction = random.choice(['up', 'down', 'left', 'right'])
if direction == 'up':
self.rect.y -= ENEMY_SPEED
elif direction == 'down':
self.rect.y += ENEMY_SPEED
elif direction == 'left':
self.rect.x -= ENEMY_SPEED
elif direction == 'right':
self.rect.x += ENEMY_SPEED
# Keep enemy on the screen
self.rect.clamp_ip(screen.get_rect())
def main():
clock = pygame.time.Clock()
player = Player()
enemy = Enemy()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(enemy)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
player.update(keys)
enemy.update()
# Collisions
if pygame.sprite.collide_rect(player, enemy):
print("Collided with enemy!")
# Draw everything
screen.fill(BLACK)
all_sprites.draw(screen)
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
Explanation
-
Pygame Setup: The game initializes with a window size of 640x480 pixels and sets up the display and clock for controlling the frame rate.
-
Classes:
- Player: Can be controlled with arrow keys, moving in 4 directions.
- Enemy: Moves randomly around the screen.
-
Sprite Groups: Pygame sprite groups handle the efficient updating and drawing of sprites.
-
Main Loop:
- Handles quitting the game.
- Updates player and enemy positions.
- Checks for collisions.
- Draws all sprites on the screen.
This simple framework provides a base to expand, adding more gameplay elements like additional enemies, shooting mechanics, or scoring systems. This version is straightforward and does not include sophisticated graphics or sound effects, but you can extend it for a more involved parody experience!