Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- import math, sys
- SCREEN_SIZE_X = 800
- SCREEN_SIZE_Y = 600
- MAP_SIZE_X = 2000
- MAP_SIZE_Y = 2000
- RIGHT = 0
- LEFT = 1
- def clamp(x, minimum, maximum):
- return max(minimum, min(maximum, x))
- class Camera():
- def __init__(self, min_x, min_y, max_x, max_y):
- self.min_x, self.min_y = min_x, min_y
- self.max_x, self.max_y = max_x, max_y
- self.x = min_x
- self.y = min_y
- def center_on(self, x, y):
- new_x = x - SCREEN_SIZE_X // 2
- new_y = y - SCREEN_SIZE_Y // 2
- self.x = clamp(new_x, self.min_x, self.max_x)
- self.y = clamp(new_y, self.min_y, self.max_y)
- class AI():
- def __init__(self, masks):
- self.image = pygame.image.load('player.png').convert()
- self.image.set_colorkey((255, 255, 255))
- self.rect = self.image.get_rect()
- self.original = self.image
- self.virtual_x, self.virtual_y = 1400, 972
- self.angle = 0
- self.speed = 4.5
- self.out_mask, self.obj_mask = masks
- def steerToLure(self, lure, dist):
- """
- Get the X and Y components of the velocity to steer to the lure.
- """
- adj = math.fabs(lure.virtual_x - self.virtual_x)
- hyp = dist
- self.angle = math.acos(adj/hyp)
- self.speedx = math.cos(self.angle) * self.speed
- self.speedy = math.sin(self.angle) * self.speed
- if lure.virtual_x < self.virtual_x:
- self.speedx *= -1
- if lure.virtual_y < self.virtual_y:
- self.speedy *= -1
- def getDistanceToLure(self, lure):
- 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 ) )
- def update(self, lure):
- dist = self.getDistanceToLure(lure)
- if dist > 100:
- lure.stop()
- else:
- lure.start()
- self.steerToLure(lure, dist)
- self.virtual_x += self.speedx
- self.virtual_y += self.speedy
- self.rect = self.image.get_rect(center = (self.virtual_x, self.virtual_y))
- def draw(self, screen, camera):
- screen.blit(self.image, self.rect.move(-camera.x, -camera.y))
- start_pos = self.virtual_x - camera.x, self.virtual_y - camera.y
- end_pos = start_pos[0] + (self.speedx * self.speed) * 2, start_pos[1]
- pygame.draw.line(screen, (0, 0, 255), start_pos, end_pos, 2)
- end_pos = start_pos[0], start_pos[1] + (self.speedy * self.speed) * 2
- pygame.draw.line(screen, (0, 255, 0), start_pos, end_pos, 2)
- class Lure():
- def __init__(self):
- self.image = pygame.image.load('lure.png').convert()
- self.rect = self.image.get_rect()
- self.virtual_x, self.virtual_y = 1400, 972 #initial position of the lure
- self.angle = 0
- self.speed = 5
- #arbitary list of points of the path (in real conditions this would be areas and the points would be choosen randomly)
- 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)]
- self.current_point = 0
- self.started = True
- def getDistanceToNextPoint(self):
- 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 ) )
- def steerToNextPoint(self, dist):
- """
- Steer to the next point of the path.
- """
- adj = math.fabs(self.points[self.current_point][0] - self.virtual_x)
- hyp = dist
- self.angle = math.acos(adj/hyp)
- self.speedx = math.cos(self.angle) * self.speed
- self.speedy = math.sin(self.angle) * self.speed
- if self.points[self.current_point][0] < self.virtual_x:
- self.speedx *= -1
- if self.points[self.current_point][1] < self.virtual_y:
- self.speedy *= -1
- def updateTargetPoint(self):
- if self.current_point < len(self.points) - 1:
- self.current_point += 1
- else:
- self.current_point = 0
- def start(self):
- self.started = True
- def stop(self):
- self.started = False
- def update(self):
- #check if the lure and the attached AI entity are not too far from each other
- #if there are too far, stop the lure
- dist = self.getDistanceToNextPoint()
- if dist > self.speed:
- self.steerToNextPoint(dist)
- else:
- self.updateTargetPoint()
- if self.started:
- self.virtual_x += self.speedx
- self.virtual_y += self.speedy
- self.rect = self.image.get_rect(center = (self.virtual_x, self.virtual_y))
- def draw(self, screen, camera):
- screen.blit(self.image, self.rect.move(-camera.x, -camera.y))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_SIZE_X, SCREEN_SIZE_Y), HWSURFACE|DOUBLEBUF)
- background = pygame.image.load('map.png').convert()
- masks = []
- for img in ['out.png', 'obj.png']:
- mask_img = pygame.image.load(img).convert()
- mask_img.set_colorkey((255, 255, 255))
- masks.append(pygame.mask.from_surface(mask_img))
- lure = Lure()
- car = AI(masks)
- clock = pygame.time.Clock()
- camera = Camera(0, 0, MAP_SIZE_X - SCREEN_SIZE_X, MAP_SIZE_Y - SCREEN_SIZE_Y)
- loop = True
- while loop:
- clock.tick(60)
- for event in pygame.event.get():
- if event.type == QUIT:
- loop = False
- if event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- loop = False
- lure.update()
- car.update(lure)
- camera.center_on(car.virtual_x, car.virtual_y)
- screen.blit(background, (-camera.x, -camera.y))
- lure.draw(screen, camera)
- car.draw(screen, camera)
- pygame.display.flip()
- if __name__=="__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement