Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # t_fireflies.py
- import turtle
- import colorsys
- import random
- import math
- screen = turtle.Screen()
- screen.setup(500,500)
- screen.title("Fireflies")
- turtle.hideturtle()
- turtle.speed(0)
- turtle.tracer(0,0)
- # Constants
- H_YELLOWGREEN = 0.22 # constant: hue value of yellow green color.
- V_DARK = 0.1 # constant: brightness value of initial dark state
- V_BRIGHT = 1 # constant: brightness value of the brightest state
- FPS = 30 # constant: refresh about 30 times per second
- TIMER_VALUE = 1000//FPS # the timer value in milliseconds for timer events
- LIGHTUP_TIME = 1 # constant: 1 second light up and dim
- SPEED = 20 # 100 units per second
- CLOSE_ENOUGH = 16 # if distance squared to target is less than 16, then it is close enough.
- # make sure that this number is greater than SPEED/FPS squared
- N = 100 # Number of fireflies
- # Variables
- fireflies = [] # list of firefly turtles
- v = [] # list of brightness values
- glow = [float(str(z*0.01)[:4]) for z in range(0,100,5)]
- glow += [1]+glow[::-1]
- CYCLE = len(glow) # costant: cycle for firefly to light up
- phase = [] # list of phases
- current_xpos = [] # list of current x coordinate
- current_ypos = [] # list of current y coordinate
- target_xpos = [] # list of target x coordinate
- target_ypos = [] # list of target y coordinate
- if 1: # initialze_fireflies
- for i in range(N):
- fireflies.append(turtle.Turtle()) # Add a turtle to the firefly turtle list
- v.append(V_DARK) # set them DARK first. The update function will update it to the correct value
- phase.append(random.randint(0,CYCLE)) # phase is random from 0 to CYCLE
- current_xpos.append(random.uniform(-210,210)) # Let them go anywhere on screen
- current_ypos.append(random.uniform(-210,210))
- target_xpos.append(random.uniform(-210,210))
- target_ypos.append(random.uniform(-210,210))
- for firefly in fireflies: # initialize these turtles
- firefly.hideturtle()
- firefly.up()
- # this function computes brightness based on phase
- def update_brightness():
- global phase,v
- for i in range(N):
- phase[i] = (phase[i]+1)%CYCLE # increase the phase
- v[i] = glow[phase[i]] # compute the brightness based on phase
- def update_position():
- global current_xpos,current_ypos,target_xpos,target_ypos
- for i in range(N):
- # move towards target steps
- # figure out angle to target first
- angle_to_target = math.atan2(target_ypos[i]-current_ypos[i],target_xpos[i]-current_xpos[i])
- # compute changes to current position based on the angle and distance to move.
- current_xpos[i] += 5*math.cos(angle_to_target)
- current_ypos[i] += 5*math.sin(angle_to_target)
- # check to see if close enough to target.
- dist_to_target_squared = (current_xpos[i]-target_xpos[i])**2 + (current_ypos[i]-target_ypos[i])**2
- if dist_to_target_squared < CLOSE_ENOUGH: # close enough, set new target
- target_xpos[i] = random.randint(-210,210) # target x coordinate, random location
- target_ypos[i] = random.randint(-210,210) # target y coordinate, random location
- def draw():
- global v,fireflies,current_xpos,current_ypos
- for i in range(N):
- fireflies[i].clear() # clear the current drawing
- color = colorsys.hsv_to_rgb(H_YELLOWGREEN,1,v[i]) # use colorsys to convert HSV to RGB color
- fireflies[i].color(color)
- fireflies[i].goto(current_xpos[i],current_ypos[i])
- fireflies[i].dot(10)
- screen.bgcolor('black')
- while True:
- update_brightness()
- update_position()
- draw() # draw forever
- screen.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement