Untitled

 avatar
unknown
plain_text
13 days ago
2.4 kB
2
Indexable
mport pygame
import random

# 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 de Saltar Obstáculos")

# Colores
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Reloj para controlar la velocidad del juego
clock = pygame.time.Clock()

# Jugador
player_width = 50
player_height = 50
player_x = 50
player_y = HEIGHT - player_height - 50
player_vel_y = 0
gravity = 1

# Obstáculos
obstacle_width = 50
obstacle_height = random.randint(100, 300)
obstacle_x = WIDTH
obstacle_vel_x = -5

def draw_player(x, y):
    pygame.draw.rect(screen, BLACK, (x, y, player_width, player_height))

def draw_obstacle(x, height):
    pygame.draw.rect(screen, RED, (x, HEIGHT - height, obstacle_width, height))

def check_collision(player_x, player_y, obstacle_x):
    if obstacle_x < player_x + player_width and obstacle_x + obstacle_width > player_x:
        if player_y + player_height > HEIGHT - obstacle_height:
            return True
    return False

# Bucle principal del juego
running = True
while running:
    screen.fill(WHITE)

    # Eventos del juego
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Salto del jugador al presionar espacio
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and player_y == HEIGHT - player_height - 50:
                player_vel_y = -15

    # Movimiento del jugador (gravedad)
    player_vel_y += gravity
    player_y += player_vel_y

    # Limitar el jugador al suelo
    if player_y >= HEIGHT - player_height - 50:
        player_y = HEIGHT - player_height - 50

    # Movimiento del obstáculo
    obstacle_x += obstacle_vel_x

    # Si el obstáculo sale de la pantalla, generar uno nuevo
    if obstacle_x + obstacle_width < 0:
        obstacle_x = WIDTH
        obstacle_height = random.randint(100, 300)

    # Dibujar jugador y obstáculo
    draw_player(player_x, player_y)
    draw_obstacle(obstacle_x, obstacle_height)

    # Verificar colisión
    if check_collision(player_x, player_y, obstacle_x):
        print("¡Perdiste!")
        running = False

    # Actualizar pantalla y controlar FPS (Frames Per Second)
    pygame.display.update()
    clock.tick(30)

# Salir del juego al terminar el bucle principal.
pygame.quit()
Editor is loading...
Leave a Comment