0% found this document useful (0 votes)
14 views3 pages

Import Pygam1

This document contains a Python script using Pygame to create a simple game called 'Dodge the Blocks'. The player controls a blue square that must avoid falling red squares, with the game increasing in difficulty as the player's score rises. The script includes functionality for player movement, enemy generation, collision detection, and score tracking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Import Pygam1

This document contains a Python script using Pygame to create a simple game called 'Dodge the Blocks'. The player controls a blue square that must avoid falling red squares, with the game increasing in difficulty as the player's score rises. The script includes functionality for player movement, enemy generation, collision detection, and score tracking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import random

# Inicializar Pygame

pygame.init()

# Configuración de la pantalla

WIDTH, HEIGHT = 500, 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Dodge the Blocks")

# Colores

WHITE = (255, 255, 255)

BLUE = (0, 0, 255)

RED = (255, 0, 0)

BLACK = (0, 0, 0)

# Jugador

player_size = 50

player_x = WIDTH // 2 - player_size // 2

player_y = HEIGHT - player_size - 10

player_speed = 7

# Enemigos

enemy_size = 50

enemy_speed = 5

enemy_list = []

# Agregar un enemigo inicial

def add_enemy():

x_pos = random.randint(0, WIDTH - enemy_size)

enemy_list.append([x_pos, 0]) # [posición X, posición Y]

add_enemy()

# Bucle del juego


running = True

clock = pygame.time.Clock()

score = 0

while running:

screen.fill(WHITE) # Fondo blanco

pygame.time.delay(30) # Control de velocidad

# Capturar eventos

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

# Obtener teclas presionadas

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and player_x > 0:

player_x -= player_speed

if keys[pygame.K_RIGHT] and player_x < WIDTH - player_size:

player_x += player_speed

# Mover enemigos

for enemy in enemy_list:

enemy[1] += enemy_speed # Mover hacia abajo

# Agregar nuevos enemigos aleatoriamente

if random.randint(1, 50) == 1: # Probabilidad de generar un nuevo bloque

add_enemy()

# Eliminar enemigos que salgan de la pantalla y aumentar puntuación

for enemy in enemy_list:

if enemy[1] > HEIGHT:

enemy_list.remove(enemy)

score += 1 # Aumentar puntuación

# Aumentar dificultad con el tiempo

if score % 10 == 0 and score > 0:


enemy_speed += 0.1 # Los bloques caen más rápido

# Comprobar colisión con el jugador

for enemy in enemy_list:

if (

player_x < enemy[0] + enemy_size and

player_x + player_size > enemy[0] and

player_y < enemy[1] + enemy_size and

player_y + player_size > enemy[1]

):

running = False # Fin del juego

# Dibujar el jugador

pygame.draw.rect(screen, BLUE, (player_x, player_y, player_size, player_size))

# Dibujar enemigos

for enemy in enemy_list:

pygame.draw.rect(screen, RED, (enemy[0], enemy[1], enemy_size, enemy_size))

# Mostrar puntuación

font = pygame.font.SysFont("Arial", 24)

score_text = font.render(f"Puntuación: {score}", True, BLACK)

screen.blit(score_text, (10, 10))

pygame.display.update() # Actualizar pantalla

clock.tick(30) # FPS

pygame.quit()

You might also like