Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- from OpenGL.GL import *
- from OpenGL.GLU import *
- import math
- # Initialize Pygame and set OpenGL attributes for multisampling
- pygame.init()
- pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1)
- # Increase multisample samples for improved anti-aliasing
- pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 16)
- width, height = 800, 600
- screen = pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL)
- pygame.display.set_caption("Extra Smooth OpenGL Spinning Circles with Zoom")
- # Setup a 2D orthographic projection
- glViewport(0, 0, width, height)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- gluOrtho2D(0, width, 0, height)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- # Enable anti-aliasing features
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- glEnable(GL_LINE_SMOOTH)
- glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
- glEnable(GL_MULTISAMPLE)
- # Parameters for the arcs
- center = (width / 2, height / 2)
- base_radius = 40
- spacing = 20
- num_circles = 10
- rotation_angles = [0] * num_circles
- base_rotation_speed = 1.0 # degrees per frame
- gap_angle = 25 # gap in degrees
- line_color = (0, 255, 255) # Cyan
- line_width = 3
- clockwise = False # True means rotating clockwise
- # Radii and rotation speeds for each circle
- radii = [base_radius + i * spacing for i in range(num_circles)]
- rotation_speeds = [base_rotation_speed / (1 + i * 0.3) for i in range(num_circles)]
- # Zoom factor (1.0 means no zoom)
- zoom = 1.0
- def draw_arc(cx, cy, radius, start_angle, end_angle, segments):
- """
- Draw an arc using GL_LINE_STRIP from start_angle to end_angle (in radians)
- with a specified number of segments.
- """
- glBegin(GL_LINE_STRIP)
- for i in range(segments + 1):
- t = start_angle + (end_angle - start_angle) * i / segments
- x = cx + radius * math.cos(t)
- y = cy + radius * math.sin(t)
- glVertex2f(x, y)
- glEnd()
- clock = pygame.time.Clock()
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == QUIT:
- running = False
- # Use the mouse wheel for zooming
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 4: # Scroll up -> zoom in
- zoom *= 1.1
- elif event.button == 5: # Scroll down -> zoom out
- zoom /= 1.1
- # Use the keyboard for zooming
- elif event.type == pygame.KEYDOWN:
- if event.key in (pygame.K_PLUS, pygame.K_KP_PLUS, pygame.K_EQUALS):
- zoom *= 1.1
- elif event.key in (pygame.K_MINUS, pygame.K_KP_MINUS):
- zoom /= 1.1
- # Update rotation angles; subtract to rotate clockwise
- for i in range(num_circles):
- if clockwise:
- rotation_angles[i] = (rotation_angles[i] - rotation_speeds[i]) % 360
- else:
- rotation_angles[i] = (rotation_angles[i] + rotation_speeds[i]) % 360
- # Clear the screen with a black background
- glClearColor(0, 0, 0, 1)
- glClear(GL_COLOR_BUFFER_BIT)
- # Apply the zoom transformation:
- # 1. Reset the modelview matrix.
- # 2. Translate to the center.
- # 3. Scale by the zoom factor.
- # 4. Translate back.
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glTranslatef(center[0], center[1], 0)
- glScalef(zoom, zoom, 1)
- glTranslatef(-center[0], -center[1], 0)
- # Set line properties
- glLineWidth(line_width)
- glColor3f(line_color[0] / 255.0,
- line_color[1] / 255.0,
- line_color[2] / 255.0)
- # Draw each arc (circle with a gap)
- for radius, angle in zip(radii, rotation_angles):
- start_angle = math.radians(angle)
- arc_span = 2 * math.pi - math.radians(gap_angle)
- end_angle = start_angle + arc_span
- # More segments produce a smoother arc
- segments = max(50, int((arc_span * radius) / 2))
- draw_arc(center[0], center[1], radius, start_angle, end_angle, segments)
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement