馃弫 MVA.F1: GP Nocturno de Berazategui V.10 2025.py (Asistido por NOvaPy)
(vueltas de entrenamiento)
'''
Si prefer铆s la amena interfaz de un navegador web podes correr ya mismo en: <<< LINK >>>
'''
# convencido de la l贸gica del caos
import pygame
import math
import time
import sys
# Inicializaci贸n
pygame.init()
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
TRACK_MARGIN = 100
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("F1 GP Berazategui V10 2025 (Nocturno)")
# Colores
BLACK = (0, 0, 0)
GRAY = (70, 70, 70)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (50, 150, 255)
ORANGE = (255, 140, 0)
font = pygame.font.SysFont("Courier", 20)
# Estado inicial del Monoplaza
car_x = 350 # centro horizontal
car_y = 50 # arriba de la pista
car_angle = 180
car_speed = 0
car_acceleration = 0.1
car_max_speed = 4
car_turn_speed = 3
car_friction = 0.05
car_color = RED
# Estado de la carrera
lap_start_time = None
lap_times = []
laps = 0
passed_start_line = False
race_started = False
crash_count = 0 # Contador de despistes
clock = pygame.time.Clock()
def draw_track():
screen.fill(GRAY)
pygame.draw.rect(screen, BLACK, (100, 100, 600, 400))
def draw_start_line():
pygame.draw.line(screen, WHITE,
(342, 0), (342, 100), 4)
def draw_car():
size = 10
angle_rad = math.radians(car_angle)
tip = (car_x + math.cos(angle_rad) * size, car_y + math.sin(angle_rad) * size)
left = (car_x + math.cos(angle_rad + math.radians(140)) * size, car_y + math.sin(angle_rad + math.radians(140)) * size)
right = (car_x + math.cos(angle_rad - math.radians(140)) * size, car_y + math.sin(angle_rad - math.radians(140)) * size)
pygame.draw.polygon(screen, car_color, [tip, left, right])
def draw_ui():
ui_x, ui_y = TRACK_MARGIN + 20, TRACK_MARGIN + 20
lap_text = font.render(f"Vueltas: {laps}", True, WHITE)
screen.blit(lap_text, (ui_x, ui_y))
if lap_start_time and race_started:
current_time = time.time() - lap_start_time
timer_text = font.render(f"Tiempo vuelta: {current_time:.2f}s", True, WHITE)
screen.blit(timer_text, (ui_x, ui_y + 30))
if lap_times:
best = min(lap_times)
best_text = font.render(f"Best lap: {best:.2f}s", True, BLUE)
screen.blit(best_text, (ui_x, ui_y + 60))
carcoor_text = font.render(f"Car: (X:{car_x}) (Y:{car_y})", True, WHITE)
screen.blit(carcoor_text, (ui_x, ui_y + 90))
crash_text = font.render(f"Despistes: {crash_count}", True, RED)
screen.blit(crash_text, (ui_x, ui_y + 120))
controls_text1 = font.render("W/↑: Acelerar S/↓: Frenar", True, WHITE)
controls_text2 = font.render("A/←: Izq D/→: Der", True, WHITE)
controls_text3 = font.render("ESPACIO: Freno", True, WHITE)
screen.blit(controls_text1, (ui_x, TRACK_MARGIN + 200))
screen.blit(controls_text2, (ui_x, TRACK_MARGIN + 230))
screen.blit(controls_text3, (ui_x, TRACK_MARGIN + 260))
def draw_radar():
radar_width, radar_height = 60, 40
radar_x, radar_y = SCREEN_WIDTH - radar_width - 137, 440
pygame.draw.rect(screen, WHITE, (radar_x-10, radar_y-10, radar_width+20, radar_height+20))
pygame.draw.rect(screen, (30, 30, 30), (radar_x, radar_y, radar_width, radar_height))
pygame.draw.rect(screen, WHITE, (radar_x, radar_y, radar_width, radar_height), 1)
scaled_x = (car_x - 100) / 10
scaled_y = (car_y - 100) / 10
radar_car_x = radar_x + scaled_x
radar_car_y = radar_y + scaled_y
pygame.draw.circle(screen, RED, (int(radar_car_x), int(radar_car_y)), 2)
def handle_keys():
global car_speed, car_angle
keys = pygame.key.get_pressed()
if keys[pygame.K_w] or keys[pygame.K_UP]:
car_speed = min(car_speed + car_acceleration, car_max_speed)
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
car_speed = max(car_speed - car_acceleration, -car_max_speed / 2)
if keys[pygame.K_SPACE]:
car_speed *= 0.85
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
car_angle -= car_turn_speed
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
car_angle += car_turn_speed
def move_car():
global car_x, car_y, car_speed
angle_rad = math.radians(car_angle)
car_x += math.cos(angle_rad) * car_speed
car_y += math.sin(angle_rad) * car_speed
if car_speed > 0:
car_speed -= car_friction
elif car_speed < 0:
car_speed += car_friction
def check_bounds():
# Devuelve True si el auto est谩 fuera de la pista v谩lida (transitable)
en_pista = (
(0 <= car_x <= 800 and 0 <= car_y <= 100) or # banda superior
(0 <= car_x <= 100 and 0 <= car_y <= 600) or # banda izquierda
(700 <= car_x <= 800 and 0 <= car_y <= 600) or # banda derecha
(0 <= car_x <= 800 and 500 <= car_y <= 600) # banda inferior
)
return not en_pista
def check_lap():
global lap_start_time, lap_times, laps, passed_start_line
if 350 < car_x < 355 and 0 < car_y < 100 and car_speed > 0:
if not passed_start_line and lap_start_time:
lap_time = time.time() - lap_start_time
lap_times.append(lap_time)
laps += 1
lap_start_time = time.time()
passed_start_line = True
else:
passed_start_line = False
def countdown():
for i, color in zip([5, 4, 3, 2, 1], [RED, RED, ORANGE, ORANGE, GREEN]):
draw_track()
draw_start_line()
draw_car()
label = font.render(str(i), True, color)
screen.blit(label, (SCREEN_WIDTH // 2 - 10, SCREEN_HEIGHT // 2 - 10))
pygame.display.flip()
time.sleep(1)
def game_loop():
global lap_start_time, race_started, crash_count
countdown()
lap_start_time = time.time()
race_started = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
handle_keys()
move_car()
if check_bounds():
print("¡Fuera de pista! Excediste los l铆mites prohibidos.")
crash_count += 1 # Incrementa el contador de despistes
# Reposiciona el Monoplaza en la Recta Principal
global car_x, car_y, car_angle, car_speed
car_x = 350
car_y = 50
car_angle = 180 # Mirando hacia abajo (0 real ser铆a hacia la derecha en trigonometr铆a, pero ac谩 el 180 apunta hacia abajo)
car_speed = 0
countdown() # Reinicia la cuenta regresiva
lap_start_time = time.time() # Reinicia el tiempo de vuelta
check_lap()
draw_track()
draw_start_line()
draw_car()
draw_ui()
draw_radar() # 馃憟 NUEVA Y APASIONANTE FUNCION TELEMETRICA
pygame.display.flip()
clock.tick(60)
# Iniciar
game_loop()
Comentarios
Publicar un comentario
Te invitamos a comentar y compartir tus impresiones y pensamientos sobre este art铆culo