Advertisement
here2share

# Tk_starfield_2.py

Apr 1st, 2021
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. # Tk_starfield_2.py
  2.  
  3. from Tkinter import *
  4. from random import randint as rnd
  5. from time import time
  6.  
  7. ww = 1200
  8. hh = 600
  9.  
  10. root = Tk()
  11. root.title("Tk_starfield_2")
  12. root.geometry("%dx%d+0+0"%(ww,hh))
  13. canvas = Canvas(root, width=ww, height=hh, bg='black')
  14. canvas.grid()
  15.  
  16. def rgb2hex(rgb): # pass
  17.     r,g,b = rgb
  18.     return "#%02x%02x%02x" % (r,g,b)
  19.    
  20. def e(z): # pass
  21.     if not z: return 1
  22.     return z/50.0
  23.  
  24. W = ww*8
  25. H = hh*8
  26. class Star:
  27.    
  28.     def __init__(self):
  29.         self.x = rnd(-W, W)
  30.         self.y =  rnd(-H, H)
  31.         self.z = rnd(0, ww)
  32.  
  33.         self.pz = self.z
  34.    
  35.     def Update(self):
  36.         self.z -= 100
  37.  
  38.         if self.z < 0:
  39.             self.z = ww
  40.             self.x = rnd(-W, W)
  41.             self.y =  rnd(-H, H)
  42.  
  43.             self.pz = self.z
  44.  
  45.    
  46.     def Draw(self):
  47.         pz = self.pz
  48.         z = self.z
  49.  
  50.         sx = (self.x/e(z))+ww/2
  51.         sy = (self.y/e(z))+hh/2
  52.  
  53.         px = (self.x/e(pz))+ww/2
  54.         py = (self.y/e(pz))+hh/2
  55.  
  56.         r = int(self.z)
  57.        
  58.         canvas.create_line((px, py, sx, sy), fill='white', width=1)
  59.  
  60.         self.pz = self.z
  61.  
  62. count = 200
  63. stars = [Star() for _ in range(count)]
  64.  
  65. while 1:
  66.     t = time()+0.05
  67.     canvas.delete('all')
  68.  
  69.     for star in stars:
  70.         star.Update()
  71.         star.Draw()
  72.     while t > time():
  73.         canvas.update()
  74. canvas.Quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement