You are on page 1of 3

BTVN

Bài 1:
import turtle

# Khởi tạo turtle và màn hình vẽ


t = turtle.Turtle()
screen = turtle.Screen()

# Hàm để vẽ một hình bát giác


def draw_polygon(sides, length):
for _ in range(sides):
t.forward(length)
t.right(360 / sides)

# Hàm để vẽ một mặt bên của hình trụ


def draw_side(sides, length):
for _ in range(2):
draw_polygon(sides, length)
t.forward(length)
t.right(180)

# Hàm để vẽ hình trụ bát giác


def draw_cone(sides, length, height):
# Vẽ các mặt bên
for _ in range(height):
draw_side(sides, length)
t.penup()
t.right(90)
t.forward(length)
t.left(90)
t.pendown()
t.forward(1)
t.left(90)
t.forward(length)
t.left(180)
t.penup()
t.forward(1)
t.pendown()
# Vẽ đáy
t.left(90)
draw_polygon(sides, length)
t.penup()
t.left(90)
t.forward(height)
t.left(90)
t.pendown()
draw_polygon(sides, length)

# Vẽ hình trụ bát giác với 6 cạnh và kích thước mặt cạnh là 50, chiều cao là 100
draw_cone(6, 50, 100)

# Đóng turtle khi kết thúc


screen.mainloop()
Bài 2:
import turtle

# Khởi tạo turtle và màn hình vẽ


t = turtle.Turtle()
screen = turtle.Screen()

# Hàm để vẽ ellipse
def draw_ellipse(a, b):
t.penup()
t.goto(a, 0)
t.pendown()
t.speed(0)
for _ in range(2):
t.circle(a, 90)
t.circle(b, 90)

# Hàm để quay ellipse


def rotate_ellipse(angle):
t.left(angle)

# Hàm để di chuyển ellipse sang trái


def move_left(distance):
t.penup()
t.backward(distance)
t.pendown()

# Vẽ ellipse
draw_ellipse(30, 20)

# Quay ellipse
rotate_ellipse(45)

# Di chuyển sang trái


move_left(50)

# Đóng turtle khi kết thúc


screen.mainloop()
Bài 3:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

# Khởi tạo Pygame


pygame.init()

# Kích thước cửa sổ


width, height = 800, 600

# Khởi tạo cửa sổ đồ họa OpenGL


pygame.display.set_mode((width, height), DOUBLEBUF|OPENGL)

# Thiết lập màn hình


glViewport(0, 0, width, height)

# Thiết lập ma trận chiếu


glMatrixMode(GL_PROJECTION)
gluPerspective(45, (width/height), 0.1, 50.0)
# Đặt vị trí người quan sát
glMatrixMode(GL_MODELVIEW)
gluLookAt(5, 5, 5, 0, 0, 0, 0, 1, 0)

# Hàm vẽ hình lập phương


def draw_cube():
glBegin(GL_QUADS)
# Mặt trước
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)
# Mặt bên trái
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)
# Mặt dưới
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)
glEnd()

# Vòng lặp chính


while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

# Xóa màn hình và vẽ lại


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

# Vẽ hình lập phương


glLoadIdentity()
glRotatef(1, 0, 0, 1) # Quay quanh trục z
glTranslatef(0.01, 0, 0) # Di chuyển theo trục x
draw_cube()

pygame.display.flip()
pygame.time.wait(10)

BT trên lớp:

You might also like