acoolrocket

Programming Exam Code - Spacecraft Slingshot Final

Apr 16th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.96 KB | None | 0 0
  1. import simplegui
  2. import math
  3.  
  4. width = 900
  5. height = 900
  6.  
  7. # player to obstacles variables
  8. orbit_time = 5
  9. fx = 0
  10. fy = 0
  11. dx = 0
  12. dy = 0
  13. force_scalar = 0
  14. distance = 0
  15. collision = 0
  16. player_mass = 5
  17. key_binding = "up"
  18. hover_pos_spacecraft = 0
  19. met_planet_list = []
  20. obstacle_check_color = 'White'
  21.  
  22. # game global variables
  23. timespent = 0
  24. lives = 3
  25. score = 4000
  26. t = 1000
  27. time_score = 10
  28. game_end = 0
  29.  
  30. # hint boxes variables
  31. time_hint = 100
  32. time_hint_duration = 0
  33. hit_by_this_ticker = 100
  34. hit_by_this_duration = 0
  35. hint_message = ""
  36. deviator_warning_message = ""
  37. hit_by_this_lives2_message = ""
  38. hit_by_this_lives1_message = ""
  39.  
  40.  
  41. # Time functions
  42.  
  43. def tick():
  44. global timespent
  45. timespent = timespent + 1
  46.  
  47. def tick_score():
  48. global score
  49. score -= 1
  50.  
  51.  
  52. class spacecraft:
  53. def __init__(self,centre, lwidth,lcolour, fillcolour, vel, boost_acceleration, hitbox_radius):
  54. self.centre = centre
  55. self.lwidth = lwidth
  56. self.lcolour = lcolour
  57. self.fillcolour = fillcolour
  58. self.vel = vel
  59. self.boost_acceleration = boost_acceleration
  60. self.player_mass = player_mass
  61. self.hitbox_radius = hitbox_radius
  62.  
  63.  
  64. def move(self, obstacle):
  65. global width, height, lives, score, met_planet_list, obstacle_check_color
  66. self.centre[0] = self.centre[0] + self.vel[0]
  67. self.centre[1] = self.centre[1] + self.vel[1]
  68.  
  69. # Code to ensure spacecraft doesn’t go beyond the edges of the canvas
  70. if self.centre[0] <= self.hitbox_radius:
  71. lives -= 1
  72. score -= 10
  73. self.centre = [50, 850]
  74. self.vel = [0, 0]
  75. victory_call.game_victory = 0
  76. met_planet_list = []
  77. o1.lcolour = 'White'
  78. o2.lcolour = 'White'
  79. o3.lcolour = 'White'
  80. o4.lcolour = 'White'
  81. o5.lcolour = 'White'
  82. o6.lcolour = 'White'
  83. o7.lcolour = 'White'
  84. o8.lcolour = 'White'
  85. o9.lcolour = 'White'
  86. o10.lcolour = 'White'
  87. elif self.centre[0] >= width-self.hitbox_radius:
  88. lives -= 1
  89. score -= 10
  90. self.centre = [50, 850]
  91. self.vel = [0, 0]
  92. victory_call.game_victory = 0
  93. met_planet_list = []
  94. o1.lcolour = 'White'
  95. o2.lcolour = 'White'
  96. o3.lcolour = 'White'
  97. o4.lcolour = 'White'
  98. o5.lcolour = 'White'
  99. o6.lcolour = 'White'
  100. o7.lcolour = 'White'
  101. o8.lcolour = 'White'
  102. o9.lcolour = 'White'
  103. o10.lcolour = 'White'
  104. elif self.centre[1] <= self.hitbox_radius:
  105. lives -= 1
  106. score -= 10
  107. self.centre = [50, 850]
  108. self.vel = [0, 0]
  109. victory_call.game_victory = 0
  110. met_planet_list = []
  111. o1.lcolour = 'White'
  112. o2.lcolour = 'White'
  113. o3.lcolour = 'White'
  114. o4.lcolour = 'White'
  115. o5.lcolour = 'White'
  116. o6.lcolour = 'White'
  117. o7.lcolour = 'White'
  118. o8.lcolour = 'White'
  119. o9.lcolour = 'White'
  120. o10.lcolour = 'White'
  121. elif self.centre[1] >= height-self.hitbox_radius:
  122. lives -= 1
  123. score -= 10
  124. self.centre = [50, 850]
  125. self.vel = [0, 0]
  126. victory_call.game_victory = 0
  127. met_planet_list = []
  128. o1.lcolour = 'White'
  129. o2.lcolour = 'White'
  130. o3.lcolour = 'White'
  131. o4.lcolour = 'White'
  132. o5.lcolour = 'White'
  133. o6.lcolour = 'White'
  134. o7.lcolour = 'White'
  135. o8.lcolour = 'White'
  136. o9.lcolour = 'White'
  137. o10.lcolour = 'White'
  138.  
  139.  
  140. def drawSpacecraft(self, canvas):
  141. canvas.draw_polygon([(self.centre[0] + 10, self.centre[1]), (self.centre[0] - 10, self.centre[1] -10), (self.centre[0] -10, self.centre[1] +10)], self.lwidth, self.lcolour, self.fillcolour)
  142.  
  143.  
  144. def gravity(self, obstacle):
  145. global dx, dy, distance, force_scalar, fx, fy, theta, ax, ay, orbit_time, collision, lives, score, player_mass, Call_hit_by_this_message, hit_by_this_lives2_message, hit_by_this_lives1_message, ticker_hint, met_planet_list, obstacle_check_color
  146.  
  147. dx = (obstacle.centre[0]- self.centre[0])
  148. dy = (obstacle.centre[1] - self.centre[1])
  149.  
  150. # Calculate the distance between the spacecraft and the obstacle - d
  151. distance = math.sqrt(dx**2 + dy**2)
  152.  
  153. # Calculate the gravitational force - F
  154. force_scalar = player_mass * obstacle.mass/(distance**2)
  155.  
  156. # Compute the direction of the force
  157. theta = math.atan2(dy, dx)
  158.  
  159. # Calculate x and y components of the force
  160. fx = math.cos(theta) * force_scalar
  161. fy = math.cos(theta) * force_scalar
  162.  
  163. # Axis changing
  164. if self.centre[0] > obstacle.centre[0]:
  165. theta = -theta
  166. elif self.centre[0] < obstacle.centre[0]:
  167. theta = math.fabs(theta)
  168. elif self.centre[1] > obstacle.centre[1]:
  169. theta = math.fabs(theta)
  170. elif self.centre[1] < obstacle.centre[1]:
  171. theta = -theta
  172.  
  173. # Calculate acceleration in x and y directions
  174.  
  175. ax = fx/player_mass
  176. ay = fy/player_mass
  177.  
  178. # updating velocity components using acceleration
  179.  
  180. self.vel[0] = self.vel[0]+ ax * orbit_time
  181. self.vel[1]= self.vel[1] + ay * orbit_time
  182.  
  183. # Collision detection -
  184. if distance <= (10 + obstacle.radius):
  185. lives -= 1
  186. score -= 100
  187. self.centre = [50, 850]
  188. self.vel = [0, 0]
  189. Call_hit_by_this_message.planet_hit_location2 = obstacle.centre
  190. Call_hit_by_this_message.planet_hit_location1 = obstacle.centre
  191. Call_hit_by_this_message.planet_hit_lives2 = obstacle.SNo
  192. Call_hit_by_this_message.planet_hit_lives1 = obstacle.SNo
  193. victory_call.game_victory = 0
  194. met_planet_list = []
  195. obstacle.lcolour = 'White'
  196. o1.lcolour = 'White'
  197. o2.lcolour = 'White'
  198. o3.lcolour = 'White'
  199. o4.lcolour = 'White'
  200. o5.lcolour = 'White'
  201. o6.lcolour = 'White'
  202. o7.lcolour = 'White'
  203. o8.lcolour = 'White'
  204. o9.lcolour = 'White'
  205. o10.lcolour = 'White'
  206.  
  207. if lives <= 2 and lives >= 1:
  208. hit_by_this_lives2_message = "You've been hit by planet" + " " + str(Call_hit_by_this_message.planet_hit_lives2)
  209.  
  210. if lives <= 1:
  211. hit_by_this_lives1_message = "You've been hit by planet" + " " + str(Call_hit_by_this_message.planet_hit_lives1)
  212.  
  213. def met_planet(self, obstacle_hitboxes, obstacle):
  214. global dx, dy, distance, met_planet_list, obstacle_check_color
  215.  
  216. # met planet checker
  217.  
  218. if distance <= (80 + obstacle_hitboxes.radius):
  219. met_planet_list.append(str(obstacle_hitboxes.SNo))
  220. obstacle.lcolour = 'Green'
  221.  
  222. # Key Bindings
  223.  
  224. def keydown(self, key):
  225. # Code for booster
  226. global key_binding, hover_pos_spacecraft, game_end, timespent, score, lives, hit_by_this_lives2_message, hit_by_this_lives1_message, met_planet_list, obstacle_check_color
  227. if key == simplegui.KEY_MAP[key_binding]:
  228. self.vel[1] -= self.boost_acceleration
  229. elif key == simplegui.KEY_MAP["space"]:
  230. hover_pos_spacecraft = 10
  231.  
  232. if key == simplegui.KEY_MAP["r"] and game_end == 1:
  233. lives = 3
  234. timespent = 0
  235. score = 4000
  236. hit_by_this_lives2_message = ""
  237. hit_by_this_lives1_message = ""
  238. hover_pos_spacecraft = 0
  239. Call_hit_by_this_message.hit_by_this_spacer = 0
  240. victory_call.game_victory = 0
  241.  
  242. def keyup(self, key):
  243. #Code for booster key
  244. global key_binding, game_end
  245. if key == simplegui.KEY_MAP[key_binding]:
  246. self.vel[1] = 0
  247.  
  248. if key == simplegui.KEY_MAP["r"]:
  249. game_end = 0
  250.  
  251.  
  252. # Obstacles classes
  253.  
  254. class obstacle1:
  255. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  256. self.centre = centre
  257. self.radius = radius
  258. self.lwidth = lwidth
  259. self.lcolour = lcolour
  260. self.fillcolour = fillcolour
  261. self.SNo = SNo
  262. self.mass = mass
  263.  
  264.  
  265. def drawobstacle(self, canvas):
  266. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  267. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  268.  
  269. class obstacle2:
  270. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  271. self.centre = centre
  272. self.radius = radius
  273. self.lwidth = lwidth
  274. self.lcolour = lcolour
  275. self.fillcolour = fillcolour
  276. self.SNo = SNo
  277. self.mass = mass
  278.  
  279.  
  280. def drawobstacle(self, canvas):
  281. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  282. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  283.  
  284. class obstacle3:
  285. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  286. self.centre = centre
  287. self.radius = radius
  288. self.lwidth = lwidth
  289. self.lcolour = lcolour
  290. self.fillcolour = fillcolour
  291. self.SNo = SNo
  292. self.mass = mass
  293.  
  294.  
  295. def drawobstacle(self, canvas):
  296. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  297. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  298.  
  299. class obstacle4:
  300. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  301. self.centre = centre
  302. self.radius = radius
  303. self.lwidth = lwidth
  304. self.lcolour = lcolour
  305. self.fillcolour = fillcolour
  306. self.SNo = SNo
  307. self.mass = mass
  308.  
  309.  
  310. def drawobstacle(self, canvas):
  311. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  312. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  313.  
  314. class obstacle5:
  315. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  316. self.centre = centre
  317. self.radius = radius
  318. self.lwidth = lwidth
  319. self.lcolour = lcolour
  320. self.fillcolour = fillcolour
  321. self.SNo = SNo
  322. self.mass = mass
  323.  
  324.  
  325. def drawobstacle(self, canvas):
  326. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  327. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  328.  
  329. class obstacle6:
  330. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  331. self.centre = centre
  332. self.radius = radius
  333. self.lwidth = lwidth
  334. self.lcolour = lcolour
  335. self.fillcolour = fillcolour
  336. self.SNo = SNo
  337. self.mass = mass
  338.  
  339.  
  340. def drawobstacle(self, canvas):
  341. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  342. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  343.  
  344. class obstacle7:
  345. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  346. self.centre = centre
  347. self.radius = radius
  348. self.lwidth = lwidth
  349. self.lcolour = lcolour
  350. self.fillcolour = fillcolour
  351. self.SNo = SNo
  352. self.mass = mass
  353.  
  354.  
  355. def drawobstacle(self, canvas):
  356. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  357. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  358.  
  359. class obstacle8:
  360. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  361. self.centre = centre
  362. self.radius = radius
  363. self.lwidth = lwidth
  364. self.lcolour = lcolour
  365. self.fillcolour = fillcolour
  366. self.SNo = SNo
  367. self.mass = mass
  368.  
  369.  
  370. def drawobstacle(self, canvas):
  371. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  372. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  373.  
  374. class obstacle9:
  375. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  376. self.centre = centre
  377. self.radius = radius
  378. self.lwidth = lwidth
  379. self.lcolour = lcolour
  380. self.fillcolour = fillcolour
  381. self.SNo = SNo
  382. self.mass = mass
  383.  
  384.  
  385. def drawobstacle(self, canvas):
  386. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  387. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  388.  
  389. def drawobstacle(self, canvas):
  390. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  391. canvas.draw_text(str(self.SNo), [self.centre[0]-5, self.centre[1]+5], 20, "White")
  392.  
  393. class obstacle10:
  394. def __init__(self, centre,radius, lwidth, lcolour,fillcolour, SNo, mass):
  395. self.centre = centre
  396. self.radius = radius
  397. self.lwidth = lwidth
  398. self.lcolour = lcolour
  399. self.fillcolour = fillcolour
  400. self.SNo = SNo
  401. self.mass = mass
  402.  
  403.  
  404. def drawobstacle(self, canvas):
  405. canvas.draw_circle([self.centre[0], self.centre[1]], self.radius,self.lwidth,self.lcolour,self.fillcolour)
  406. canvas.draw_text(str(self.SNo), [self.centre[0]-11, self.centre[1]+5], 20, "White")
  407.  
  408. # obstacle hitboxes to utilize
  409.  
  410. class obstacle_hitboxes1:
  411. def __init__(self, centre,radius, SNo):
  412. self.centre = centre
  413. self.radius = radius
  414. self.SNo = SNo
  415.  
  416. class obstacle_hitboxes2:
  417. def __init__(self, centre,radius, SNo):
  418. self.centre = centre
  419. self.radius = radius
  420. self.SNo = SNo
  421.  
  422. class obstacle_hitboxes3:
  423. def __init__(self, centre,radius, SNo):
  424. self.centre = centre
  425. self.radius = radius
  426. self.SNo = SNo
  427.  
  428. class obstacle_hitboxes4:
  429. def __init__(self, centre,radius, SNo):
  430. self.centre = centre
  431. self.radius = radius
  432. self.SNo = SNo
  433.  
  434. class obstacle_hitboxes5:
  435. def __init__(self, centre,radius, SNo):
  436. self.centre = centre
  437. self.radius = radius
  438. self.SNo = SNo
  439.  
  440. class obstacle_hitboxes6:
  441. def __init__(self, centre,radius, SNo):
  442. self.centre = centre
  443. self.radius = radius
  444. self.SNo = SNo
  445.  
  446. class obstacle_hitboxes7:
  447. def __init__(self, centre,radius, SNo):
  448. self.centre = centre
  449. self.radius = radius
  450. self.SNo = SNo
  451.  
  452. class obstacle_hitboxes8:
  453. def __init__(self, centre,radius, SNo):
  454. self.centre = centre
  455. self.radius = radius
  456. self.SNo = SNo
  457.  
  458. class obstacle_hitboxes9:
  459. def __init__(self, centre,radius, SNo):
  460. self.centre = centre
  461. self.radius = radius
  462. self.SNo = SNo
  463.  
  464. class obstacle_hitboxes10:
  465. def __init__(self, centre,radius, SNo):
  466. self.centre = centre
  467. self.radius = radius
  468. self.SNo = SNo
  469.  
  470. # Hint message
  471.  
  472. class hint_message_fun:
  473. def hint_trigger():
  474. global ticker_hint, key_binding, hint_message, lives, time_hint_duration
  475. if lives == 2:
  476. ticker_hint.start()
  477. hint_message = "Hint: Carefully rapid tap the" + " \"" + str(key_binding) + "\" " + "key to boost."
  478.  
  479. if time_hint_duration > 30:
  480. hint_message = ""
  481. ticker_hint.stop()
  482.  
  483. def tick_hint():
  484. global time_hint_duration
  485. time_hint_duration += 1
  486.  
  487.  
  488. class hit_by_this_message:
  489. def __init__(self, planet_hit_lives2, planet_hit_lives1, planet_hit_location2, planet_hit_location1, time_hint_restart, hit_by_this_spacer):
  490. self.planet_hit_lives2 = planet_hit_lives2
  491. self.planet_hit_lives1 = planet_hit_lives1
  492. self.planet_hit_location2 = planet_hit_location2
  493. self.planet_hit_location1 = planet_hit_location1
  494. self.time_hint_restart = time_hint_restart
  495. self.hit_by_this_spacer = hit_by_this_spacer
  496.  
  497. def hit_by_this_lives2(self):
  498. global lives, hit_by_this_lives2_message, time_hint_duration
  499.  
  500. if time_hint_duration > 30:
  501. hit_by_this_lives2_message = ""
  502. if self.planet_hit_location2[0] < 750:
  503. self.hit_by_this_spacer = 0
  504.  
  505. def time_hit_by_this_restarter(self):
  506. global time_hint_duration, ticker_hint
  507.  
  508. if self.time_hint_restart == 1:
  509. time_hint_duration = 0
  510. self.time_hint_restart = 0
  511.  
  512. def hit_by_this_lives1(self):
  513. global lives, hit_by_this_lives1_message, time_hint_duration
  514.  
  515. if lives <= 1:
  516. if time_hint_duration > 30:
  517. hit_by_this_lives1_message = ""
  518. if self.planet_hit_location2[0] < 750:
  519. self.hit_by_this_spacer = 0
  520.  
  521. def draw_hitbythis_text(self, canvas):
  522. global hit_by_this_lives2_message, hit_by_this_lives1_message
  523. canvas.draw_text (hit_by_this_lives2_message, [self.planet_hit_location2[0] - (70 + self.hit_by_this_spacer), self.planet_hit_location2[1] - 25], 10, "White","monospace")
  524. canvas.draw_text (hit_by_this_lives1_message, [self.planet_hit_location1[0] - (70 + self.hit_by_this_spacer), self.planet_hit_location1[1] - 25], 10, "White","monospace")
  525.  
  526. def text_spacer(self):
  527. global s1
  528. if s1.centre[0] > 780:
  529. self.hit_by_this_spacer = 30
  530.  
  531. # Calling spacecraft
  532.  
  533. s1 = spacecraft([50, 850] , 2,'black','Red', [0,0], 3.5, 10)
  534.  
  535.  
  536. #11 Obstacles
  537.  
  538. obslist = []
  539. o1 = obstacle1 ([136, 803], 40, 5, 'White', 'Teal', 1, 10)
  540. obslist.append(o1)
  541. o2 = obstacle2 ([52, 700], 30, 5, 'White', 'Cyan', 2, 10)
  542. obslist.append(o2)
  543. o3 = obstacle3 ([205, 631], 55, 5, 'White', 'Brown', 3, 50)
  544. obslist.append(o3)
  545. o4 = obstacle4 ([351, 536], 40, 5, 'White', 'Red', 4, 40)
  546. obslist.append(o4)
  547. o5 = obstacle5 ([496, 689], 65, 5, 'White', 'Blue', 5, 50)
  548. obslist.append(o5)
  549. o6 = obstacle6 ([758, 558], 125, 5, 'White', 'Orange', 6, 50)
  550. obslist.append(o6)
  551. o7 = obstacle7 ([747, 304], 52, 5, 'White', 'Orange', 8, 30)
  552. obslist.append(o7)
  553. o8 = obstacle8 ([496, 223], 125, 5, 'White', 'Silver', 7, 40)
  554. obslist.append(o8)
  555. o9 = obstacle9 ([658, 141], 41, 5, 'White', 'Maroon', 9, 20)
  556. obslist.append(o9)
  557. o10 = obstacle10 ([859, 135], 30, 5, 'White', 'Orange', 10, 70)
  558. obslist.append(o10)
  559.  
  560.  
  561. # Obstacle hitboxes list
  562.  
  563. obhitlist = []
  564. oh1 = obstacle_hitboxes1 ([136, 803], 70, 1)
  565. obhitlist.append(oh1)
  566. oh2 = obstacle_hitboxes2 ([52, 700], 60, 2)
  567. obhitlist.append(oh2)
  568. oh3 = obstacle_hitboxes3 ([205, 631], 85, 3)
  569. obhitlist.append(oh3)
  570. oh4 = obstacle_hitboxes4 ([351, 536], 70, 4)
  571. obhitlist.append(oh4)
  572. oh5 = obstacle_hitboxes5 ([496, 689], 95, 5)
  573. obhitlist.append(oh5)
  574. oh6 = obstacle_hitboxes6 ([758, 558], 105, 6)
  575. obhitlist.append(oh6)
  576. oh7 = obstacle_hitboxes7 ([747, 304], 82, 7)
  577. obhitlist.append(oh7)
  578. oh8 = obstacle_hitboxes8 ([476, 193], 115, 8)
  579. obhitlist.append(oh8)
  580. oh9 = obstacle_hitboxes9 ([658, 141], 71, 9)
  581. obhitlist.append(oh9)
  582. oh10 = obstacle_hitboxes10 ([859, 135], 60, 10)
  583. obhitlist.append(oh10)
  584.  
  585. # Calling message functions
  586.  
  587. Call_hint_message = hint_message_fun
  588. Call_hit_by_this_message = hit_by_this_message(0, 0, [0, 0], [0, 0], 0, 0)
  589.  
  590. class planet_checker_for_victory:
  591.  
  592. def __init__(self, game_victory):
  593. self.game_victory = game_victory
  594.  
  595. def victory_checker(self, obstacle):
  596. if obstacle.lcolour == 'White':
  597. self.game_victory = 0
  598. elif o1.lcolour == 'White':
  599. self.game_victory = 0
  600. elif o2.lcolour == 'White':
  601. self.game_victory = 0
  602. elif o3.lcolour == 'White':
  603. self.game_victory = 0
  604. elif o4.lcolour == 'White':
  605. self.game_victory = 0
  606. elif o5.lcolour == 'White':
  607. self.game_victory = 0
  608. elif o6.lcolour == 'White':
  609. self.game_victory = 0
  610. elif o7.lcolour == 'White':
  611. self.game_victory = 0
  612. elif o8.lcolour == 'White':
  613. self.game_victory = 0
  614. elif o9.lcolour == 'White':
  615. self.game_victory = 0
  616. elif o10.lcolour == 'White':
  617. self.game_victory = 0
  618. else:
  619. self.game_victory = 1
  620.  
  621. victory_call = planet_checker_for_victory(0)
  622.  
  623. # Calling functions and drawing on canvas
  624.  
  625. def draw(canvas):
  626. global s1, obslist, timespent, score, game_end, hit_by_this_lives2_message, Call_hit_by_this_message, hint_message, met_planet_list, obhitlist, victory_call, deviator_warning_message
  627. #Game Over Screen
  628. if lives == 0 or score < 0:
  629. canvas.draw_text("Game Over!", [330,450], 50, "Red")
  630. game_end = 1
  631. canvas.draw_text("Press R to restart", [360,500], 25, "White")
  632.  
  633. elif s1.centre[0] > 740 and s1.centre[1] < 100 and victory_call.game_victory == 1:
  634. #Code to check if the spacecraft has reached the finish point and display
  635. #message on the canvas accordingly
  636. canvas.draw_text("You're Winner!", [330,440], 50, "Yellow")
  637. time_score.stop()
  638. t.stop()
  639. canvas.draw_text("You're score:" + " " + str(score), [372,485], 25, "White")
  640. canvas.draw_text("You've completed in:" + " " + str(timespent) + " seconds.", [370,520], 15, "White")
  641.  
  642. else:
  643.  
  644. # Draw main objects
  645.  
  646. s1.drawSpacecraft(canvas)
  647. for item in obslist:
  648. item.drawobstacle(canvas)
  649. s1.gravity(item)
  650. s1.met_planet(item, item)
  651. victory_call.victory_checker(item)
  652. s1.move(item)
  653.  
  654. # Deviator warning boundries
  655.  
  656. if (s1.centre[0] < 490 and s1.centre[1] < 514) or (s1.centre[0] > 450 and s1.centre[1] > 645) or (s1.centre[0] > 806 and (s1.centre[1] < 465 and s1.centre[1] > 175)):
  657. deviator_warning_message = "You are going out of planet order and won't be able to complete the level."
  658.  
  659. else:
  660. deviator_warning_message = ""
  661.  
  662. # Code to display time elapsed
  663. canvas.draw_text ("Time Elapsed since the start:" + str(timespent), [200, 30], 20, "White","monospace")
  664.  
  665. # Code to display lives on the canvas
  666. canvas.draw_text ("Lives left:" + str(lives), [570, 30], 20, "Yellow","monospace")
  667.  
  668. # Code to display score on the canvas
  669. canvas.draw_text ("Score:" + str(score), [380, 70], 30, "Cyan","monospace")
  670.  
  671. # Draw finish box
  672. canvas.draw_polygon([(740, 0), (740, 70), (900, 70), (900, 0)], 5, "Green", "Black")
  673.  
  674. canvas.draw_text ("Finish Line", [790, 37], 10, "White","monospace")
  675.  
  676. canvas.draw_text (deviator_warning_message, [100,400], 18, "White","monospace")
  677.  
  678. # Draw hint text
  679. Call_hit_by_this_message.draw_hitbythis_text(canvas)
  680.  
  681. canvas.draw_text (hint_message, [180,450], 20, "White","monospace")
  682.  
  683. # Call hint trigger
  684.  
  685. Call_hint_message.hint_trigger()
  686.  
  687. Call_hit_by_this_message.hit_by_this_lives2()
  688. Call_hit_by_this_message.hit_by_this_lives1()
  689. Call_hit_by_this_message.time_hit_by_this_restarter()
  690. Call_hit_by_this_message.text_spacer()
  691.  
  692. # Dev information text
  693.  
  694. canvas.draw_text (str(s1.centre), [s1.centre[0] - 70, s1.centre[1] - 25], hover_pos_spacecraft, "White","monospace")
  695.  
  696. canvas.draw_text (str(victory_call.game_victory), [290, 800], hover_pos_spacecraft/1.5, "White","monospace")
  697.  
  698.  
  699. #Frame Handlers
  700.  
  701. f = simplegui.create_frame("Spacecraft Slingshot", width, height)
  702. t = simplegui.create_timer(t, tick)
  703. time_score = simplegui.create_timer(time_score, tick_score)
  704. ticker_hint = simplegui.create_timer(time_hint, Call_hint_message.tick_hint)
  705. f.set_keydown_handler(s1.keydown)
  706. f.set_keyup_handler(s1.keyup)
  707. f.set_draw_handler(draw)
  708. f.start()
  709. t.start()
  710. time_score.start()
  711. ticker_hint.stop()
Advertisement
Add Comment
Please, Sign In to add comment