From dd6832293786ea0f3e7ba872893734db9fd1c30b Mon Sep 17 00:00:00 2001 From: SpitfireX Date: Tue, 11 Aug 2015 02:09:58 +0200 Subject: Some more gui.py improvements --- gui.py | 51 ++++++++++++++++++++++++++++++++++----------------- main.py | 6 +++--- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/gui.py b/gui.py index 7040ab0..5804055 100644 --- a/gui.py +++ b/gui.py @@ -25,36 +25,53 @@ clock = pygame.time.Clock() screensize=(1280, 720) screen=pygame.display.set_mode(screensize,HWSURFACE|DOUBLEBUF|RESIZABLE) -def debug_line(p1, p2, color): - global screen - p1win = world_to_win_pt(p1, c.player.center) - p2win = world_to_win_pt(p2, c.player.center) - gfxdraw.line(screen, p1win[0], p1win[1], p2win[0], p2win[1], color) - -def debug_box(rect, color, filled=False): +def draw_line(p1, p2, color, global_coords=True): + if global_coords: + p1 = world_to_win_pt(p1, c.player.center) + p2 = world_to_win_pt(p2, c.player.center) + + gfxdraw.line(screen, p1[0], p1[1], p2[0], p2[1], color) + +def draw_box(rect, color, filled=False, global_coords=True): + if global_coords: + t = ( + world_to_win_pt(rect[0][0], c.player.center), + world_to_win_pt(rect[0][1], c.player.center) + ) + rect = (t, rect[1]) + if filled: screen.fill(color, rect) else: gfxdraw.rectangle(screen, rect, color) -def debug_circle(pos, r, color, filled=False): +def draw_circle(pos, r, color, filled=False, global_coords=True): + if global_coords: + pos = ( + world_to_win_pt(pos[0], c.player.center), + world_to_win_pt(pos[1], c.player.center) + ) + if filled: gfxdraw.filled_circle(screen, pos[0], pos[1], r, color) else: gfxdraw.circle(screen, pos[0], pos[1], r, color) gfxdraw.aacircle(screen, pos[0], pos[1], r, color) -def debug_polygon(polygon, color, filled=False): - polygon = list(map(lambda x: world_to_win_pt(x, c.player.center), polygon)) - if filled: - gfxdraw.filled_polygon(screen, polygon, color) - else: - gfxdraw.polygon(screen, polygon, color) - gfxdraw.aapolygon(screen, polygon, color) +def draw_polygon(polygon, color, filled=False, global_coords=True): + if len(polygon) > 2: + if global_coords: + polygon = list(map(lambda x: world_to_win_pt(x, c.player.center), polygon)) + + if filled: + gfxdraw.filled_polygon(screen, polygon, color) + else: + gfxdraw.polygon(screen, polygon, color) + gfxdraw.aapolygon(screen, polygon, color) -def debug_path(path, color): +def draw_path(path, color, global_coords=True): for i in range(1, len(path)): - debug_line(path[i-1], path[i], color) + draw_line(path[i-1], path[i], color, global_coords) def update(): pygame.display.update() diff --git a/main.py b/main.py index af452fa..1fc04e4 100644 --- a/main.py +++ b/main.py @@ -57,7 +57,7 @@ while True: c.send_target(runaway_x, runaway_y) print ("Running away: " + str((runaway_x-c.player.center[0], runaway_y-c.player.center[1]))) - gui.debug_line(c.player.center, (runaway_x,runaway_y),(255,0,0)) + gui.draw_line(c.player.center, (runaway_x,runaway_y),(255,0,0)) else: food = list(filter(lambda x: x.is_food or x.mass <= sorted(c.player.own_cells, key = lambda x: x.mass)[0].mass * 0.75 and not x.is_virus, c.world.cells.values())) def dist(cell): return math.sqrt((cell.pos[0]-c.player.center[0])**2 + (cell.pos[1]-c.player.center[1])**2) @@ -65,13 +65,13 @@ while True: if len(food) > 0: c.send_target(food[0].pos[0], food[0].pos[1]) - gui.debug_line(c.player.center, food[0].pos,(0,0,255)) + gui.draw_line(c.player.center, food[0].pos,(0,0,255)) print("Found food at: " + str(food[0].pos)) else: rx = c.player.center[0] + random.randrange(-400, 401) ry = c.player.center[1] + random.randrange(-400, 401) c.send_target(rx, ry) - gui.debug_line(c.player.center, (rx, ry),(0,255,0)) + gui.draw_line(c.player.center, (rx, ry),(0,255,0)) print("Nothing to do, heading to random destination: " + str((rx, ry))) gui.update() -- cgit v1.2.1