Advertisement
SM110866

Spinning cube script (Python)

Mar 3rd, 2025
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. #spinning cube python code
  2.  
  3. import math
  4. import os
  5. import time
  6. A = B = C = 0
  7.  
  8. cube_width= 20
  9. width, height = 160, 40
  10. z_buffer = [0]*(width*height)
  11. buffer = [' ']*(width*height)
  12. background_ascii_code = '_'
  13. distance_from_cam = 100
  14. horizontal_offset = 0
  15. K1 = 40
  16.  
  17. increment_speed = 0.6
  18.  
  19. def calculate_x(i,j,k):
  20.     return j * math.sin(A) *\
  21.               math.sin(B)*\
  22.               math.cos(C) -k*\
  23.               math.cos(A)*\
  24.               math.sin(B)*\
  25.               math.cos(C)+j*\
  26.               math.cos(A)*math.sin(C)+k*\
  27.               math.sin(A)*\
  28.               math.sin(C)+i*\
  29.               math.cos(B)*\
  30.               math.cos(C)
  31.  
  32. def calculate_y(i,j,k):
  33.     return j*math.cos(A)*\
  34.             math.cos(C)+k*\
  35.             math.sin(A)*\
  36.             math.cos(C)-j*\
  37.             math.sin(A)*\
  38.             math.sin(B)*\
  39.             math.sin(C)+k*\
  40.             math.cos(A)*\
  41.             math.sin(B)*\
  42.             math.sin(C)-i*\
  43.             math.cos(B)*\
  44.             math.sin(C)
  45.  
  46. def calculate_z(i,j,k):
  47.     return k * math.cos(A) * math.cos(B) - j * math.sin(A) * math.cos(B) + i * math.sin(B)
  48.  
  49. def calculate_for_surface(cube_x, cube_y, cube_z,ch):
  50.     global buffer, z_buffer
  51.     x = calculate_x(cube_x, cube_y, cube_z)
  52.     y = calculate_y(cube_x, cube_y, cube_z)
  53.     z = calculate_z(cube_x, cube_y, cube_z)+distance_from_cam
  54.  
  55.     ozz = 1/z
  56.     xp = int(width/2+horizontal_offset+K1 *ozz*x*2)
  57.     yp = int(height/2+K1*ozz*y)
  58.  
  59.     idx = xp+yp*width
  60.     if 0 <= idx <width*height:
  61.         if ozz>z_buffer[idx]:
  62.             z_buffer[idx] = ozz
  63.             buffer[idx] = ch
  64.  
  65.  
  66. def clear_screen():
  67.     os.system('cls' if os.name == 'nt' else 'clear')
  68.  
  69. def main():
  70.     global A, B, C,cube_width,horizontal_offset, buffer, z_buffer
  71.  
  72.     while True:
  73.         buffer = [background_ascii_code]*(width*height)
  74.         z_buffer = [0]*(width*height)
  75.         cube_width = 20
  76.         horizontal_offset = -2*cube_width
  77.  
  78.         cube_x = -cube_width
  79.         while cube_x < cube_width:
  80.             cube_y = -cube_width
  81.             while cube_y < cube_width:
  82.                 calculate_for_surface(cube_x, cube_y, -cube_width,'@')
  83.                 calculate_for_surface(cube_width, cube_y, cube_x,'$')
  84.                 calculate_for_surface(-cube_width, cube_y, -cube_x,'~')
  85.                 calculate_for_surface(-cube_x, cube_y, cube_width,'#')
  86.                 calculate_for_surface(cube_x, -cube_width, -cube_y,';')
  87.                 calculate_for_surface(cube_x, cube_width, cube_y,'+')
  88.                 cube_y+=increment_speed
  89.             cube_x+=increment_speed
  90.  
  91.         clear_screen()
  92.         for k in range(width*height):
  93.             if k % width == 0 and k!= 0:
  94.                 print()
  95.             print(buffer[k], end='')
  96.        
  97.         A+=0.05
  98.         B+=0.05
  99.         C+=0.01
  100.         time.sleep(0.016)
  101.  
  102.    
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement