用户:
XCookie查看:0 回复:0 评论:0 创建时间:2021-08-01T19:02:36
【作品展示】

【作品介绍】
基于Pygame的3D测试
【作品源代码】
import pygame
pygame.init()
width, height = 1080, 720
window = pygame.display.set_mode((width, height))
exdiv = lambda self, other : 0 if other == 0 else self // other
twed = lambda point, f, screen, cam : (
int(exdiv((point[0] + cam[0]) * f, (point[2] + cam[2])) + screen[0] // 2),
int(exdiv((point[1] + cam[1]) * f, (point[2] + cam[2])) + screen[1] // 2)
)
f = 500
cam = [0, 0, -2.5]
move = [0, 0, 0]
speed = .005
points3d = [
[1, 0, 0],
[1, 0, 1],
[0, 0, 1],
[0, 0, 0],
[1, 1, 0],
[1, 1, 1],
[0, 1, 1],
[0, 1, 0]
]
lines = [
[1, 2], [1, 4], [1, 5],
[2, 3], [2, 6],
[3, 4], [3, 7],
[4, 8],
[5, 6], [5, 8],
[6, 7],
[7, 8]
]
def render():
points2d = [ twed(point, f, (width, height), cam) for point in points3d ]
window.fill((0, 0, 0))
for point2d in points2d:
pygame.draw.circle(window, (255, 255, 255), point2d, 10, 0)
for line in lines:
pygame.draw.aaline(window, (255, 255, 255), points2d[line[0] - 1], points2d[line[1] - 1], 5)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
cam[0], cam[1] = -(event.pos[0] - width / 2) * speed - .5, -(event.pos[1] - height / 2) * speed - .5
render()
if event.type == pygame.QUIT:
pygame.quit(); exit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn