Files
PythonProject/GUI/pantalla.py
2025-03-04 21:12:17 +01:00

62 lines
2.0 KiB
Python

import pygame
# Inicializar pygame
pygame.init()
# Configuración de la pantalla
ANCHO, ALTO = 600, 400 # Puedes cambiar el tamaño
TERCIO_ANCHO = ANCHO // 3
screen = pygame.display.set_mode((ANCHO, ALTO))
pygame.display.set_caption("Cambio de colores con teclas")
# Fuente para el texto
font = pygame.font.Font(None, 36)
# Variables para controlar la iluminación
i_presionado = False
d_presionado = False
espacio_presionado = False
# Bucle principal
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
i_presionado = True
elif event.key == pygame.K_d:
d_presionado = True
elif event.key == pygame.K_SPACE:
espacio_presionado = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_i:
i_presionado = False
elif event.key == pygame.K_d:
d_presionado = False
elif event.key == pygame.K_SPACE:
espacio_presionado = False
# Fondo en blanco
screen.fill((255, 255, 255))
# Dibujar los rectángulos si se presionan teclas
if i_presionado:
pygame.draw.rect(screen, (255, 0, 0), (0, 0, TERCIO_ANCHO, ALTO))
text_surface = font.render("Izquierda", True, (0, 0, 0))
screen.blit(text_surface, (TERCIO_ANCHO // 2 - text_surface.get_width() // 2, 20))
if d_presionado:
pygame.draw.rect(screen, (0, 255, 0), (2 * TERCIO_ANCHO, 0, TERCIO_ANCHO, ALTO))
text_surface = font.render("Derecha", True, (0, 0, 0))
screen.blit(text_surface, (2 * TERCIO_ANCHO + TERCIO_ANCHO // 2 - text_surface.get_width() // 2, 20))
# Mostrar texto solo cuando se presiona espacio
if espacio_presionado:
text_surface = font.render("None", True, (0, 0, 0))
screen.blit(text_surface, (ANCHO // 2 - text_surface.get_width() // 2, ALTO // 2))
pygame.display.flip()
pygame.quit()