Python Heart Animation

 import pygame

from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * import math def draw_heart(): glBegin(GL_TRIANGLES) for i in range(0, 360, 1): theta1 = math.radians(i) theta2 = math.radians(i + 1) x1 = 16 * math.sin(theta1) ** 3 y1 = 13 * math.cos(theta1) - 5 * math.cos(2 * theta1) - 2 * math.cos(3 * theta1) - math.cos(4 * theta1) z1 = 0 x2 = 16 * math.sin(theta2) ** 3 y2 = 13 * math.cos(theta2) - 5 * math.cos(2 * theta2) - 2 * math.cos(3 * theta2) - math.cos(4 * theta2) z2 = 0 x3 = 0 y3 = 0 z3 = -20 # Depth of the heart shape # Assign colors based on sections of the heart shape if i < 90:


glColor3f(1, 0, 0) # Red color for the top section elif i < 180: glColor3f(0, 1, 0) # Green color for the left section elif i < 270: glColor3f(0, 0, 1) # Blue color for the right section else: glColor3f(1, 1, 0) # Yellow color for the bottom section glVertex3f(x1, y1, z1) glVertex3f(x2, y2, z2) glVertex3f(x3, y3, z3) glEnd() pygame.init() display = (1920, 1080) pygame.display.set_mode(display, DOUBLEBUF | OPENGL) gluPerspective(115, (display[0] / display[1]), 0.1, 50.0) glTranslatef(0.0, 0.0, -30) # Move the heart away from the camera for better visibility while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() glRotatef(1, 3, 1, 1) # Rotate the heart shape glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) draw_heart() pygame.display.flip() pygame.time.wait(10)

Comments

Popular posts from this blog

Amazing Design on Python turtle