Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_starfield_2.py
- from Tkinter import *
- from random import randint as rnd
- from time import time
- ww = 1200
- hh = 600
- root = Tk()
- root.title("Tk_starfield_2")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh, bg='black')
- canvas.grid()
- def rgb2hex(rgb): # pass
- r,g,b = rgb
- return "#%02x%02x%02x" % (r,g,b)
- def e(z): # pass
- if not z: return 1
- return z/50.0
- W = ww*8
- H = hh*8
- class Star:
- def __init__(self):
- self.x = rnd(-W, W)
- self.y = rnd(-H, H)
- self.z = rnd(0, ww)
- self.pz = self.z
- def Update(self):
- self.z -= 100
- if self.z < 0:
- self.z = ww
- self.x = rnd(-W, W)
- self.y = rnd(-H, H)
- self.pz = self.z
- def Draw(self):
- pz = self.pz
- z = self.z
- sx = (self.x/e(z))+ww/2
- sy = (self.y/e(z))+hh/2
- px = (self.x/e(pz))+ww/2
- py = (self.y/e(pz))+hh/2
- r = int(self.z)
- canvas.create_line((px, py, sx, sy), fill='white', width=1)
- self.pz = self.z
- count = 200
- stars = [Star() for _ in range(count)]
- while 1:
- t = time()+0.05
- canvas.delete('all')
- for star in stars:
- star.Update()
- star.Draw()
- while t > time():
- canvas.update()
- canvas.Quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement