Demonstration of using events; a variation on the project SkeletonBonez: Functions

""" By: CodeFanDot - Fox Valley Girls Coding Club Demonstrates Events Demonstrates Color Names and Hex Values Skeleton "Song" is based on the traditional spritual "Them Bones" """ stage.set_background("hauntedhouseinterior") logo = codesters.Sprite("FVGCCLogo_56f", 175, 130) logo.set_size(.2) coding_club_name = codesters.Text("F\nV\nG\nC\nC", 220, 140) coding_club_name.set_color("fuchsia") # image from https://publicdomainvectors.org/tn_img/Skeleton-With-Arms-Out.png billy_bones = codesters.Sprite("SkeletonT_f31", -130, 60) billy_bones.set_size(.7) # image from https://publicdomainvectors.org/tn_img/Skeleton2.png dusty= codesters.Sprite("SkeletonStanding_cf2", 50, 0) skeleton = codesters.Sprite("frankenstein", -30, -100) sound = codesters.Sound("drum_clap") #Colors are defined by assigning a value from 0 to 256 to RED, to GREEN, AND to BLUE (RGB) #Colors can be assigned by name; each name represents a specific RGB value #https://www.w3schools.com/colors/colors_names.asp speech_color = "cyan" #RGB(0,255,255) No red, max green, max blue #Color can also be assigned by Hexadecimal values, allowing even more possible hues #https://www.w3schools.com/colors/colors_hex.asp #Start the HEX string with the # character; followed by 3 Hex bytes #a HEX byte hold a value from 00 to FF; 00 = 0 and FF = 255 #The first HEX byte represents RED, next 2 GREEN, last 2 BLUE #00FF88 has 0 Red, 255 (the maximum) Green saturation, and 136 Blue bone_color = "#00FF88" skeleton.say("Bone-jour!", 3, speech_color) skeleton.say("Let's have EVENTFUL fun!", 3, speech_color) # Another approach to accomplish the same thing is to define # separate functions for each bone and chain them together by # calling each successive function from within the preceding one # def toe_bone(): sound.play() skeleton.say("TOE BONE", 1, bone_color) skeleton.say("The Toe Bone's connected to the....", 1, speech_color) skeleton.turn_left(360) foot_bone() def foot_bone(): sound.play() skeleton.say("FOOT BONE", 1, bone_color) skeleton.say("The Foot Bone's connected to the...", 1, speech_color) skeleton.turn_right(360) heel_bone() def heel_bone(): sound.play() skeleton.say("HEEL BONE!", 1, bone_color) skeleton.say("The Heel Bone's connected to the..", 1, speech_color) skeleton.turn_left(360) ankle_bone() def ankle_bone(): sound.play() skeleton.say("ANKLE BONE!", 1, bone_color) skeleton.say("The Ankle Bone's connected to the...", 1, speech_color) skeleton.turn_right(360) shin_bone() def shin_bone(): sound.play() skeleton.say("SHIN BONE!", 1, bone_color) skeleton.say("The Shin Bone's connected to the...", 1, speech_color) skeleton.turn_left(360) knee_bone() def knee_bone(): sound.play() skeleton.say("KNEE BONE!", 1, bone_color) skeleton.say("The Knee Bone's connected to the...", 1, speech_color) skeleton.turn_right(360) thigh_bone() def thigh_bone(): sound.play() skeleton.say("THIGH BONE!", 1, bone_color) skeleton.say("The Thigh Bone's connected to the...", 1, speech_color) skeleton.turn_left(360) hip_bone() def hip_bone(): sound.play() skeleton.say("HIP BONE!", 1, bone_color) skeleton.say("The Hip Bone's connected to the...", 1, speech_color) skeleton.turn_right(360) back_bone() def back_bone(): sound.play() skeleton.say("BACK BONE!", 1, bone_color) skeleton.say("The Back Bone's connected to the...", 1, speech_color) skeleton.turn_left(360) shoulder_bone() def shoulder_bone(): sound.play() skeleton.say("SHOULDER BONE!", 1, bone_color) skeleton.say("The Shoulder Bone's connected to the...", 1, speech_color) skeleton.turn_right(360) neck_bone() def neck_bone(): sound.play() skeleton.say("NECK BONE!", 1, bone_color) skeleton.say("The Neck Bone's connected to the...", 1, speech_color) skeleton.turn_left(360) head_bone() def head_bone(): sound.play() skeleton.set_say_color(bone_color) skeleton.say("HEAD BONE!") skeleton.turn_right(360) # For this demo we want the recitation to be event-driven def skeleton_click(sprite): sprite.say("Bone-Jour!\nYou clicked me!", 3) sprite.say("Let's kick this off with the toe bone!", 3) toe_bone() skeleton.event_click(skeleton_click) #Add movement controls by defining event listeners and handlers #the function named left_key is an event handler def left_key(): skeleton.move_left(20) # This statement sets up an Event Listener; the program will "listen" for the left key press # and when left key press is detected, it will execute the function named "left_key" stage.event_key("left", left_key) def right_key(): skeleton.move_right(20) stage.event_key("right", right_key) #stage_click is the Event Handler function tied to the stage_click event # the stage "knows" the x and y coordinates where it was clicked # so we can use that knowledge to move to the clicked location def stage_click(): x = stage.click_x() y = stage.click_y() billy_bones.say("I'm flying!") billy_bones.glide_to(x, y) stage.event_click(stage_click) def billy_collision(sprite, hit_sprite): hit_sprite_name = hit_sprite.get_image_name() if hit_sprite_name == "frankenstein": sprite.say("I hit Frankie!", 1) billy_bones.event_collision(billy_collision) def d_key(): dusty.say("Big D, that's me!") stage.event_key("d", d_key) time_running = 0 # Event handler for the stage interval event; this will get executed # each time the interval passes def interval(): #time_running has to be declared global so we can keep its updated value #between executions of the interval function global time_running local_time_running = 10 local_time_running += 10 time_running +=10 dusty.say("Run time: " + str(time_running) + str(local_time_running), 2) stage.event_interval(interval, 10)
  • Run Code
  • Show Console
  • Codesters How To (opens in a new tab)