Advertisement
here2share

# t_fireflies.py

Sep 3rd, 2020
1,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. # t_fireflies.py
  2.  
  3. import turtle
  4. import colorsys
  5. import random
  6. import math
  7.  
  8. screen = turtle.Screen()
  9. screen.setup(500,500)
  10. screen.title("Fireflies")
  11. turtle.hideturtle()
  12. turtle.speed(0)
  13. turtle.tracer(0,0)
  14.  
  15. # Constants
  16. H_YELLOWGREEN = 0.22 # constant: hue value of yellow green color.
  17. V_DARK = 0.1 # constant: brightness value of initial dark state
  18. V_BRIGHT = 1 # constant: brightness value of the brightest state
  19. FPS = 30 # constant: refresh about 30 times per second
  20. TIMER_VALUE = 1000//FPS # the timer value in milliseconds for timer events
  21. LIGHTUP_TIME = 1 # constant: 1 second light up and dim
  22. SPEED = 20 # 100 units per second
  23. CLOSE_ENOUGH = 16 # if distance squared to target is less than 16, then it is close enough.
  24.                     # make sure that this number is greater than SPEED/FPS squared
  25. N = 100 # Number of fireflies
  26.  
  27. # Variables
  28. fireflies = [] # list of firefly turtles
  29. v = [] # list of brightness values
  30. glow = [float(str(z*0.01)[:4]) for z in range(0,100,5)]
  31. glow += [1]+glow[::-1]
  32. CYCLE = len(glow) # costant: cycle for firefly to light up
  33. phase = [] # list of phases
  34. current_xpos = [] # list of current x coordinate
  35. current_ypos = [] # list of current y coordinate
  36. target_xpos = [] # list of target x coordinate
  37. target_ypos = [] # list of target y coordinate
  38.  
  39. if 1: # initialze_fireflies
  40.     for i in range(N):
  41.         fireflies.append(turtle.Turtle()) # Add a turtle to the firefly turtle list
  42.         v.append(V_DARK) # set them DARK first. The update function will update it to the correct value
  43.         phase.append(random.randint(0,CYCLE)) # phase is random from 0 to CYCLE
  44.         current_xpos.append(random.uniform(-210,210)) # Let them go anywhere on screen
  45.         current_ypos.append(random.uniform(-210,210))
  46.         target_xpos.append(random.uniform(-210,210))
  47.         target_ypos.append(random.uniform(-210,210))
  48.  
  49.     for firefly in fireflies: # initialize these turtles
  50.         firefly.hideturtle()
  51.         firefly.up()
  52.                
  53. # this function computes brightness based on phase
  54.  
  55. def update_brightness():
  56.     global phase,v
  57.     for i in range(N):
  58.         phase[i] = (phase[i]+1)%CYCLE # increase the phase
  59.         v[i] = glow[phase[i]] # compute the brightness based on phase
  60.  
  61. def update_position():
  62.     global current_xpos,current_ypos,target_xpos,target_ypos
  63.     for i in range(N):
  64.         # move towards target steps
  65.         # figure out angle to target first
  66.         angle_to_target = math.atan2(target_ypos[i]-current_ypos[i],target_xpos[i]-current_xpos[i])
  67.         # compute changes to current position based on the angle and distance to move.
  68.         current_xpos[i] += 5*math.cos(angle_to_target)
  69.         current_ypos[i] += 5*math.sin(angle_to_target)
  70.         # check to see if close enough to target.
  71.         dist_to_target_squared = (current_xpos[i]-target_xpos[i])**2 + (current_ypos[i]-target_ypos[i])**2
  72.         if dist_to_target_squared < CLOSE_ENOUGH: # close enough, set new target
  73.             target_xpos[i] = random.randint(-210,210) # target x coordinate, random location
  74.             target_ypos[i] = random.randint(-210,210) # target y coordinate, random location
  75.  
  76. def draw():
  77.     global v,fireflies,current_xpos,current_ypos
  78.     for i in range(N):
  79.         fireflies[i].clear() # clear the current drawing
  80.         color = colorsys.hsv_to_rgb(H_YELLOWGREEN,1,v[i]) # use colorsys to convert HSV to RGB color
  81.         fireflies[i].color(color)
  82.         fireflies[i].goto(current_xpos[i],current_ypos[i])
  83.         fireflies[i].dot(10)
  84.  
  85. screen.bgcolor('black')
  86. while True:
  87.     update_brightness()
  88.     update_position()
  89.     draw() # draw forever
  90.     screen.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement