here2share

# Tk_warp_speed.py

Feb 23rd, 2021 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. # Tk_warp_speed.py
  2.  
  3. import math
  4. from Tkinter import Tk, Canvas
  5. from random import randrange
  6.  
  7. ww = 1200
  8. hh = 600
  9. master = Tk()
  10. master.title("StarField")
  11. master.resizable(False, False)
  12. master.maxsize(ww, hh)
  13. fov = 180 * math.pi / 180
  14. view_distance = 0
  15. stars = []
  16. canvas = Canvas(master, width=ww, height=hh, bg="#000000")
  17. canvas.pack()
  18.  
  19. class Star():
  20.     id = None
  21.     x = 0
  22.     y = 0
  23.     z = 0
  24.     radius = 1
  25.     fill = 0
  26.    
  27. def xy(star, width, height):
  28.     d = 2.5
  29.     star.x=randrange(-width*d, width*d)
  30.     star.y=randrange(-height*d, height*d)
  31.  
  32. def StarField(width, height, max_depth=32, num_stars=500):
  33.     for x in range(num_stars):
  34.         star = Star()
  35.         xy(star, width, height)
  36.         star.z=randrange(3, max_depth)
  37.         star.id = canvas.create_oval(star.x - star.radius, star.y - star.radius, star.x + star.radius, star.y + star.radius, fill='#FFFFFF')
  38.         stars.append(star)
  39.  
  40.     while 1:
  41.         canvas.delete('streaks')
  42.         for star in stars:
  43.             # move depth
  44.             star.z -= 1.0
  45.             star.radius = (1 - star.z / max_depth) * 3.0
  46.             star.fill = int((1 - star.z / max_depth) * 255)
  47.  
  48.             # reset depth
  49.             if star.z <= 0:
  50.                 xy(star, width, height)
  51.                 star.z=randrange(int(max_depth*0.6), max_depth)
  52.                 star.radius = 3
  53.                 star.fill = 0
  54.  
  55.             # Transforms 2D to 3D using a perspective projection
  56.             factor = fov / (view_distance + star.z)
  57.             x = star.x * factor + width / 2
  58.             y = -star.y * factor + height / 2
  59.  
  60.             if star.fill:
  61.                 c = 255-star.fill
  62.                 t = star.fill**2*10
  63.                 x0, y0 = star.x*t, star.y*t
  64.                 streak = [(x0/p+ww/2, y0/p+hh/2) for p in (c*(50+c),c*(200+c))]
  65.                 line_color = '#'+('%02x'%star.fill)*3
  66.                 canvas.create_line(streak, fill=line_color, tag='streaks')
  67.             canvas.coords(star.id, x - star.radius, y - star.radius, x + star.radius, y + star.radius)
  68.             canvas.itemconfig(star.id, fill='#'+('%02x'%star.fill)*3)
  69.         canvas.update()
  70.  
  71. StarField(ww, hh)
Add Comment
Please, Sign In to add comment