¡Claro! Aquí tienes un ejemplo de un juego simple en 2D hecho con Pygame. Este juego será un pequeño "Juego de Plataformas Básico" donde el jugador puede moverse hacia los lados y saltar sobre una plataforma.
🛠️ Requisitos previos
Asegúrate de tener instalado pygame. Si no lo tienes, puedes instalarlo con:
pip install pygame
🕹️ Juego: Movimiento básico en 2D (Plataformas)
import pygame
import sys
# Inicializar Pygame
pygame.init()
# Configuración de la pantalla
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Juego 2D Simple - Plataformas")
# Colores
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
# FPS
clock = pygame.time.Clock()
FPS = 60
# Clase del jugador
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50, 50))
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = 100
self.rect.y = HEIGHT - 100
self.velocity_y = 0
self.jumping = False
def update(self):
keys = pygame.key.get_pressed()
dx = 0
dy = 0
# Movimiento lateral
if keys[pygame.K_LEFT]:
dx = -5
if keys[pygame.K_RIGHT]:
dx = 5
# Salto
if keys[pygame.K_SPACE] and not self.jumping:
self.velocity_y = -15
self.jumping = True
# Aplicar gravedad
self.velocity_y += 1
if self.velocity_y > 5:
self.velocity_y = 5
dy += self.velocity_y
# Actualizar posición
self.rect.x += dx
self.rect.y += dy
# Evitar caer fuera de la pantalla
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
self.jumping = False
self.velocity_y = 0
# Clase de las plataformas
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# Crear grupos de sprites
player = Player()
platform = Platform(200, HEIGHT - 40, 400, 20)
all_sprites = pygame.sprite.Group()
all_sprites.add(player, platform)
# Loop principal del juego
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Actualizar sprites
all_sprites.update()
# Dibujar todo
screen.fill(WHITE)
all_sprites.draw(screen)
# Actualizar pantalla
pygame.display.flip()
pygame.quit()
sys.exit()
✅ Funcionalidades incluidas:
- Movimiento izquierda/derecha con teclas
← y →.
- Salto con la barra espaciadora.
- Gravedad simple.
- Una plataforma verde en la parte inferior.
🚀 ¿Quieres mejorarlo?
Aquí algunas ideas para seguir desarrollando:
- Añadir colisión con múltiples plataformas.
- Agregar enemigos o monedas para recolectar.
- Crear niveles.
- Agregar sonidos o música.
Si quieres que lo expandamos, ¡me encantará ayudarte!
¿Te gustaría que hagamos otro tipo de juego? Por ejemplo:
- Un juego tipo "Flappy Bird"
- Pong
- Snake
- Juego de disparos
Elige uno y lo hacemos paso a paso 😄