Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. def __init__(self, wh):
  2. super(Car, self).__init__()
  3. self.cars = pygame.sprite.RenderPlain(self)
  4.  
  5. self.pos = Vector2()
  6. self.image = pygame.Surface(wh, pygame.SRCALPHA)
  7. pygame.draw.rect(self.image, RED, (0, 0, meter_per_pixel(4), meter_per_pixel(1.75)))
  8. self.original_image = self.image
  9. self.rect = self.image.get_rect(center=self.pos)
  10. self.aPrev = 0
  11. self.vPrev = 0
  12. self.sPrev = 0
  13. self.totalS = 0
  14. self.maxAcc = 15
  15. self.safeDistance = False
  16.  
  17. def find_acc(self):
  18. # self.acc = self.maxacc
  19. a = random.randint(-10, 30)
  20. return a
  21.  
  22. def moveto(self, new_pos):
  23. self.rect.x = new_pos.x
  24. self.rect.y = new_pos.y
  25. self.pos = self.rect.center
  26.  
  27. def linear_routing(self, horizontal, positive, total_distance, first):
  28.  
  29. if horizontal:
  30. if positive:
  31. if first:
  32. self.moveto(getEnterPositions("Left"))
  33.  
  34. else:
  35. self.moveto(getLeavePositions("Right"))
  36. else:
  37. if first:
  38. self.moveto(getEnterPositions("Right"))
  39. else:
  40. self.moveto(getLeavePositions("Left"))
  41. else:
  42. if positive:
  43. if first:
  44. self.moveto(getEnterPositions("Top"))
  45. else:
  46. self.moveto(getLeavePositions("Bottom"))
  47. else:
  48. if first:
  49. self.moveto(getEnterPositions("Bottom"))
  50. else:
  51. self.moveto(getLeavePositions("Top"))
  52.  
  53. while self.totalS <= total_distance:
  54. a = self.find_acc()
  55. a_avg = 0.5 * (a + self.aPrev)
  56. v = self.vPrev + a_avg * deltaT
  57. s = self.sPrev + self.vPrev * deltaT + 0.5 * a_avg * deltaT * deltaT
  58. deltaS = self.vPrev * deltaT + 0.5 * a_avg * deltaT * deltaT
  59.  
  60. pos = self.rect
  61.  
  62. if horizontal:
  63. if positive:
  64. pos.x += deltaS
  65. else:
  66. pos.x -= deltaS
  67. else:
  68. if positive:
  69. pos.y += deltaS
  70. else:
  71. pos.y -= deltaS
  72.  
  73. self.moveto(pos)
  74.  
  75. self.totalS += deltaS
  76. self.aPrev = a
  77. self.vPrev = v
  78. self.sPrev = s
  79.  
  80. if horizontal:
  81. self.image = pygame.transform.rotate(self.original_image, 0)
  82. else:
  83. self.image = pygame.transform.rotate(self.original_image, 90)
  84.  
  85. Background()
  86. self.cars.draw(screen)
  87. pygame.display.flip()
  88. #pygame.event.get()
  89. clock.tick(30)
  90. # time.sleep(0.1)
  91.  
  92. return self.totalS
  93.  
  94. def circular_routing(self, in_degree, out_degree):
  95. current_degree = in_degree
  96. pos = Vector2()
  97. distance = Vector2()
  98. distance.x = pixel_x(0) - self.rect.x
  99. distance.y = pixel_y(0) - self.rect.y
  100. radius = distance.length()
  101.  
  102. while current_degree <= out_degree:
  103. a = self.find_acc()
  104. a_avg = 0.5 * (a + self.aPrev)
  105. v = self.vPrev + a_avg * deltaT
  106. s = self.sPrev + self.vPrev * deltaT + 0.5 * a_avg * deltaT * deltaT
  107.  
  108. s0 = straightRoad
  109. alpha0 = in_degree * math.pi / 180
  110. alpha = (s - s0) / (radius * math.pi)
  111.  
  112. current_rad = alpha + alpha0
  113. current_degree = (current_rad * 180 / math.pi) # % 360
  114.  
  115. pos.x = pixel_x(0) + (math.cos(current_rad) * radius)
  116. pos.y = pixel_y(0) - (math.sin(current_rad) * radius)
  117.  
  118. self.moveto(pos)
  119.  
  120. self.totalS = s
  121. self.aPrev = a
  122. self.vPrev = v
  123. self.sPrev = s
  124.  
  125. self.image = pygame.transform.rotate(self.original_image, -(90 - current_degree))
  126. Background()
  127. self.cars.draw(screen)
  128. pygame.display.flip()
  129. #pygame.event.get()
  130. clock.tick(30)
  131. # time.sleep(0.1)
  132. return current_degree, self.totalS
  133.  
  134. def BigMove(self, Enter, Leave):
  135. firstHorizontal = False
  136. firstPositive = False
  137. secondHorizontal = False
  138. secondPositive = False
  139. inDegree = 0
  140. outDegree = 0
  141. total_distance = straightRoad
  142.  
  143. if Enter == "Left":
  144.  
  145. if Leave == "Left":
  146.  
  147. circularAngle = 330 # kavsakta kac derece döndügü
  148. inDegree = 190 # kavsaga nereden girdigi
  149. outDegree = (circularAngle + inDegree) # % 360 # cikis derecesi
  150. firstHorizontal = True
  151. firstPositive = True
  152. secondHorizontal = True
  153. secondPositive = False
  154.  
  155. elif Leave == "Bottom":
  156.  
  157. circularAngle = 70
  158. inDegree = 190
  159. outDegree = (circularAngle + inDegree) # % 360
  160. firstHorizontal = True
  161. firstPositive = True
  162. secondHorizontal = False
  163. secondPositive = True
  164.  
  165. elif Leave == "Right":
  166.  
  167. circularAngle = 160
  168. inDegree = 190
  169. outDegree = (circularAngle + inDegree) # % 360
  170. firstHorizontal = True
  171. firstPositive = True
  172. secondHorizontal = True
  173. secondPositive = True
  174.  
  175. elif Leave == "Top":
  176.  
  177. circularAngle = 250
  178. inDegree = 190
  179. outDegree = (circularAngle + inDegree) # % 360
  180. firstHorizontal = True
  181. firstPositive = True
  182. secondHorizontal = False
  183. secondPositive = False
  184. else:
  185. print("XXX")
  186.  
  187. elif Enter == "Right":
  188.  
  189. if Leave == "Left":
  190.  
  191. circularAngle = 150
  192. inDegree = 20
  193. outDegree = circularAngle + inDegree
  194. firstHorizontal = True
  195. firstPositive = False
  196. secondHorizontal = True
  197. secondPositive = False
  198.  
  199. elif Leave == "Bottom":
  200.  
  201. circularAngle = 240
  202. inDegree = 20
  203. outDegree = circularAngle + inDegree
  204. firstHorizontal = True
  205. firstPositive = False
  206. secondHorizontal = False
  207. secondPositive = True
  208.  
  209. elif Leave == "Right":
  210.  
  211. circularAngle = 330
  212. inDegree = 20
  213. outDegree = circularAngle + inDegree
  214. firstHorizontal = True
  215. firstPositive = False
  216. secondHorizontal = True
  217. secondPositive = True
  218.  
  219. elif Leave == "Top":
  220.  
  221. circularAngle = 70
  222. inDegree = 20
  223. outDegree = circularAngle + inDegree
  224. firstHorizontal = True
  225. firstPositive = False
  226. secondHorizontal = False
  227. secondPositive = False
  228. else:
  229. print("XXX")
  230.  
  231. elif Enter == "Bottom":
  232.  
  233. if Leave == "Left":
  234.  
  235. circularAngle = 250
  236. inDegree = 280
  237. outDegree = circularAngle + inDegree
  238. firstHorizontal = False
  239. firstPositive = False
  240. secondHorizontal = True
  241. secondPositive = False
  242.  
  243. elif Leave == "Bottom":
  244.  
  245. circularAngle = 340
  246. inDegree = 280
  247. outDegree = circularAngle + inDegree
  248. firstHorizontal = False
  249. firstPositive = False
  250. secondHorizontal = False
  251. secondPositive = True
  252.  
  253. elif Leave == "Right":
  254.  
  255. circularAngle = 70
  256. inDegree = 280
  257. outDegree = circularAngle + inDegree
  258. firstHorizontal = False
  259. firstPositive = False
  260. secondHorizontal = True
  261. secondPositive = True
  262.  
  263. elif Leave == "Top":
  264.  
  265. circularAngle = 160
  266. inDegree = 280
  267. outDegree = circularAngle + inDegree
  268. firstHorizontal = False
  269. firstPositive = False
  270. secondHorizontal = False
  271. secondPositive = False
  272. else:
  273. print("XXX")
  274.  
  275. elif Enter == "Top":
  276.  
  277. if Leave == "Left":
  278.  
  279. circularAngle = 70
  280. inDegree = 105
  281. outDegree = circularAngle + inDegree
  282. firstHorizontal = False
  283. firstPositive = True
  284. secondHorizontal = True
  285. secondPositive = False
  286.  
  287. elif Leave == "Bottom":
  288.  
  289. circularAngle = 155
  290. inDegree = 105
  291. outDegree = circularAngle + inDegree
  292. firstHorizontal = False
  293. firstPositive = True
  294. secondHorizontal = False
  295. secondPositive = True
  296.  
  297. elif Leave == "Right":
  298.  
  299. circularAngle = 250
  300. inDegree = 105
  301. outDegree = circularAngle + inDegree
  302. firstHorizontal = False
  303. firstPositive = True
  304. secondHorizontal = True
  305. secondPositive = True
  306.  
  307. elif Leave == "Top":
  308.  
  309. circularAngle = 340
  310. inDegree = 105
  311. outDegree = circularAngle + inDegree
  312. firstHorizontal = False
  313. firstPositive = True
  314. secondHorizontal = False
  315. secondPositive = False
  316. else:
  317. print("XXX")
  318. else:
  319. print("XXX")
  320.  
  321. self.linear_routing(firstHorizontal, firstPositive, total_distance, True)
  322. self.circular_routing(inDegree, outDegree)
  323. self.linear_routing(secondHorizontal, secondPositive, self.totalS + total_distance, False)
  324.  
  325. auto_a = Car(Wh)
  326. auto_b = Car(Wh)
  327. auto_c = Car(Wh)
  328. auto_d = Car(Wh)
  329.  
  330. #auto_a.BigMove("Left", "Top")
  331. #auto_b.BigMove("Left", "Bottom")
  332. #auto_c.BigMove("Left", "Left")
  333. #auto_d.BigMove("Left", "Right")
  334. #auto_d.BigMove("Left", "Right")
  335.  
  336. t1 = threading.Thread(target=auto_a.BigMove, args=("Left", "Top"))
  337. t2 = threading.Thread(target=auto_b.BigMove, args=("Left", "Bottom"))
  338. t3 = threading.Thread(target=auto_c.BigMove, args=("Left", "Left"))
  339. t4 = threading.Thread(target=auto_d.BigMove, args=("Left", "Right"))
  340.  
  341. t1.start()
  342. t2.start()
  343. t3.start()
  344. t4.start()
  345.  
  346. #pygame.display.flip()
  347. #pygame.event.get()
  348. #pygame.quit()
Add Comment
Please, Sign In to add comment