Python Project – Make Spinning Donut using Python

1332
0
Python Project Make spinning donut using python

In this Python Project, we have created a 3d spinning donut using binary numbers such as 0’s and 1’s. 

Source Code for this Python Project

import pygame
import math
import colorsys

pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)
hue = 0

WIDTH = 1920
HEIGHT = 1080

x_start, y_start = 0, 0

x_separator = 10
y_separator = 20


rows = HEIGHT // y_separator
columns = WIDTH // x_separator
screen_size = rows * columns
x_offset = columns / 2
y_offset = rows / 2

# rotating animation
A, B = 0, 0

spacing1 = 10
spacing2 = 2

chars = "101010101010"  # luminance index

screen = pygame.display.set_mode((WIDTH, HEIGHT))

display_surface = pygame.display.set_mode((WIDTH, HEIGHT))
# display_surface = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('Donut')
font = pygame.font.SysFont('Arial', 18, bold=True)


def hsv2rgb(h, s, v):
    return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h, s, v))


def text_display(letter, x_start, y_start):
    text = font.render(str(letter), True, hsv2rgb(hue, 1, 1))
    display_surface.blit(text, (x_start, y_start))


run = True
while run:

    screen.fill((black))

    z = [0] * screen_size  # Donut. Fills donut space
    b = [' '] * screen_size  # Background. Fills empty space

    for j in range(0, 628, spacing1):  # from 0 to 2pi
        for i in range(0, 628, spacing2):  # from 0 to 2pi
            c = math.sin(i)
            d = math.cos(j)
            e = math.sin(A)
            f = math.sin(j)
            g = math.cos(A)
            h = d + 2
            D = 1.5 / (c * h * e + f * g + 5)
            l = math.cos(i)
            m = math.cos(B)
            n = math.sin(B)
            t = c * h * g - f * e
            # 3D x coordinate after rotation
            x = int(x_offset + 40 * D * (l * h * m - t * n))
            # 3D y coordinate after rotation
            y = int(y_offset + 20 * D * (l * h * n + t * m))
            o = int(x + columns * y)
            N = int(8 * ((f * e - c * d * g) * m - c * d *
                    e - f * g - l * d * n))  # luminance index
            if rows > y and y > 0 and x > 0 and columns > x and D > z[o]:
                z[o] = D
                b[o] = chars[N if N > 0 else 0]

    if y_start == rows * y_separator - y_separator:
        y_start = 0

    for i in range(len(b)):
        A += 0.00001  # for faster rotation change to bigger value
        B += 0.000002  # for faster rotation change to bigger value
        if i == 0 or i % columns:
            text_display(b[i], x_start, y_start)
            x_start += x_separator
        else:
            y_start += y_separator
            x_start = 0
            text_display(b[i], x_start, y_start)
            x_start += x_separator

    pygame.display.update()

    hue += 0.005

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
Python

Code Explanation

This Python code uses the Pygame library to create a spinning donut animation with changing colors.

First, the necessary libraries are imported: pygame, math, and colorsys. Then, some colors and screen dimensions are defined, followed by some variables that are used to set up the animation, such as the starting position of the text characters and the spacing between them.

A function is defined to convert a color from HSV to RGB format using the colorsys library. Another function is defined to display a text character on the screen at a given position with a given color.

The main loop of the animation starts with filling the screen with black. Two lists are created to represent the donut space (z) and the background (b). These lists will be filled with characters that represent the luminance index of the donut and the background.

Two nested loops iterate through values from 0 to 2pi, calculating various trigonometric values and using them to calculate the x and y coordinates of each point on the donut. These coordinates are then used to calculate the index of the corresponding character in the b and z lists. The D value is used to determine whether a given point is closer to the observer than other points and should be displayed, or whether it is further away and should be hidden. The N value is used to determine the luminance of each point on the donut.

Next, the text_display function is called for each character in the b list. This function takes the character, its position, and the current color of the animation, and displays the character on the screen with the appropriate color. The hue value is then incremented to change the color of the animation.

Finally, the Pygame event loop is checked for any events, such as quitting the program or pressing the escape key. If either of these events occurs, the run variable is set to False, and the program exits.

Demo Video

Kindly Subscribe to our Youtube channel for more awesome python projects. Let me know in the comments if you want any specific python project.

ScreenShots

Python Project - Make Spinning Donut using Python
Python Project - Make Spinning Donut using Python

If you liked this, click the 💚 below so other people will see this here on Xalgord. Please let me know if you have any comments! Feel free to connect on Instagram.

xalgord
WRITTEN BY

xalgord

Constantly learning & adapting to new technologies. Passionate about solving complex problems with code. #programming #softwareengineering

Leave a Reply