赞
踩
转换公式
shiju = 300
self.x1 = shiju * x / z
self.y1 = shiju * y / z
# p3.append((self.x1, self.y1))
# pygame.draw.polygon(screen, (0, 244, 0), self.p)
# return (self.x1,self.y1)
参考原理
用scratch制作一个简单的3D引擎
import sys import time import numpy as np import pygame from pygame import surface from pygame._sprite import Group class lu: def __init__(self): self.x = self self.y = self self.z = self self.x1 = self self.y1 = self self.color = self self.screen = self self.p = self def zhuanhuan(self, x, y, z): shiju = 300 self.x1 = shiju * x / z self.y1 = shiju * y / z # p3.append((self.x1, self.y1)) # pygame.draw.polygon(screen, (0, 244, 0), self.p) # return (self.x1,self.y1) pygame.init() # 初始化pygame size = width, height = 600, 600 # 设置窗口大小 screen = pygame.display.set_mode(size) # 显示窗口 color = (255, 255, 255) zsx = 220 zsy = 100 zenx = 70 zeny = 600 ysx = 320 ysy = 100 yenx = 570 yeny = 600 ############################################ f = False fcclock = pygame.time.Clock() cjshuzu = [] xsshuzu = [] o = 5 gp = Group() add = pygame.USEREVENT pygame.time.set_timer(add, 1) # for i in range(6): h1 = lu() h1.x = 0 h1.y = 0 h1.width = 662 h1.height = 35 h1.screen = screen # h1.chuangjian() # cjshuzu.append(h1) def you(screen, ystx, ysty, yenx, yeny): hei = (0, 0, 0) start = (ystx, ysty) end = (yenx, yeny) width = 1222 pygame.draw.line(screen, hei, start, end, 10) def zuo(screen, zstx, zsty, zenx, zeny): hei = (0, 0, 0) start = (zstx, zsty) end = (zenx, zeny) width = 1262 pygame.draw.line(screen, hei, start, end, 10) # aaline抗锯齿 p = [] p1 = [] p2 = [] p3 = [] p4=[] p5=[] p6=[] p7=[] p8=[] p9=[] p10=[] p11=[] p12=[] p13=[] p14=[] p15=[] p16=[] p17=[] p18=[] p19=[] p20=[] p21=[] p22=[] cp = [] xinp = [] cdp = [] z = [{'x1': 0, 'y1': 100, 'z1': 450}, {'x1': 940, 'y1': 100, 'z1': 450}, {'x1': 940, 'y1': 100, 'z1': 350}, {'x1': 0, 'y1': 100, 'z1': 350, }] def huizhi(x, y, z): shiju = 300 x1 = shiju * x / z y1 = shiju * y / z # p.append((x1,y1)) return x1, y1 while True: # 死循环确保窗口一直显示 ic = 1 screen.fill(color) # pygame.draw.polygon(screen, (0, 244, 0), p) fcclock.tick(6) # 设置图像刷新率,如果刷新率太高,图像闪的太厉害 for event in pygame.event.get(): # 遍历所有事件 if event.type == pygame.QUIT: # 如果单击关闭窗口,则退出 sys.exit() pygame.quit() exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: for c in range(len(z)): '''cp[c].x=cp[c].x+1 cp[c].zhuanhuan(cp[c].x,cp[c].y,cp[c].z) p3.append((cp[c].x1,cp[c].y1)) print(p3)''' z[c]['x1'] = z[c]['x1'] - 3 p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1']))) pygame.draw.polygon(screen, (0, 244, 0), p3) # 需要跳出循环才能画 pygame.display.update() elif event.key == pygame.K_w: for c in range(len(z)): z[c]['z1'] = z[c]['z1'] + 30 z[c]['x1'] = z[c]['x1'] + 9#不加的话会向左偏移 p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1']))) pygame.draw.polygon(screen, (0, 244, 0), p3) pygame.display.update() elif event.key == pygame.K_s: for c in range(len(z)): z[c]['z1'] = z[c]['z1'] - 30 z[c]['y1'] = z[c]['y1'] +30 z[c]['x1'] = z[c]['x1'] - 9 p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1']))) pygame.draw.polygon(screen, (0, 244, 0), p3) pygame.display.update() elif event.key == pygame.K_d: for c in range(len(z)): # z[c]['z1'] = z[c]['z1'] - 10 z[c]['x1'] = z[c]['x1'] + 3 p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1']))) pygame.draw.polygon(screen, (0, 244, 0), p3) pygame.display.update() # pygame.draw.polygon(screen,(0, 144, 0),((-22,-80),(44,-80),(57-102),(-28,-102))) #pygame.draw.polygon(screen, (0, 144, 0), points) # pygame.draw.polygon(screen, (0, 244, 0), p) # 需要不停画,才能避免被背景覆盖,p3如果增加,会在这里画出增加后的更新图形位置 for c in range(len(z)): print(z[c]['x1']) z[c]['x1'] = z[c]['x1'] p3.append((huizhi(z[c]['x1'], z[c]['y1'], z[c]['z1']))) p4.append((huizhi(z[c]['x1'], z[c]['y1']+10, z[c]['z1']-60)))#-z是变大,+z是变小 p5.append((huizhi(z[c]['x1'], z[c]['y1'] + 49, z[c]['z1'] - 60))) # -z是变大,+z是变小 p6.append((huizhi(z[c]['x1'], z[c]['y1'] + 100, z[c]['z1'] - 60))) # -z是变大,+z是变小 p7.append((huizhi(z[c]['x1'], z[c]['y1'] + 170, z[c]['z1'] - 60))) # -z是变大,+z是变小 p8.append((huizhi(z[c]['x1'], z[c]['y1'] + 265, z[c]['z1'] - 60))) # -z是变大,+z是变小 p9.append((huizhi(z[c]['x1'], z[c]['y1'] + 390, z[c]['z1'] - 60))) # -z是变大,+z是变小 p10.append((huizhi(z[c]['x1'], z[c]['y1'] + 560, z[c]['z1'] - 60))) # -z是变大,+z是变小 p11.append((huizhi(z[c]['x1'], z[c]['y1'] -10, z[c]['z1'] +60))) # -z是变大,+z是变小 p12.append((huizhi(z[c]['x1'], z[c]['y1'] - 30, z[c]['z1'] + 60))) # -z是变大,+z是变小 p13.append((huizhi(z[c]['x1'], z[c]['y1'] - 40, z[c]['z1'] + 60))) # -z是变大,+z是变小 p14.append((huizhi(z[c]['x1'], z[c]['y1'] - 50, z[c]['z1'] + 60))) # -z是变大,+z是变小 p16.append((huizhi(z[c]['x1'], z[c]['y1'] - 60, z[c]['z1'] + 60))) # -z是变大,+z是变小 p17.append((huizhi(z[c]['x1'], z[c]['y1'] - 70, z[c]['z1'] + 60))) # -z是变大,+z是变小 p18.append((huizhi(z[c]['x1'], z[c]['y1'] - 80, z[c]['z1'] + 60))) # -z是变大,+z是变小 p19.append((huizhi(z[c]['x1'], z[c]['y1'] - 90, z[c]['z1'] + 60))) # -z是变大,+z是变小 if (p10[2][0] > 1600): print("超了") z = [{'x1': 0, 'y1': 100, 'z1': 450}, {'x1': 1090, 'y1': 100, 'z1': 450}, {'x1': 1090, 'y1': 100, 'z1': 350}, {'x1': 0, 'y1': 100, 'z1': 350, }] '''for c in range(len(z1)): z1[c]['x1'] = z1[c]['x1'] p15.append((huizhi(z1[c]['x1'], z1[c]['y1'], z1[c]['z1']))) pygame.draw.polygon(screen, (0, 244, 0), p15)''' print(p10[2]) pygame.draw.polygon(screen, (0, 244, 0), p3) pygame.draw.polygon(screen, (0, 144, 0), p4) pygame.draw.polygon(screen, (0, 244, 0), p5) pygame.draw.polygon(screen, (0, 144, 0), p6) pygame.draw.polygon(screen, (0, 244, 0), p7) pygame.draw.polygon(screen, (0, 144, 0), p8) pygame.draw.polygon(screen, (0, 244, 0), p9) pygame.draw.polygon(screen, (0, 144, 0), p10) pygame.draw.polygon(screen, (0, 144, 0), p11) pygame.draw.polygon(screen, (0, 244, 0), p12) pygame.draw.polygon(screen, (0, 144, 0), p13) pygame.draw.polygon(screen, (0, 244, 0), p14) pygame.draw.polygon(screen, (0, 144, 0), p16) pygame.draw.polygon(screen, (0, 244, 0), p17) pygame.draw.polygon(screen, (0, 144, 0), p18) pygame.draw.polygon(screen, (0, 244, 0), p19) pygame.draw.polygon(screen, (220, 224, 234), ((220,105),(330,105),(560,590),(80,590))) zuo(screen, zsx, zsy, zenx, zeny) you(screen, ysx, ysy, yenx, yeny) pygame.draw.rect(screen, (116, 194, 243), (0, 0, 600, 100), 0) pygame.display.update() p3.clear() p4.clear() p5.clear() p6.clear() p7.clear() p8.clear() p9.clear() p10.clear() p11.clear() p12.clear() p13.clear() p14.clear() p15.clear() p16.clear() p17.clear() p18.clear() p19.clear() pygame.display.flip() # 更新全部显示
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。