Advertisement
AlfonsoPEREZ

3D MOVING SQUARE

Apr 27th, 2020
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. import turtle
  2. from math import sin,cos
  3. import random
  4.  
  5. win = turtle.Screen()
  6. win.setup(1000,1000)
  7. win.bgcolor('black')
  8. win.tracer(0)
  9. win.listen()
  10. counter = 0
  11.  
  12. def rotate(x,y,r):
  13.     s,c = sin(r), cos(r)
  14.     return x*c-y*s, x*s+y*c
  15.  
  16.  
  17. class Cube:
  18.     EDGES = (0,1), (1,2), (2,3), (3,0),(4,5), (5,6), (6,7), (7,4),(0,4), (1,5), (2,6), (3,7)
  19.     VERTEXES = [(-1,-1,-1),(1,-1,-1), (1,1,-1),(-1,1,-1),(-1,-1,1),(1,-1,1),( 1,1,1),(-1,1,1)]
  20.  
  21.     def __init__(self, xpos, ypos, counter,axes, color):
  22.         self.xpos = xpos
  23.         self.ypos = ypos
  24.         self.counter = counter
  25.         self.axes = axes
  26.         self.c = color
  27.         self.t = turtle.Turtle()
  28.         self.t.ht()
  29.         self.t.color(self.c)
  30.         self.distance = 300
  31.  
  32.     def draw(self):
  33.  
  34.         for edge in self.EDGES:
  35.             points = []
  36.            
  37.             for vertex in edge:
  38.                 x,y,z = self.VERTEXES[vertex]
  39.  
  40.                 if self.axes == '3':
  41.                     x,z = rotate(x,z,self.counter)
  42.                     y,z = rotate(y,z,self.counter)
  43.                     x,y = rotate(x,y,self.counter)
  44.                    
  45.                 elif self.axes == 'x':
  46.                     y,z = rotate(y,z,self.counter)
  47.                 elif self.axes == 'y':
  48.                     x,z = rotate(x,z,self.counter)
  49.                 elif self.axes == 'z':
  50.                     x,y = rotate(x,y,self.counter)
  51.            
  52.                 z += 5
  53.                 if z != 0:
  54.                     f = self.distance/(z)
  55.                
  56.                 sx, sy = x*f,y*f
  57.                 points.append((sx,sy))
  58.  
  59.             self.t.up()
  60.             self.t.goto(points[0][0]+self.xpos, points[0][1]+self.ypos)
  61.             self.t.down()
  62.             self.t.goto(points[1][0]+self.xpos, points[1][1]+self.ypos)
  63.             self.t.goto(points[0][0]+self.xpos, points[0][1]+self.ypos)
  64.             self.t.up()
  65.  
  66.     def move_in(self):
  67.         if self.distance<1000:
  68.             self.distance+=50
  69.  
  70.     def move_out(self):
  71.         if self.distance>50:
  72.             self.distance-= 50
  73.  
  74.  
  75. # Cube(x,y,counter,axes,color)
  76. cube = Cube(0,0,counter,'3', 'blue')
  77.  
  78. win.onkey(cube.move_in, 'Up')
  79. win.onkey(cube.move_out, 'Down')
  80.  
  81.  
  82. while True:
  83.     cube.t.clear()
  84.     cube.draw()
  85.  
  86.     win.update()
  87.     cube.counter += 0.005
  88. #MADE BY AVMP
  89. #https://www.youtube.com/channel/UCQor7IURWM-lGT-tmFbFSCw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement