Skip to content

Instantly share code, notes, and snippets.

@diVineProportion
Last active September 28, 2019 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diVineProportion/12a61a127e23222e5cca5009fa469dea to your computer and use it in GitHub Desktop.
Save diVineProportion/12a61a127e23222e5cca5009fa469dea to your computer and use it in GitHub Desktop.
import os
import math
import pygame
import requests
def distance_formula(x1, x2, y1, y2):
a = x2 - x1
b = y2 - y1
a = math.pow(a, 2)
b = math.pow(b, 2)
c = a + b
return math.sqrt(c)
# return math.sqrt(math.pow((x2-x1),2) + math.pow((y2-y1),2))
def main_loop():
# infinite loop
while True:
# for all detectable pygame events
for event in pygame.event.get():
# if detected event type is a key press
if event.type == pygame.KEYDOWN:
# if key pressed is escape
if event.key == pygame.K_ESCAPE:
return
"""
------------------------------------
UNCOMMENT FOR MOVING PLAYER POSITION
------------------------------------
map_objects = requests.get("http://localhost:8111/map_obj.json").json()
player_list = [el for el in map_objects if el['icon'] == 'Player']
player_position_pixels_x = player_list[0]["x"] * total_size_pixels_w
player_position_pixels_y = player_list[0]["y"] * total_size_pixels_h
v_player_line_start = player_position_pixels_x, background_image_posi_y
v_player_line_final = player_position_pixels_x, background_image_size_h
h_player_line_start = background_image_posi_x, player_position_pixels_y
h_player_line_final = background_image_size_w, player_position_pixels_y
player_position_pixels = [int(player_position_pixels_x), int(player_position_pixels_y)]
# we need to re-blit (draw) the canvas before we draw the lines on top of it
screen.blit(background_image_resize, (background_image_posi_x, background_image_posi_y))
pygame.draw.line(screen, COLOR_BLACK, v_player_line_start, v_player_line_final, 1)
pygame.draw.line(screen, COLOR_BLACK, h_player_line_start, h_player_line_final, 1)
pygame.draw.line(screen, COLOR_AIRFIELD, airfield_start, airfield_final, 5)
pygame.draw.circle(screen, COLOR_PLAYER, player_position_pixels, 2)
"""
# timer of sorts, ensures consitent FPS on all computers by forcing a wait
clock.tick(60)
# allows drawing the next frame ahead of time
pygame.display.flip()
pygame.display.update()
if __name__ == "__main__":
pygame.init()
clock = pygame.time.Clock()
print("-----------------------------------------------------------------------")
# (x,y) starting position of the window frame & background image
window_start_posi_x = window_start_posi_y = 0
background_image_posi_x = background_image_posi_y = 0
# (w,h) starting size of the window frame & background image
window_frame_size_w = window_frame_size_h = 1024
background_image_size_w = background_image_size_h = 1024
mimg = "http://localhost:8111/map.img"
with open("map.jpg", 'wb') as f:
map_req = requests.get(mimg, timeout=0.02)
f.write(map_req.content)
background_image_source = r'map.jpg'
# # path to the high resolution war thunder map image
# background_image_source = r'A:\WT_Maps\War Thunder maps 1.87\berlin_map.jpg'
# function that actually moves the window frame to our starting position
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (window_start_posi_x, window_start_posi_y)
# initialization of the canvas for which pygame will place the background image upon
screen = pygame.display.set_mode((window_frame_size_w, window_frame_size_h), pygame.NOFRAME)
# loading the background image and converting it to pygame image format
background_image_pygame = pygame.image.load(background_image_source).convert()
# control to transform the image (stretch, skew); we only needed it resized
background_image_resize = pygame.transform.scale(background_image_pygame,
(background_image_size_w, background_image_size_h))
# function to draw the transformed image to the canvas; remember screen was the canvas name
screen.blit(background_image_resize, (background_image_posi_x, background_image_posi_y))
# refresh the pygame window
# fetch the map size information and store into json object
map_objects = requests.get("http://localhost:8111/map_obj.json").json()
# fetch the map object information and store into json object
map_information = requests.get("http://localhost:8111/map_info.json").json()
# using the size from the fetched data, get the total map size in meters
total_size_meters_w = map_information['map_max'][0] - map_information['map_min'][0]
total_size_meters_h = map_information['map_max'][1] - map_information['map_min'][1]
# using the size from the fetched data, get the total map size in pixels
total_size_pixels_w = window_frame_size_w
total_size_pixels_h = window_frame_size_h
# print all details
print(total_size_meters_h, total_size_meters_w)
print("MAP INFO:", map_information)
print("MAP OBJ :", map_objects)
# condensed way of iterating through the list of map objects to lock onto the airfield and player
airfield_list = [el for el in map_objects if el['type'] == 'airfield']
player_list = [el for el in map_objects if el['icon'] == 'Player']
# using color from the list
COLOR_AIRFIELD = airfield_list[0]['color[]']
COLOR_PLAYER = player_list[0]['color[]']
# generic color definition
COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)
COLOR_RED = (255, 0, 0)
"""
the engine uses positional data in terms of a percentage of 1, or (0.000000 - 1.000000)
this ensures easy conversion to pixels for the browser map and meters for the API
daeditor3x - Mission Editor, Location Editor & Asset Viewer
usually some power of 2. EX: (2^x | where x == 15 || 16)
{
"grid_steps" : [ 8192.0, 8192.0 ],
"grid_zero" : [ -28672.0, 28672.0 ],
"map_generation" : 38,
"map_max" : [ 32768.0, 32768.0 ],
"map_min" : [ -32768.0, -32768.0 ]
}
Pixels:
TL corner: [0.00, 0.00]
BR corner: [1.00, 1.00]
TOTALSIZE: [1.00, 1.00]
Meters:
TL corner: [ 32768.0, 32768.0]
BR corner: [-32768.0, -32768.0]
TOTALSIZE: [ 65536.0, 65536.0]
32768.0 - (-32768.0) => 32768.0 + 32768.0 => 65536.0
"""
# multiplying by the total size of the map in pixels gives us pixels
airfield_start_pixels_x = airfield_list[0]['sx'] * total_size_pixels_w
airfield_start_pixels_y = airfield_list[0]['sy'] * total_size_pixels_h
airfield_final_pixels_x = airfield_list[0]['ex'] * total_size_pixels_w
airfield_final_pixels_y = airfield_list[0]['ey'] * total_size_pixels_h
# multiplying by the total size of the map in meters gives us meters
airfield_start_meters_x = airfield_list[0]['sx'] * total_size_meters_w
airfield_start_meters_y = airfield_list[0]['sy'] * total_size_meters_h
airfield_final_meters_x = airfield_list[0]['ex'] * total_size_meters_w
airfield_final_meters_y = airfield_list[0]['ey'] * total_size_meters_h
# grouping the separate x and y values into a tuple (x,y)
airfield_start = airfield_start_pixels_x, airfield_start_pixels_y
airfield_final = airfield_final_pixels_x, airfield_final_pixels_y
# converting player position x and y to player position x and y in meters
player_position_meters_x = player_list[0]["x"] * total_size_meters_w
player_position_meters_y = player_list[0]["y"] * total_size_meters_h
# converting player position x and y to player position x and y in pixels
player_position_pixels_x = player_list[0]["x"] * total_size_pixels_w
player_position_pixels_y = player_list[0]["y"] * total_size_pixels_h
# ..grouping to tuple
v_player_line_start = player_position_pixels_x, background_image_posi_y
v_player_line_final = player_position_pixels_x, background_image_size_h
h_player_line_start = background_image_posi_x, player_position_pixels_y
h_player_line_final = background_image_size_w, player_position_pixels_y
# putting in a list and casting to type:int
player_position_meters = [int(player_position_meters_x), int(player_position_meters_y)]
player_position_pixels = [int(player_position_pixels_x), int(player_position_pixels_y)]
# draw line from start(x,y) to final(x,y)
pygame.draw.line(screen, COLOR_BLACK, v_player_line_start, v_player_line_final, 1)
pygame.draw.line(screen, COLOR_BLACK, h_player_line_start, h_player_line_final, 1)
pygame.draw.line(screen, COLOR_AIRFIELD, airfield_start, airfield_final, 5)
# drawing circle centered at player_position_pixels (converted to tuple above) of size 2
pygame.draw.circle(screen, COLOR_PLAYER, player_position_pixels, 2)
# sorting out terms to input to distance formula | sqrt((x2-x1)^2 + (y2-y1)^2)
x1, y1 = background_image_posi_x, background_image_posi_y
x2, y2 = player_position_meters_x, player_position_meters_y
# print the results of running the coordinates through the distance formula
print("DISTANCE FROM (0,0) to player(x,y) in meters: {}".format(distance_formula(x1, y1, x2, y2)))
x2, y2 = player_position_pixels_x, player_position_pixels_y
# drawing line to visually represent the distance
pygame.draw.line(screen, COLOR_RED, (x1, y1), (x2, y2), 1)
# refresh
pygame.display.update()
# start the infinite loop and or moving map
main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment