gpt4 book ai didi

python - 扩展在 pygame 中制作的线框 3d 形状

转载 作者:行者123 更新时间:2023-12-04 07:26:21 24 4
gpt4 key购买 nike

所以我有这个 draw_rectangular_prism使用 pygame 绘制 9 行的函数。我在原始点上加了 100,所以它们不会在左上角。我想让直角棱镜变大,这样它就不会全部聚集在一起。
这是我的功能:

def draw_rectangular_prism(color=BLACK, display_surface=display_surface, width=3): #using lines    (5, 0) (15, 0) (0, 2) (10, 2) (0, 10) (10, 10) (15, 8)
draw_line((105, 100), (115, 100)) # (5, 0) : (15, 0)
draw_line((100, 102), (105, 100)) # (0, 2) : (5, 0)
draw_line((100, 102), (100, 110)) # (0, 2) : (0, 10)
draw_line((100, 110), (110, 110)) # (0, 10) : (10, 10)
draw_line((110, 110), (115, 108)) # (10, 10) : (15, 8)
draw_line((115, 108), (115, 100)) # (15, 8) : (15, 0)
draw_line((100, 102), (110, 102)) # (0, 2) : (10, 2)
draw_line((110, 102), (110, 110)) # (10, 2) : (10, 10)
draw_line((110, 102), (115, 100)) # (10, 2) : (15, 0)

最佳答案

Pygame 没有缩放坐标的功能。你必须改变你自己的坐标。
draw_rectangular_prism 添加比例和原点参数功能:

def draw_rectangular_prism(origin, scale, color=BLACK, display_surface=display_surface, width=3):

lines = [
((5, 0), (15, 0)),
((0, 2), (5, 0)),
((0, 2), (0, 10)),
((0, 10), (10, 10)),
((10, 10), (15, 8)),
((15, 8), (15, 0)),
((0, 2), (10, 2)),
((10, 2), (10, 10)),
((10, 2), (15, 0))]

for p1, p2 in lines:
tp1 = (round(origin[0] + p1[0] * scale[0]), round(origin[1] + p1[1] * scale[1]))
tp2 = (round(origin[0] + p2[0] * scale[0]), round(origin[1] + p2[1] * scale[1]))
draw_line(tp1, tp2)

最小的例子:

import pygame
from pygame.locals import *

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

def draw_rectangular_prism(origin, scale, color, surf, width=3):

lines = [
((5, 0), (15, 0)),
((0, 2), (5, 0)),
((0, 2), (0, 10)),
((0, 10), (10, 10)),
((10, 10), (15, 8)),
((15, 8), (15, 0)),
((0, 2), (10, 2)),
((10, 2), (10, 10)),
((10, 2), (15, 0))]

for p1, p2 in lines:
tp1 = (round(origin[0] + p1[0] * scale[0]), round(origin[1] + p1[1] * scale[1]))
tp2 = (round(origin[0] + p2[0] * scale[0]), round(origin[1] + p2[1] * scale[1]))
pygame.draw.line(surf, color, tp1, tp2, width)

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

window.fill(0)
draw_rectangular_prism((50, 100), (10, 20), (255, 0, 0), window)
draw_rectangular_prism((240, 140), (15, 10), (255, 255, 0), window)
pygame.display.flip()
clock.tick(60)

pygame.quit()
exit()

关于python - 扩展在 pygame 中制作的线框 3d 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68204153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com