Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_warp_speed.py
- import math
- from Tkinter import Tk, Canvas
- from random import randrange
- ww = 1200
- hh = 600
- master = Tk()
- master.title("StarField")
- master.resizable(False, False)
- master.maxsize(ww, hh)
- fov = 180 * math.pi / 180
- view_distance = 0
- stars = []
- canvas = Canvas(master, width=ww, height=hh, bg="#000000")
- canvas.pack()
- class Star():
- id = None
- x = 0
- y = 0
- z = 0
- radius = 1
- fill = 0
- def xy(star, width, height):
- d = 2.5
- star.x=randrange(-width*d, width*d)
- star.y=randrange(-height*d, height*d)
- def StarField(width, height, max_depth=32, num_stars=500):
- for x in range(num_stars):
- star = Star()
- xy(star, width, height)
- star.z=randrange(3, max_depth)
- star.id = canvas.create_oval(star.x - star.radius, star.y - star.radius, star.x + star.radius, star.y + star.radius, fill='#FFFFFF')
- stars.append(star)
- while 1:
- canvas.delete('streaks')
- for star in stars:
- # move depth
- star.z -= 1.0
- star.radius = (1 - star.z / max_depth) * 3.0
- star.fill = int((1 - star.z / max_depth) * 255)
- # reset depth
- if star.z <= 0:
- xy(star, width, height)
- star.z=randrange(int(max_depth*0.6), max_depth)
- star.radius = 3
- star.fill = 0
- # Transforms 2D to 3D using a perspective projection
- factor = fov / (view_distance + star.z)
- x = star.x * factor + width / 2
- y = -star.y * factor + height / 2
- if star.fill:
- c = 255-star.fill
- t = star.fill**2*10
- x0, y0 = star.x*t, star.y*t
- streak = [(x0/p+ww/2, y0/p+hh/2) for p in (c*(50+c),c*(200+c))]
- line_color = '#'+('%02x'%star.fill)*3
- canvas.create_line(streak, fill=line_color, tag='streaks')
- canvas.coords(star.id, x - star.radius, y - star.radius, x + star.radius, y + star.radius)
- canvas.itemconfig(star.id, fill='#'+('%02x'%star.fill)*3)
- canvas.update()
- StarField(ww, hh)
Add Comment
Please, Sign In to add comment