Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def __init__(self, wh):
- super(Car, self).__init__()
- self.cars = pygame.sprite.RenderPlain(self)
- self.pos = Vector2()
- self.image = pygame.Surface(wh, pygame.SRCALPHA)
- pygame.draw.rect(self.image, RED, (0, 0, meter_per_pixel(4), meter_per_pixel(1.75)))
- self.original_image = self.image
- self.rect = self.image.get_rect(center=self.pos)
- self.aPrev = 0
- self.vPrev = 0
- self.sPrev = 0
- self.totalS = 0
- self.maxAcc = 15
- self.safeDistance = False
- def find_acc(self):
- # self.acc = self.maxacc
- a = random.randint(-10, 30)
- return a
- def moveto(self, new_pos):
- self.rect.x = new_pos.x
- self.rect.y = new_pos.y
- self.pos = self.rect.center
- def linear_routing(self, horizontal, positive, total_distance, first):
- if horizontal:
- if positive:
- if first:
- self.moveto(getEnterPositions("Left"))
- else:
- self.moveto(getLeavePositions("Right"))
- else:
- if first:
- self.moveto(getEnterPositions("Right"))
- else:
- self.moveto(getLeavePositions("Left"))
- else:
- if positive:
- if first:
- self.moveto(getEnterPositions("Top"))
- else:
- self.moveto(getLeavePositions("Bottom"))
- else:
- if first:
- self.moveto(getEnterPositions("Bottom"))
- else:
- self.moveto(getLeavePositions("Top"))
- while self.totalS <= total_distance:
- a = self.find_acc()
- a_avg = 0.5 * (a + self.aPrev)
- v = self.vPrev + a_avg * deltaT
- s = self.sPrev + self.vPrev * deltaT + 0.5 * a_avg * deltaT * deltaT
- deltaS = self.vPrev * deltaT + 0.5 * a_avg * deltaT * deltaT
- pos = self.rect
- if horizontal:
- if positive:
- pos.x += deltaS
- else:
- pos.x -= deltaS
- else:
- if positive:
- pos.y += deltaS
- else:
- pos.y -= deltaS
- self.moveto(pos)
- self.totalS += deltaS
- self.aPrev = a
- self.vPrev = v
- self.sPrev = s
- if horizontal:
- self.image = pygame.transform.rotate(self.original_image, 0)
- else:
- self.image = pygame.transform.rotate(self.original_image, 90)
- Background()
- self.cars.draw(screen)
- pygame.display.flip()
- #pygame.event.get()
- clock.tick(30)
- # time.sleep(0.1)
- return self.totalS
- def circular_routing(self, in_degree, out_degree):
- current_degree = in_degree
- pos = Vector2()
- distance = Vector2()
- distance.x = pixel_x(0) - self.rect.x
- distance.y = pixel_y(0) - self.rect.y
- radius = distance.length()
- while current_degree <= out_degree:
- a = self.find_acc()
- a_avg = 0.5 * (a + self.aPrev)
- v = self.vPrev + a_avg * deltaT
- s = self.sPrev + self.vPrev * deltaT + 0.5 * a_avg * deltaT * deltaT
- s0 = straightRoad
- alpha0 = in_degree * math.pi / 180
- alpha = (s - s0) / (radius * math.pi)
- current_rad = alpha + alpha0
- current_degree = (current_rad * 180 / math.pi) # % 360
- pos.x = pixel_x(0) + (math.cos(current_rad) * radius)
- pos.y = pixel_y(0) - (math.sin(current_rad) * radius)
- self.moveto(pos)
- self.totalS = s
- self.aPrev = a
- self.vPrev = v
- self.sPrev = s
- self.image = pygame.transform.rotate(self.original_image, -(90 - current_degree))
- Background()
- self.cars.draw(screen)
- pygame.display.flip()
- #pygame.event.get()
- clock.tick(30)
- # time.sleep(0.1)
- return current_degree, self.totalS
- def BigMove(self, Enter, Leave):
- firstHorizontal = False
- firstPositive = False
- secondHorizontal = False
- secondPositive = False
- inDegree = 0
- outDegree = 0
- total_distance = straightRoad
- if Enter == "Left":
- if Leave == "Left":
- circularAngle = 330 # kavsakta kac derece döndügü
- inDegree = 190 # kavsaga nereden girdigi
- outDegree = (circularAngle + inDegree) # % 360 # cikis derecesi
- firstHorizontal = True
- firstPositive = True
- secondHorizontal = True
- secondPositive = False
- elif Leave == "Bottom":
- circularAngle = 70
- inDegree = 190
- outDegree = (circularAngle + inDegree) # % 360
- firstHorizontal = True
- firstPositive = True
- secondHorizontal = False
- secondPositive = True
- elif Leave == "Right":
- circularAngle = 160
- inDegree = 190
- outDegree = (circularAngle + inDegree) # % 360
- firstHorizontal = True
- firstPositive = True
- secondHorizontal = True
- secondPositive = True
- elif Leave == "Top":
- circularAngle = 250
- inDegree = 190
- outDegree = (circularAngle + inDegree) # % 360
- firstHorizontal = True
- firstPositive = True
- secondHorizontal = False
- secondPositive = False
- else:
- print("XXX")
- elif Enter == "Right":
- if Leave == "Left":
- circularAngle = 150
- inDegree = 20
- outDegree = circularAngle + inDegree
- firstHorizontal = True
- firstPositive = False
- secondHorizontal = True
- secondPositive = False
- elif Leave == "Bottom":
- circularAngle = 240
- inDegree = 20
- outDegree = circularAngle + inDegree
- firstHorizontal = True
- firstPositive = False
- secondHorizontal = False
- secondPositive = True
- elif Leave == "Right":
- circularAngle = 330
- inDegree = 20
- outDegree = circularAngle + inDegree
- firstHorizontal = True
- firstPositive = False
- secondHorizontal = True
- secondPositive = True
- elif Leave == "Top":
- circularAngle = 70
- inDegree = 20
- outDegree = circularAngle + inDegree
- firstHorizontal = True
- firstPositive = False
- secondHorizontal = False
- secondPositive = False
- else:
- print("XXX")
- elif Enter == "Bottom":
- if Leave == "Left":
- circularAngle = 250
- inDegree = 280
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = False
- secondHorizontal = True
- secondPositive = False
- elif Leave == "Bottom":
- circularAngle = 340
- inDegree = 280
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = False
- secondHorizontal = False
- secondPositive = True
- elif Leave == "Right":
- circularAngle = 70
- inDegree = 280
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = False
- secondHorizontal = True
- secondPositive = True
- elif Leave == "Top":
- circularAngle = 160
- inDegree = 280
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = False
- secondHorizontal = False
- secondPositive = False
- else:
- print("XXX")
- elif Enter == "Top":
- if Leave == "Left":
- circularAngle = 70
- inDegree = 105
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = True
- secondHorizontal = True
- secondPositive = False
- elif Leave == "Bottom":
- circularAngle = 155
- inDegree = 105
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = True
- secondHorizontal = False
- secondPositive = True
- elif Leave == "Right":
- circularAngle = 250
- inDegree = 105
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = True
- secondHorizontal = True
- secondPositive = True
- elif Leave == "Top":
- circularAngle = 340
- inDegree = 105
- outDegree = circularAngle + inDegree
- firstHorizontal = False
- firstPositive = True
- secondHorizontal = False
- secondPositive = False
- else:
- print("XXX")
- else:
- print("XXX")
- self.linear_routing(firstHorizontal, firstPositive, total_distance, True)
- self.circular_routing(inDegree, outDegree)
- self.linear_routing(secondHorizontal, secondPositive, self.totalS + total_distance, False)
- auto_a = Car(Wh)
- auto_b = Car(Wh)
- auto_c = Car(Wh)
- auto_d = Car(Wh)
- #auto_a.BigMove("Left", "Top")
- #auto_b.BigMove("Left", "Bottom")
- #auto_c.BigMove("Left", "Left")
- #auto_d.BigMove("Left", "Right")
- #auto_d.BigMove("Left", "Right")
- t1 = threading.Thread(target=auto_a.BigMove, args=("Left", "Top"))
- t2 = threading.Thread(target=auto_b.BigMove, args=("Left", "Bottom"))
- t3 = threading.Thread(target=auto_c.BigMove, args=("Left", "Left"))
- t4 = threading.Thread(target=auto_d.BigMove, args=("Left", "Right"))
- t1.start()
- t2.start()
- t3.start()
- t4.start()
- #pygame.display.flip()
- #pygame.event.get()
- #pygame.quit()
Add Comment
Please, Sign In to add comment