Absolute Zero Reasoner

https://news.ycombinator.com/rss Hits: 10
Summary

import pygame import sys import math import random # Initialize Pygame pygame.init() # Screen dimensions SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Bouncing Balls in Spinning Hexagon") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) # Ball properties BALL_RADIUS = 10 BALL_COUNT = 10 balls = [] # Hexagon properties HEX_SIDES = 6 HEX_RADIUS = 200 HEX_CENTER = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) HEX_SPIN_SPEED = 0.01 # Gravity and friction GRAVITY = 0.5 FRICTION = 0.99 def create_balls(): for _ in range(BALL_COUNT): x = SCREEN_WIDTH // 2 + (random.randint(-100, 100)) y = SCREEN_HEIGHT // 2 + (random.randint(-100, 100)) vx = random.uniform(-3, 3) vy = random.uniform(-3, 3) balls.append([x, y, vx, vy]) def draw_hexagon(angle): points = [] for i in range(HEX_SIDES): angle_rad = math.radians(i * 60 - angle) x = HEX_CENTER[0] + HEX_RADIUS * math.cos(angle_rad) y = HEX_CENTER[1] + HEX_RADIUS * math.sin(angle_rad) points.append((int(x), int(y))) return points def check_collision(ball, points): px, py = ball[:2] for i in range(len(points)): j = (i + 1) % len(points) if is_point_in_triangle(px, py, points[i], points[j], HEX_CENTER): return True return False def is_point_in_triangle(px, py, p1, p2, p3): def sign(p1, p2, p3): return (p1[0] - p3[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p3[1]) b1 = sign((px, py), p1, p2) < 0 b2 = sign((px, py), p2, p3) < 0 b3 = sign((px, py), p3, p1) < 0 return ((b1 == b2) and (b2 == b3)) def update_ball(ball, points): x, y, vx, vy = ball x += vx y += vy vy += GRAVITY if check_collision(ball, points): # Bounce logic normal_vector = get_normal_vector(ball, points) reflect_vector(normal_vector, ball) # Apply friction vx *= FRICTION vy *= FRICTION ball[0] = x ball[1] = y ball[2] = vx ball[3] = vy def get_normal_vector(ball, points): px, py = ball[:2] closest_point = min(points, key=lambda point: math.hypot(point[0] - px, point[1] ...

First seen: 2025-05-12 01:24

Last seen: 2025-05-12 10:26