#androidappdevelopment

Published: 27 April 2023
on channel: TR Tech BD
213
7

‪@thaque26‬

code is below 👇,
=======≠========
import pygame
import math

Set up the display
pygame.init()
width = 500
height = 500
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("3D Cube")

Define the vertices of the cube
vertices = [[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, 1],
[1, -1, 1],
[1, 1, 1],
[-1, 1, 1]]

Scale the vertices
scale_factor = 2
vertices = [[scale_factor * x, scale_factor * y, scale_factor * z] for x, y, z in vertices]

Define the edges of the cube
edges = [[0, 1],
[1, 2],
[2, 3],
[3, 0],
[4, 5],
[5, 6],
[6, 7],
[7, 4],
[0, 4],
[1, 5],
[2, 6],
[3, 7]]

Define the center of the Pydroid 3 screen
center = [width//2, height//2]

Define the angle of rotation
angle = 0

Define the main game loop
while True:
Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

Clear the screen
display.fill((255, 255, 255))

Rotate the vertices
sin_a = math.sin(angle)
cos_a = math.cos(angle)
rotated_vertices = []
for vertex in vertices:
x = vertex[0]
y = vertex[1]
z = vertex[2]
Rotate around the x-axis
y, z = y * cos_a - z * sin_a, y * sin_a + z * cos_a
Rotate around the y-axis
x, z = x * cos_a - z * sin_a, x * sin_a + z * cos_a
Rotate around the z-axis
x, y = x * cos_a - y * sin_a, x * sin_a + y * cos_a
Project onto the 2D screen
f = 200 / (z + 4)
x, y = x * f, y * f
Shift the center of the cube to the center of the screen
x, y = x + center[0], y + center[1]
rotated_vertices.append((x, y))

Draw the edges
for edge in edges:
start = rotated_vertices[edge[0]]
end = rotated_vertices[edge[1]]
pygame.draw.line(display, (0, 0, 0), start, end, 2)

Update the angle of rotation
angle += 0.01

Update the display
pygame.display.update()


On this page of the site you can watch the video online #androidappdevelopment with a duration of hours minute second in good quality, which was uploaded by the user TR Tech BD 27 April 2023, share the link with friends and acquaintances, this video has already been watched 213 times on youtube and it was liked by 7 viewers. Enjoy your viewing!