Last active
May 21, 2020 18:16
-
-
Save max-kov/1fb20815ee2c165aad072c556ef5f6b1 to your computer and use it in GitHub Desktop.
The two guy problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import math | |
start_x = 50 | |
start_y = 150 | |
start_dist = 300 | |
top_guy = [start_x,start_dist+start_y] | |
bottom_guy = [start_x,start_y] | |
vel = 0.1 | |
angle = 0 | |
resolution = [1000, 500] | |
pygame.init() | |
window = pygame.display.set_mode(resolution) | |
font = pygame.font.Font(pygame.font.get_default_font(),20) | |
dist = math.sqrt((bottom_guy[0]-top_guy[0])**2 + (bottom_guy[1] - top_guy[1])**2) | |
dist_text = font.render("distance="+str(dist), False, (255,255,255)) | |
window.blit(dist_text, (10,10)) | |
pygame.draw.circle(window, (0,0,255), top_guy, 3) | |
pygame.draw.circle(window, (255,0,0), bottom_guy, 3) | |
pygame.display.update() | |
while 1: | |
pygame.draw.line(window, (0,0,0),bottom_guy, top_guy) | |
top_guy_next = [top_guy[0]+vel, top_guy[1]] | |
bottom_guy_next = [bottom_guy[0] + math.sin(angle)*vel, bottom_guy[1] + math.cos(angle)*vel] | |
pygame.draw.line(window,(255,0,0),bottom_guy, bottom_guy_next) | |
pygame.draw.line(window,(0,0,255), [start_x, start_y+start_dist], top_guy_next) | |
bottom_guy = bottom_guy_next | |
top_guy = top_guy_next | |
pygame.draw.line(window, (255,255,255), bottom_guy, top_guy) | |
angle = math.atan((top_guy[0]-bottom_guy[0])/(top_guy[1]-bottom_guy[1])) | |
window.set_at((round(top_guy[0]),round(top_guy[1])), (0,0,255)) | |
window.set_at((round(bottom_guy[0]),round(bottom_guy[1])), (255,0,0)) | |
pygame.draw.rect(window, (0,0,0), (0,0,500,100)) | |
dist = math.sqrt((bottom_guy[0]-top_guy[0])**2 + (bottom_guy[1] - top_guy[1])**2) | |
dist_text = font.render("distance="+str(dist), False, (255,255,255)) | |
window.blit(dist_text, (10,10)) | |
pygame.display.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment