Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CodeSkulptor_online_visual_demo.py ### Not a Pys60 project, but does great for simulations
- # http://www.codeskulptor.org/#user39_broLYFFIQrjw0Rh_0.py
- # -------- modules --------
- import simplegui
- import math #used for math.pi for rotation
- # -------- globals/program state -------
- car_pic_draw = True
- happy_pic_draw = False
- horn_text = False
- #image formatting:
- #canvas.draw_image(image, center_source, width_height_source, center_dest, width_height_dest, rotation)
- #car_pic
- car_pic = simplegui.load_image("http://tinyurl.com/pycarjpg")
- car_pic_width = car_pic.get_width()
- car_pic_height = car_pic.get_height()
- car_pic_center = (car_pic_width/2, car_pic_height/2)
- car_pic_size = (car_pic_width, car_pic_height)
- car_pic_dest = (300,300)
- car_pic_resize = car_pic_size
- car_pic_angle = 0 # angle in radians clockwise from up
- #happy_pic
- happy_pic = simplegui.load_image("https://d262ilb51hltx0.cloudfront.net/max/600/1*DBWtbwzx4Egt9cthh1czrA.png")
- happy_pic_width = happy_pic.get_width()
- happy_pic_height = happy_pic.get_height()
- happy_pic_center = (happy_pic_width/2, happy_pic_height/2)
- happy_pic_size = (happy_pic_width, happy_pic_height)
- happy_pic_dest = (300,300)
- happy_pic_resize = (happy_pic_width/1.7, happy_pic_height/1.7)
- happy_pic_angle = 0 # angle in radians clockwise from up
- #car_horn
- car_horn = simplegui.load_sound("http://tinyurl.com/py2honks")
- #happy_sound
- happy_sound = simplegui.load_sound("http://tinyurl.com/thankyou-wav")
- # -------- helper functions --------
- # -------- classes --------
- # -------- event handlers --------
- def draw_handler(canvas): #contains all drawing
- if car_pic_draw == True:
- frame.set_canvas_background("White")
- canvas.draw_image(car_pic, car_pic_center, car_pic_size, car_pic_dest, car_pic_resize, car_pic_angle)
- if happy_pic_draw == True:
- frame.set_canvas_background("Black")
- #canvas.draw_image(happy_pic, happy_pic_center, happy_pic_size, happy_pic_dest, happy_pic_resize, happy_pic_angle)
- canvas.draw_text("Let's All Live Life Lovingly !!!", [45,55], 48, "Red")
- if horn_text == True:
- canvas.draw_text("MOVE IT... GET OUT OF THE WAY !!!", [20,575], 36, "Green")
- def honk_horn():
- if car_pic_draw == True:
- global horn_text
- horn_text = True
- car_horn.play()
- def toggle_car():
- global car_pic_draw
- global happy_pic_draw
- global horn_text
- car_pic_draw = not car_pic_draw
- if car_pic_draw == True:
- happy_pic_draw = False
- if car_pic_draw == False:
- horn_text = False
- def car_rotate(im_a_local_variable):
- global car_pic_angle
- car_pic_angle = int(im_a_local_variable) * math.pi / 180
- def happy_button():
- global horn_text
- global happy_pic_draw
- global car_pic_draw
- happy_pic_draw = not happy_pic_draw
- if happy_pic_draw == True:
- car_pic_draw = False
- horn_text = False
- happy_sound.play()
- # -------- create the frame --------
- frame = simplegui.create_frame("Car Example", 600, 600)
- # -------- register event handlers --------
- frame.set_draw_handler(draw_handler)
- button1 = frame.add_button("Honk Horn", honk_horn)
- label = frame.add_label("")
- button2 = frame.add_button("Toggle Car", toggle_car)
- label = frame.add_label("")
- input_1 = frame.add_input("Rotate car to what angle in degrees?", car_rotate, 50)
- label = frame.add_label("")
- label = frame.add_label("")
- label = frame.add_label("")
- label = frame.add_label("")
- label = frame.add_label("")
- label = frame.add_label("")
- button3 = frame.add_button("The Happy Button !!!", happy_button)
- # -------- start frame and timers --------
- frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement