Advertisement
Guest User

Untitled

a guest
Sep 20th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.32 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. import math, sys
  5.  
  6. SCREEN_SIZE_X = 800
  7. SCREEN_SIZE_Y = 600
  8.  
  9. MAP_SIZE_X = 2000
  10. MAP_SIZE_Y = 2000
  11.  
  12. RIGHT = 0
  13. LEFT = 1
  14.  
  15. def clamp(x, minimum, maximum):
  16.     return max(minimum, min(maximum, x))
  17.  
  18. class Camera():
  19.         def __init__(self, min_x, min_y, max_x, max_y):
  20.                 self.min_x, self.min_y = min_x, min_y
  21.                 self.max_x, self.max_y = max_x, max_y
  22.                 self.x = min_x
  23.                 self.y = min_y
  24.  
  25.         def center_on(self, x, y):
  26.                 new_x = x - SCREEN_SIZE_X // 2
  27.                 new_y = y - SCREEN_SIZE_Y // 2
  28.  
  29.                 self.x = clamp(new_x, self.min_x, self.max_x)
  30.                 self.y = clamp(new_y, self.min_y, self.max_y)
  31.  
  32. class AI():
  33.         def __init__(self, masks):
  34.                 self.image = pygame.image.load('player.png').convert()
  35.                 self.image.set_colorkey((255, 255, 255))
  36.                 self.rect = self.image.get_rect()
  37.  
  38.                 self.original = self.image
  39.                                                                                                                                                                                    
  40.                 self.virtual_x, self.virtual_y = 1400, 972
  41.  
  42.                 self.angle = 0
  43.                 self.speed = 4.5
  44.  
  45.                 self.out_mask, self.obj_mask = masks
  46.  
  47.         def steerToLure(self, lure, dist):
  48.                 """
  49.  
  50.                Get the X and Y components of the velocity to steer to the lure.
  51.  
  52.                """
  53.  
  54.                 adj = math.fabs(lure.virtual_x - self.virtual_x)
  55.                 hyp = dist
  56.  
  57.                 self.angle = math.acos(adj/hyp)
  58.  
  59.                 self.speedx = math.cos(self.angle) * self.speed
  60.                 self.speedy = math.sin(self.angle) * self.speed
  61.  
  62.                 if lure.virtual_x < self.virtual_x:
  63.                         self.speedx *= -1
  64.  
  65.                 if lure.virtual_y < self.virtual_y:
  66.                         self.speedy *= -1
  67.  
  68.         def getDistanceToLure(self, lure):
  69.                 return math.sqrt( math.pow( math.fabs(lure.virtual_x - self.virtual_x), 2 ) + math.pow ( math.fabs(lure.virtual_y - self.virtual_y ), 2 ) )
  70.  
  71.         def update(self, lure):
  72.                 dist = self.getDistanceToLure(lure)
  73.  
  74.                 if dist > 100:
  75.                         lure.stop()
  76.  
  77.                 else:
  78.                         lure.start()
  79.  
  80.                 self.steerToLure(lure, dist)
  81.  
  82.                 self.virtual_x += self.speedx
  83.                 self.virtual_y += self.speedy
  84.  
  85.                 self.rect = self.image.get_rect(center = (self.virtual_x, self.virtual_y))
  86.  
  87.         def draw(self, screen, camera):
  88.                 screen.blit(self.image, self.rect.move(-camera.x, -camera.y))
  89.  
  90.                 start_pos = self.virtual_x - camera.x, self.virtual_y - camera.y
  91.                 end_pos = start_pos[0] + (self.speedx * self.speed) * 2, start_pos[1]
  92.                 pygame.draw.line(screen, (0, 0, 255), start_pos, end_pos, 2)
  93.  
  94.                 end_pos = start_pos[0], start_pos[1] + (self.speedy * self.speed) * 2
  95.                 pygame.draw.line(screen, (0, 255, 0), start_pos, end_pos, 2)
  96.  
  97. class Lure():
  98.         def __init__(self):
  99.                 self.image = pygame.image.load('lure.png').convert()
  100.                 self.rect = self.image.get_rect()
  101.  
  102.                 self.virtual_x, self.virtual_y = 1400, 972 #initial position of the lure
  103.  
  104.                 self.angle = 0
  105.                 self.speed = 5
  106.  
  107.                 #arbitary list of points of the path (in real conditions this would be areas and the points would be choosen randomly)
  108.                 self.points = [(1400, 368), (1832, 188), (1772, 64), (1128, 100), (1028, 1152), (708, 1152), (628, 136), (148, 76), (108, 232), (484, 320), (456, 460), (100, 380), (76, 476), (404, 624), (180, 880), (612, 1476), (980, 1512), (1208, 1920), (1436, 1900), (1400, 972)]
  109.  
  110.                 self.current_point = 0
  111.  
  112.                 self.started = True
  113.  
  114.         def getDistanceToNextPoint(self):
  115.                 return math.sqrt( math.pow( math.fabs(self.points[self.current_point][0] - self.virtual_x), 2 ) + math.pow ( math.fabs(self.points[self.current_point][1] - self.virtual_y ), 2 ) )
  116.  
  117.         def steerToNextPoint(self, dist):
  118.                 """
  119.  
  120.                Steer to the next point of the path.
  121.  
  122.                """
  123.  
  124.                 adj = math.fabs(self.points[self.current_point][0] - self.virtual_x)
  125.                 hyp = dist
  126.  
  127.                 self.angle = math.acos(adj/hyp)
  128.  
  129.                 self.speedx = math.cos(self.angle) * self.speed
  130.                 self.speedy = math.sin(self.angle) * self.speed
  131.  
  132.                 if self.points[self.current_point][0] < self.virtual_x:
  133.                         self.speedx *= -1
  134.  
  135.                 if self.points[self.current_point][1] < self.virtual_y:
  136.                         self.speedy *= -1
  137.  
  138.         def updateTargetPoint(self):
  139.                 if self.current_point < len(self.points) - 1:
  140.                         self.current_point += 1
  141.  
  142.                 else:
  143.                         self.current_point = 0
  144.  
  145.         def start(self):
  146.                 self.started = True
  147.  
  148.         def stop(self):
  149.                 self.started = False
  150.  
  151.         def update(self):
  152.                 #check if the lure and the attached AI entity are not too far from each other
  153.                 #if there are too far, stop the lure
  154.                 dist = self.getDistanceToNextPoint()
  155.                 if dist > self.speed:
  156.                         self.steerToNextPoint(dist)
  157.  
  158.                 else:
  159.                         self.updateTargetPoint()
  160.  
  161.                 if self.started:
  162.                         self.virtual_x += self.speedx
  163.                         self.virtual_y += self.speedy
  164.  
  165.                 self.rect = self.image.get_rect(center = (self.virtual_x, self.virtual_y))
  166.  
  167.         def draw(self, screen, camera):
  168.                 screen.blit(self.image, self.rect.move(-camera.x, -camera.y))
  169.  
  170. def main():
  171.         pygame.init()
  172.  
  173.         screen = pygame.display.set_mode((SCREEN_SIZE_X, SCREEN_SIZE_Y), HWSURFACE|DOUBLEBUF)
  174.  
  175.         background = pygame.image.load('map.png').convert()
  176.  
  177.         masks = []
  178.         for img in ['out.png', 'obj.png']:
  179.                 mask_img = pygame.image.load(img).convert()
  180.                 mask_img.set_colorkey((255, 255, 255))
  181.  
  182.                 masks.append(pygame.mask.from_surface(mask_img))
  183.  
  184.         lure = Lure()
  185.         car = AI(masks)
  186.  
  187.         clock = pygame.time.Clock()
  188.         camera = Camera(0, 0, MAP_SIZE_X - SCREEN_SIZE_X, MAP_SIZE_Y - SCREEN_SIZE_Y)
  189.  
  190.         loop = True
  191.         while loop:
  192.                 clock.tick(60)
  193.  
  194.                 for event in pygame.event.get():
  195.                         if event.type == QUIT:
  196.                                 loop = False
  197.  
  198.                         if event.type == KEYDOWN:
  199.                                 if event.key == K_ESCAPE:
  200.                                         loop = False
  201.  
  202.                 lure.update()
  203.                 car.update(lure)
  204.  
  205.                 camera.center_on(car.virtual_x, car.virtual_y)
  206.  
  207.                 screen.blit(background, (-camera.x, -camera.y))
  208.  
  209.                 lure.draw(screen, camera)
  210.                 car.draw(screen, camera)
  211.  
  212.                 pygame.display.flip()
  213.  
  214. if __name__=="__main__":
  215.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement