como puedes mejorarlo import pygame import random Inicializar Pygame pygame.init()…
- User
como puedes mejorarlo
import pygame import random
Inicializar Pygame
pygame.init()
Dimensiones de la pantalla
WIDTH, HEIGHT = 800, 600 SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Simulación del Bosón de Higgs")
Definir colores
WHITE = (255, 255, 255) BLUE = (50, 150, 255) RED = (255, 100, 100) GREEN = (100, 255, 100)
Clase Partícula
class Particle: def init(self): # Posición y velocidad utilizando vectores self.pos = pygame.math.Vector2(random.uniform(0, WIDTH), random.uniform(0, HEIGHT)) angle = random.uniform(0, 360) speed = random.uniform(1, 3) self.vel = pygame.math.Vector2(speed, 0).rotate(angle) self.mass = 1 # Masa inicial self.size = 5 # Tamaño inicial self.color = BLUE # Color inicial
def update(self, higgs_field_rect): # Verificar si la partícula está en el campo de Higgs if higgs_field_rect.collidepoint(self.pos.x, self.pos.y): # Interacción con el campo de Higgs self.mass = 5 self.size = 10 self.color = RED self.vel *= 0.95 # Reducir la velocidad else: # Sin interacción self.mass = 1 self.size = 5 self.color = BLUE self.vel *= 1.05 # Aumentar ligeramente la velocidad # Limitar la velocidad máxima if self.vel.length() > 4: self.vel.scale_to_length(4) # Actualizar posición self.pos += self.vel # Manejar los bordes de la pantalla if self.pos.x <= 0 or self.pos.x >= WIDTH: self.vel.x *= -1 if self.pos.y <= 0 or self.pos.y >= HEIGHT: self.vel.y *= -1 def draw(self, screen): pygame.draw.circle(screen, self.color, (int(self.pos.x), int(self.pos.y)), self.size)Crear el campo de Higgs en el centro de la pantalla
higgs_field_rect = pygame.Rect(WIDTH // 4, HEIGHT // 4, WIDTH // 2, HEIGHT // 2)
Crear un grupo de partículas
particles = [Particle() for _ in range(50)]
Variables para controlar el movimiento del campo de Higgs
moving_higgs_field = False offset_x = 0 offset_y = 0
Bucle principal
running = True clock = pygame.time.Clock()
while running: clock.tick(60) # 60 FPS
for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Event handling for moving the Higgs field elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Botón izquierdo del ratón if higgs_field_rect.collidepoint(event.pos): moving_higgs_field = True mouse_x, mouse_y = event.pos offset_x = higgs_field_rect.x - mouse_x offset_y = higgs_field_rect.y - mouse_y elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: moving_higgs_field = False elif event.type == pygame.MOUSEMOTION: if moving_higgs_field: mouse_x, mouse_y = event.pos higgs_field_rect.x = mouse_x + offset_x higgs_field_rect.y = mouse_y + offset_y # Actualizar partículas for particle in particles: particle.update(higgs_field_rect) # Dibujar todo SCREEN.fill(WHITE) # Dibujar el campo de Higgs pygame.draw.rect(SCREEN, GREEN, higgs_field_rect, 2) # Dibujar partículas for particle in particles: particle.draw(SCREEN) pygame.display.flip() - Assistant