Advertisement
here2share

# CodeSkulptor_online_visual_demo.py

Jan 11th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. # CodeSkulptor_online_visual_demo.py ### Not a Pys60 project, but does great for simulations
  2. # http://www.codeskulptor.org/#user39_broLYFFIQrjw0Rh_0.py
  3.  
  4. # -------- modules --------
  5.  
  6. import simplegui
  7. import math #used for math.pi for rotation
  8.  
  9. # -------- globals/program state -------
  10.  
  11. car_pic_draw = True
  12. happy_pic_draw = False
  13. horn_text = False
  14.  
  15. #image formatting:
  16. #canvas.draw_image(image, center_source, width_height_source, center_dest, width_height_dest, rotation)
  17.  
  18. #car_pic
  19. car_pic = simplegui.load_image("http://tinyurl.com/pycarjpg")
  20. car_pic_width = car_pic.get_width()
  21. car_pic_height = car_pic.get_height()
  22. car_pic_center = (car_pic_width/2, car_pic_height/2)
  23. car_pic_size = (car_pic_width, car_pic_height)
  24. car_pic_dest = (300,300)
  25. car_pic_resize = car_pic_size
  26. car_pic_angle = 0 # angle in radians clockwise from up
  27.  
  28. #happy_pic
  29. happy_pic = simplegui.load_image("https://d262ilb51hltx0.cloudfront.net/max/600/1*DBWtbwzx4Egt9cthh1czrA.png")
  30. happy_pic_width = happy_pic.get_width()
  31. happy_pic_height = happy_pic.get_height()
  32. happy_pic_center = (happy_pic_width/2, happy_pic_height/2)
  33. happy_pic_size = (happy_pic_width, happy_pic_height)
  34. happy_pic_dest = (300,300)
  35. happy_pic_resize = (happy_pic_width/1.7, happy_pic_height/1.7)
  36. happy_pic_angle = 0 # angle in radians clockwise from up
  37.  
  38. #car_horn
  39. car_horn = simplegui.load_sound("http://tinyurl.com/py2honks")
  40.  
  41. #happy_sound
  42. happy_sound = simplegui.load_sound("http://tinyurl.com/thankyou-wav")
  43.  
  44.  
  45. # -------- helper functions --------
  46.  
  47.  
  48. # -------- classes --------
  49.  
  50.  
  51. # -------- event handlers --------
  52.  
  53.  
  54. def draw_handler(canvas): #contains all drawing
  55.     if car_pic_draw == True:
  56.         frame.set_canvas_background("White")
  57.         canvas.draw_image(car_pic, car_pic_center, car_pic_size, car_pic_dest, car_pic_resize, car_pic_angle)
  58.     if happy_pic_draw == True:
  59.         frame.set_canvas_background("Black")
  60.         #canvas.draw_image(happy_pic, happy_pic_center, happy_pic_size, happy_pic_dest, happy_pic_resize, happy_pic_angle)
  61.         canvas.draw_text("Let's All Live Life Lovingly !!!", [45,55], 48, "Red")
  62.     if horn_text == True:
  63.         canvas.draw_text("MOVE IT... GET OUT OF THE WAY !!!", [20,575], 36, "Green")
  64.        
  65. def honk_horn():
  66.     if car_pic_draw == True:
  67.         global horn_text
  68.         horn_text = True
  69.         car_horn.play()
  70.  
  71. def toggle_car():
  72.     global car_pic_draw
  73.     global happy_pic_draw
  74.     global horn_text
  75.     car_pic_draw = not car_pic_draw
  76.     if car_pic_draw == True:
  77.         happy_pic_draw = False
  78.     if car_pic_draw == False:
  79.         horn_text = False
  80.        
  81. def car_rotate(im_a_local_variable):
  82.     global car_pic_angle
  83.     car_pic_angle = int(im_a_local_variable) * math.pi / 180
  84.    
  85. def happy_button():
  86.     global horn_text
  87.     global happy_pic_draw
  88.     global car_pic_draw
  89.     happy_pic_draw = not happy_pic_draw
  90.     if happy_pic_draw == True:
  91.         car_pic_draw = False
  92.         horn_text = False
  93.         happy_sound.play()
  94.    
  95.    
  96. # -------- create the frame --------
  97.  
  98. frame = simplegui.create_frame("Car Example", 600, 600)
  99.  
  100. # -------- register event handlers --------
  101.  
  102.  
  103. frame.set_draw_handler(draw_handler)
  104.  
  105. button1 = frame.add_button("Honk Horn", honk_horn)
  106. label = frame.add_label("")
  107.  
  108. button2 = frame.add_button("Toggle Car", toggle_car)
  109. label = frame.add_label("")
  110.  
  111. input_1 = frame.add_input("Rotate car to what angle in degrees?", car_rotate, 50)
  112. label = frame.add_label("")
  113. label = frame.add_label("")
  114. label = frame.add_label("")
  115. label = frame.add_label("")
  116. label = frame.add_label("")
  117. label = frame.add_label("")
  118.  
  119. button3 = frame.add_button("The Happy Button !!!", happy_button)
  120.  
  121. # -------- start frame and timers --------
  122.  
  123. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement