1. Make 3D Colorful Cube Using Python:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0,0.0,-5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glBegin(GL_QUADS)
glColor3f(1.0,0.0,0.0)
glVertex3f(1.0, 1.0, -1.0)
glVertex3f(-1.0, 1.0, -1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(1.0, 1.0, 1.0)
glColor3f(0.0,1.0,0.0)
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(1.0, -1.0, -1.0)
glColor3f(0.0,0.0,1.0)
glVertex3f(1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, 1.0)
glColor3f(1.0,1.0,0.0)
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(-1.0, 1.0, -1.0)
glVertex3f(1.0, 1.0, -1.0)
glEnd()
pygame.display.flip()
pygame.time.wait(10)
Video Link:
Output:
2.Make Home using Python:
import pygame
# Initialize Pygame
pygame.init()
# Set the dimensions of the window
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
WINDOW_SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)
# Create the window
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set the caption of the window
pygame.display.set_caption("My Home Design")
# Define the colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GRAY = (128, 128, 128)
# Define the dimensions of the home
HOME_WIDTH = 400
HOME_HEIGHT = 300
HOME_X = (WINDOW_WIDTH - HOME_WIDTH) / 2
HOME_Y = (WINDOW_HEIGHT - HOME_HEIGHT) / 2
# Define the dimensions of the roof
ROOF_WIDTH = HOME_WIDTH + 100
ROOF_HEIGHT = HOME_HEIGHT / 2
# Set the loop to continue until the user quits
done = False
# Loop until the user quits
while not done:
# Handle events (e.g., clicking the close button)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Fill the screen with white
screen.fill(WHITE)
# Draw the home
pygame.draw.rect(screen, GRAY, (HOME_X, HOME_Y, HOME_WIDTH, HOME_HEIGHT), 2)
# Draw the roof
pygame.draw.polygon(screen, GRAY, [(HOME_X - 50, HOME_Y), (HOME_X + HOME_WIDTH + 50, HOME_Y), (HOME_X + HOME_WIDTH / 2, HOME_Y - ROOF_HEIGHT)], 2)
# Draw the windows
pygame.draw.rect(screen, BLUE, (HOME_X + HOME_WIDTH / 4, HOME_Y + HOME_HEIGHT / 4, HOME_WIDTH / 4, HOME_HEIGHT / 4))
pygame.draw.rect(screen, BLUE, (HOME_X + HOME_WIDTH / 2, HOME_Y + HOME_HEIGHT / 4, HOME_WIDTH / 4, HOME_HEIGHT / 4))
pygame.draw.rect(screen, BLUE, (HOME_X + HOME_WIDTH / 4, HOME_Y + HOME_HEIGHT / 2, HOME_WIDTH / 4, HOME_HEIGHT / 4))
pygame.draw.rect(screen, BLUE, (HOME_X + HOME_WIDTH / 2, HOME_Y + HOME_HEIGHT / 2, HOME_WIDTH / 4, HOME_HEIGHT / 4))
# Update the screen
pygame.display.update()
# Quit Pygame
pygame.quit()
Video Link:
Output:
3. Make 3D Cube Using Python:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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)
)
edges = (
(0, 1),
(0, 3),
(0, 4),
(2, 1),
(2, 3),
(2, 7),
(6, 3),
(6, 4),
(6, 7),
(5, 1),
(5, 4),
(5, 7)
)
def Cube():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)
if __name__ == "__main__":
main()
Video Link:
Output:
You Also Read This Topics :-
1. Battle of the AI Language Models: OpenAI's Chat GPT vs Google's BARD
2. 10 Creative and Mind-Blowing Python Turtle Examples to Inspire Your Next Project
3. Discover the Fun and Interactive World of Turtle Graphics in Python
6. Transforming Your Home with IoT: Discover the Benefits and Challenges of Smart Home Technology
7. Take Your Python Skills to the Next Level with These Advanced Turtle Examples ->
10. Revolutionizing Search: How Microsoft Bing+AI is Personalizing Your Results