编辑代码

import pygame
import sys
from pygame.locals import *
 
# 初始化pygame
pygame.init()
 
# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height), 0, 32)
 
# 设置颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
 
# 填充背景色
screen.fill(WHITE)
 
# 正方形的顶点坐标(在3D空间中)
vertices = [
    (100, 100, 100),  # 点1 (左上角)
    (200, 100, 100),  # 点2 (右上角)
    (200, 200, 100),  # 点3 (右下角)
    (100, 200, 100),  # 点4 (左下角)
    (150, 150, 200)   # 点5 (顶部中间)
]
 
# 绘制边和面(这里我们只画一个面和一个顶点到顶部)
pygame.draw.line(screen, BLUE, vertices[0], vertices[1], 5)  # AB边
pygame.draw.line(screen, BLUE, vertices[1], vertices[2], 5)  # BC边
pygame.draw.line(screen, BLUE, vertices[2], vertices[3], 5)  # CD边
pygame.draw.line(screen, BLUE, vertices[3], vertices[0], 5)  # DA边
pygame.draw.line(screen, RED, vertices[4], vertices[1], 5)   # A到顶部点5的线
pygame.draw.line(screen, RED, vertices[4], vertices[2], 5)   # B到顶部点5的线
pygame.draw.line(screen, RED, vertices[4], vertices[3], 5)   # C到顶部点5的线
pygame.draw.line(screen, RED, vertices[4], vertices[0], 5)   # D到顶部点5的线
pygame.draw.circle(screen, GREEN, vertices[4], 10)  # 绘制顶部点5(可以看作是立方体的一个顶点)
 
# 更新