Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

3D Turtles

By: a guest on Apr 3rd, 2012  |  syntax: Python  |  size: 2.31 KB  |  views: 94  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from turtle import *
  2. import random
  3.  
  4. def cube(cam, box):
  5.     box[2] = box[2] / 20
  6.     width = 300.0 #dimensions of the "screen"
  7.     height = 300.0
  8.     vertices = []
  9.     vertices.append([0.0, 0.0, 0.0]) #every vertex.
  10.     vertices.append([0.0, 0.0, 1.0 * box[2]])
  11.     vertices.append([0.0, 1.0 * box[1], 0.0])
  12.     vertices.append([0.0, 1.0 * box[1], 1.0 * box[2]])
  13.     vertices.append([1.0 * box[0], 0.0, 0.0])
  14.     vertices.append([1.0 * box[0], 0.0, 1.0 * box[2]])
  15.     vertices.append([1.0 * box[0], 1.0 * box[1], 0.0])
  16.     vertices.append([1.0 * box[0], 1.0 * box[1], 1.0 * box[2]])
  17.     newverts = []
  18.     for i in range(len(vertices)):
  19.         vert = vertices[i]
  20.         z = vert[2] + 1
  21.         F = z - cam[2]
  22.         """if (z == 0):
  23.            z = 1 #uuuuuhh"""
  24.         x = ((vert[0] - cam[0]) * (F / z)) + cam[0] + 1
  25.         y = ((vert[1] - cam[1]) * (F / z)) + cam[1] + 1
  26.         newverts.append([x, y])
  27.     """for i in range(len(newverts)):
  28.        #pu()
  29.        goto(newverts[i])
  30.        #pd()
  31.        #dot(3)
  32.        "=""rt(90)
  33.        fd(20)
  34.        rt(-90)
  35.        fd(20)"=""
  36.        print(i, newverts[i])"""
  37.     pu()
  38.     goto(newverts[0]) #manuel labor
  39.     pd()
  40.     #goto(newverts[])
  41.     goto(newverts[4])
  42.     goto(newverts[6])
  43.     goto(newverts[2])
  44.     goto(newverts[0])
  45.     pu()
  46.     goto(newverts[1])
  47.     pd()
  48.     goto(newverts[3])
  49.     goto(newverts[7])
  50.     goto(newverts[5])
  51.     goto(newverts[1])
  52.     goto(newverts[0])
  53.     pu()
  54.     goto(newverts[5])
  55.     pd()
  56.     goto(newverts[4])
  57.     pu()
  58.     goto(newverts[3])
  59.     pd()
  60.     goto(newverts[2])
  61.     pu()
  62.     goto(newverts[7])
  63.     pd()
  64.     goto(newverts[6])
  65.  
  66. #main
  67. speed(0)
  68.  
  69. camera = [-5, -5, 10] #x,y,z
  70.  
  71. def keydown_d():
  72.     key_d = true
  73.     camera[0] = camera[0] + 1
  74.  
  75. def keydown_a():
  76.     key_a = true
  77.     camera[0] = camera[0] - 1
  78.  
  79. def keydown_w():
  80.     key_w = true
  81.     camera[2] = camera[2] + .5
  82.  
  83. def keydown_s():
  84.     key_s = true
  85.     camera[2] = camera[2] - .5
  86.  
  87. while (True):
  88.     key_d = false
  89.     key_a = false
  90.     key_w = false
  91.     key_s = false
  92.     listen()
  93.     onkey(keydown_d, "d")
  94.     onkey(keydown_a, "a")
  95.     onkey(keydown_w, "w")
  96.     onkey(keydown_s, "s")
  97.     boxinfo = [10.0, 10.0, 10.0] #width, height, depth; does not really make a cube
  98.     cube(camera, boxinfo)
  99.     clear()
  100.    
  101. done()