Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. import simplegui
  2. import time
  3. import random
  4.  
  5. #-----------------------------------------------------------------------------------------------
  6. #Global variables. Please do not change these!
  7. WIDTH = 800
  8. HEIGHT = 600
  9. count=0
  10. changeCount=0.0
  11. position =[400,300]
  12. velocity=[random.randrange(1,13),random.randrange(1,3)]
  13. acceleration=[0,0]
  14. timer=time.time()
  15. sequence=[0,1,2,3,4] #default sequence of colors
  16. sound_switch='N'
  17. blink_speed =.3 #adjust this for faster or slower blinking of circles
  18. start_game =False
  19.  
  20. #loads the splash screen
  21. splash_screen ="https://www.dropbox.com/s/y5zes0s4fzlv2ku/play_button-512.png?dl=1"
  22. splash_image=simplegui.load_image(splash_screen)
  23. #-----------------------------------------------------------------------------------------------
  24.  
  25.  
  26. """
  27. -----------------------------------------------------------------------------------------------
  28. Q1 Enter the drop box links and make sure its shown in the canvas (3Marks)*
  29. Insert your dropbox links here, make sure you set the "?dl=1" parameter like sample example below else your dropbox files wont load.
  30. -----------------------------------------------------------------------------------------------
  31. """
  32.  
  33. ball_link="https://www.dropbox.com/s/fzqzgzv2uv5kj6l/ball.jpg?dl=1"
  34. sound_link="https://www.dropbox.com/s/20vaslhgs53r3ti/Darth%20Vader%20NO%21.mp3?dl=1"
  35. backgroundImage_link="https://www.dropbox.com/s/n0205jxlc8dueev/background.jpg?dl=1"
  36.  
  37. #These functions below load your image and sound into the program
  38. ball_image = simplegui.load_image(ball_link)
  39. sound = simplegui.load_sound(sound_link)
  40. background_image = simplegui.load_image(backgroundImage_link)
  41.  
  42. #You could control the sound of your music from below, enter values from 0.0 to 1.0, note the decimals
  43. sound.set_volume(.5)
  44.  
  45.  
  46.  
  47.  
  48. """
  49. ------------------------------------------------------------------------------------------------------
  50. #Qn2a This function should check if the ball has collided with the wall and reverse the direction of ball.
  51. #This creates the effect of reflection/bouncing of the wall effect. The position of the ball is effected by
  52. #the velocity parameter (x1,y1). if the ball collides with the left or right wall, we make the x1 component
  53. #negative. If it collides with the top or bottom wall, the y1 component needs to be reversed.
  54. #Movement of any object is defined as position x(t+1) = x(t) + x1(t) and y(t+1)=y(t) + y1(t), where x and y are
  55. #postion of the ball on the canvas and x1 and y1 are its velocity components (4 Marks)***
  56. #Qn2b
  57. #This function also sets the switch to play sound. So when you detect a collusion
  58. #set the sound_switch to 'Y' (1 Mark)
  59. #Qn2c Increment the count by 1 in case of collision with the walls (1 Mark)
  60. ------------------------------------------------------------------------------------------------------
  61. """
  62.  
  63. def check_for_collision(position,velocity,sound_switch,count,radius=40):
  64. x1=velocity[0] # the horizonal velocity componenent
  65. y1=velocity[1] # the vertical velocity component
  66. sound_switch ='N' #Set this to Y if you think there has been a collusion
  67. impact_switch ='N' #Set this to Y if you think there has been a collusion, could use this to update count and sound
  68. #The above components should be modified if the ball has collided with the walls
  69.  
  70. #========= Enter your code below =======================#
  71.  
  72.  
  73. #Check for left wall impact
  74. if position[0] < 16:
  75. x1=-x1
  76. sound_switch='Y'
  77. count=count+1
  78.  
  79. #check for right wall impact
  80. if position[0] > 784:
  81. x1=-x1
  82. sound_switch='Y'
  83. count=count+1
  84.  
  85. #check for top wall impact
  86. if position[1] > 584:
  87. y1=-y1
  88. sound_switch='Y'
  89. count=count+1
  90.  
  91. #check for bottom wall impact
  92. if position[1] < 14:
  93. y1=-y1
  94. sound_switch='Y'
  95. count=count+1
  96.  
  97. #set the sound_switch
  98.  
  99.  
  100. #update the count each time a collision occurs
  101.  
  102.  
  103. #===========================================================#
  104.  
  105. #Do not change the two lines below
  106. velocity=[x1,y1]
  107. return velocity,sound_switch,count
  108.  
  109. #------------------------------------------------------------------------------------------------
  110.  
  111. """
  112. ------------------------------------------------------------------------------------------------------
  113. Q3 Color of the triangles on the screen can be changed based on time
  114. your challenge here is to cycle the colors of the 4 circles on the screen. Each circle starts with
  115. a different color and must change every 1 seconds in sequential order creating a special effect.
  116. You need to select 5 colors of your choice from the website. https://htmlcolorcodes.com/
  117. Each color has its own Hex code starting with #, for example black has a html code (#070707). You need to
  118. select 4 colors of your choice from the options there and fill the below list. You can add more than 4 colors but
  119. it won't be utilized in the program. No marks will be provided for default colors here.
  120. ------------------------------------------------------------------------------------------------------
  121. """
  122.  
  123. #========= Enter your code below =======================#
  124.  
  125. change_color_every_1_second(color_list, timer, changeCount)
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. #change the below list with your color codes
  133. color_list=['#D4492B','#3A8384','#A097E9','#991B73', 'D2D420']
  134.  
  135. #===========================================================#
  136.  
  137. """
  138. BONUS question with 2 additional marks! (Bonus Marks: 2)
  139. """
  140. def change_color_every_1_second(sequence,timer,changeCount):
  141. global blink_speed
  142. #time here is in milliseconds, 100 milliseconds make a second. The timer starts running from the moment the
  143. #program starts. So you need to check for multiples of 100. You cannot use modulus operator why ?
  144. #ChangeCount variable counts how many times you have changed the sequence of colors of the circle since
  145. #the program started executing. So ideally if the time is currently 202 milli seconds, then you should have
  146. #changed the sequence twice by now. This means that you initiate a change only when timer exceeds the number
  147. #of changes. For example 202/100 > changeCount, then you should change the sequence of colors.
  148. #remember to use float before comparison and also to update change count after changing color sequence
  149.  
  150. #Hints: Check if time passed and sequence change are in sync. If it is don't do anything
  151.  
  152.  
  153. #If they are not in sync, change the sequence list.
  154. #the sequence list looks like [0,1,2,3,] meaning first circle will have 0th color corresponding to
  155. #0th color from your color_list and so on. After the first sequence shift, sequence list should look like
  156. #[3,0,1,2], next shift =[2,3,0,1] and so on. Make your code changes below.
  157.  
  158.  
  159.  
  160. #increment the changeCount variable by 1 incase you have changed the sequence
  161.  
  162.  
  163.  
  164. return sequence,changeCount
  165.  
  166.  
  167. """
  168. =============================================
  169. Any modifications past this line are optional.
  170. ==============================================
  171. """
  172.  
  173. #The draw handler will draw the canvas refreshing it 60 times every second
  174. def draw_handler(canvas):
  175. #This draws the boarder
  176. global velocity,sound_switch,count,position,timer,changeCount,sequence,start_game
  177. canvas.draw_polygon([(0, 0), (800, 0), (800, 600),(0, 600)], 12, 'Green')
  178. image_width=background_image.get_width()
  179. image_height=background_image.get_height()
  180. #format loaded image, centre of image, width and height of image, where you want the centre of image to be on canvas,
  181. #How much do you want it to be drawn( Ideally entire canvas.
  182. #If you are not sure of this, do not change these settings
  183. canvas.draw_image(background_image, (image_width/2, image_height/2), (image_width, image_height), (WIDTH/2, HEIGHT/2),(WIDTH, HEIGHT))
  184. ball_width=ball_image.get_width()
  185. ball_height=ball_image.get_height()
  186.  
  187. if start_game:
  188. #Call the function to check for collusions
  189. velocity,sound_switch,count=check_for_collision(position,velocity,sound_switch,count,radius=30)
  190. #Call the function to update colors of the circles
  191. sequence,changeCount=change_color_every_1_second(sequence,time.time()-timer,changeCount)
  192. position[0]=position[0]+velocity[0]
  193. position[1]=position[1]+velocity[1]
  194.  
  195.  
  196. #play sound in case of collusion
  197. if sound_switch =='Y':
  198. sound_switch='N'
  199. sound.rewind()
  200. sound.play()
  201.  
  202.  
  203. #Draw the count text here
  204.  
  205. canvas.draw_text(str(count), (700, 50), 30, 'white', 'serif')
  206.  
  207. #Draw the custom ball
  208. canvas.draw_image(ball_image, (ball_width/2, ball_height/2), (ball_width, ball_height), position,(30,30))
  209.  
  210.  
  211.  
  212. #In case you want to be more creative, you could change the position of the circle on the screen
  213. #you can play around with the positon of the circles. (400,250) specifies the position
  214. #remember the whole canvas is 800,600 and it should be within these boundaries
  215. #canvas.draw_circle((x,y), radius,line width, line color, fill color)
  216.  
  217.  
  218.  
  219. canvas.draw_circle((400,100), 20, 1, 'Black', color_list[sequence[0]])
  220. canvas.draw_circle((400,200), 20, 1, 'black', color_list[sequence[1]])
  221. canvas.draw_circle((400,400), 20, 1, 'black', color_list[sequence[2]])
  222. canvas.draw_circle((400,500), 20, 1, 'black', color_list[sequence[3]])
  223.  
  224. #Display the play/resume splash screen
  225. if not start_game:
  226. splash_width=splash_image.get_width()
  227. splash_height=splash_image.get_height()
  228. canvas.draw_image(splash_image, (splash_width/2, splash_height/2), (splash_width, splash_height), (250, 120),(150,150))
  229. canvas.draw_text("HIT SPACE TO", (220, 180), 10, 'orange', 'serif')
  230. canvas.draw_text("START", (240, 190), 10, 'orange', 'serif')
  231.  
  232. def key_handler(key):
  233. global start_game
  234. if start_game:
  235. start_game= False
  236. else:
  237. start_game= True
  238.  
  239.  
  240.  
  241.  
  242. #Create a simple frame
  243. frame = simplegui.create_frame('Bouncing balls', WIDTH, HEIGHT)
  244. frame.set_draw_handler(draw_handler)
  245. frame.set_keydown_handler(key_handler)
  246. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement