<The_Conductor />
In Processing, the "Driver Code" is often called the Sketch. This is where the application starts.
It has two main responsibilities:
- setup(): Runs once at the beginning. It creates the canvas, defines the colors, and spawns our army of ghosts.
- draw(): Runs continuously (usually 60 times per second). It clears the screen and tells every single ghost to "update" (move) and then "render" (draw itself).
This separation of duties (Classes define what things are, Driver Code defines how they interact) is the heart of good Object-Oriented Design.
1. Setup: setting the Stage
Here we define our global variables, set the screen size, and create the initial batch of ghosts using a loop.
52def setup(): 53 """ 54 Initialize the canvas and color palette. 55 Creates multiple random ghosts at startup. 56 """ 57 print("...setup...") 58 59 # Declare global variables that will be modified 60 global ghost_list 64 global roygbiv_colors, all_color_options 65 global canvas_width, canvas_height 66 67 # Set up canvas (match JavaScript dimensions) 68 size(canvas_width, canvas_height) 69 70 # IMPORTANT: Set color mode to HSB for easier color manipulation 71 colorMode(HSB, 360, 100, 100) 72 102 # ===== Create random ghosts like JavaScript version ===== 103 print("...creating random ghosts...") 104 105 # Generate a random number of ghosts (1-9 like JavaScript) 106 num_ghosts = int(random(3, 10)) # random(3, 10) gives 3-9 107 print("Creating {} ghosts".format(num_ghosts)) 108 109 # Create each ghost with random properties 110 for i in range(num_ghosts): 111 new_ghost = generate_random_ghost() 112 ghost_list.append(new_ghost) 113 114 # Set frame rate (match JavaScript) 115 frameRate(fr) 121#end setup
2. Draw: The Animation Loop
This loop runs continuously. It clears the background (erasing the old frame) and then asks each ghost to move and draw itself again.
125def draw(): 126 """ 127 Main animation loop. 128 Clears the screen, updates all ghosts, and renders them. 129 """ 130 # Clear the screen 131 background(my_dark_gray) 132 133 # Loop through all ghosts and update/render each one 134 for ghost in ghost_list: 135 # Update ghost position and check boundaries 136 ghost.update() 137 138 # Render the ghost 139 ghost.render() 140 #end for each ghost 141#end draw
3. The Ghost Factory
This helper function handles all the messy random math needed to create a unique ghost. It keeps the main setup code clean.
145def generate_random_ghost(): 158 print("\n...generating random ghost...") 159 160 # ===== Randomize ghost dimensions ===== 161 # Width between 20 and 100 162 ghost_width = random(20, 100) 163 ghost_radius = ghost_width / 2.0 164 172 # Number of feet between 1 and 10 173 num_bumps = int(random(1, 11)) 174 177 # Round or triangular feet (random boolean) 178 bump_round = random(1) > 0.5 179 181 # Random body color from extended palette 182 body_color = all_color_options[int(random(0, len(all_color_options)))] 183 216 # ===== Create the ghost ===== 217 # Create head Disk 218 ghost_center = (start_x, start_y) 219 head_colors = (body_color, None) # No stroke on head 220 ghost_head = Disk(ghost_center, ghost_radius, 0, head_colors) 221 225 # Create the Ghost object 226 new_ghost = Ghost( 227 ghost_head, # head Disk 228 body_height, # body height 229 num_bumps, # number of feet 230 eye_color, # eye/pupil color 231 eye_base_colors, # (white, black) for eyeball base 232 eye_span, # eye spacing factor 233 (start_x, start_y), # starting position 234 (vel_x, vel_y), # velocity 235 canvas_width, # canvas width for bounds 236 canvas_height, # canvas height for bounds 237 bump_round # round vs triangular feet 238 ) 239 240 print(" Ghost created!") 241 return new_ghost 242#end generate_random_ghost
The End!
You've explored the entire system: The Disk (base geometry), the Eyeball (composite part), the Ghost (the agent), and the Driver Code (the world).
Back to Home