lonestar

0x10c-inspired space game based on the DCPU-16
git clone git://squid-tech.com/lonestar.git
Log | Files | Refs | README

commit fa3018d25dbff20cb0cc33d2ce423166c6fcd98b
parent b715c358dd8b289c7e002f4bf23093bcf893adb7
Author: Josh Moore <jxm5210@rit.edu>
Date:   Sun, 24 Nov 2019 17:16:01 -0500

Add grid BFS pathfinding to hull draw

Diffstat:
Mutils/shipdraw/shipdraw.py | 98++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 90 insertions(+), 8 deletions(-)

diff --git a/utils/shipdraw/shipdraw.py b/utils/shipdraw/shipdraw.py @@ -18,22 +18,93 @@ drawingHull = True # handle mouse clicks def clicked(event): global drawingHull + global shiphull if drawingHull: - hullpointX = round(event.pos[0] / GRID_STEP) - hullpointY = round(event.pos[1] / GRID_STEP) - shiphull.append((hullpointX, hullpointY)) + hullpos = tuple(round(i / GRID_STEP) for i in event.pos) - if len(shiphull) > 1 and shiphull[len(shiphull) - 1] == shiphull[0]: + if len(shiphull) == 0: + shiphull.append(hullpos) + else: + shiphull = shiphull + hullPath(hullpos) + + if len(shiphull) > 1 and shiphull[-1] == shiphull[0]: drawingHull = False # draw the ship's outline (aka hull) -def drawOutline(hull): +def drawOutline(hull, preview): for i in range(1, len(hull)): hullPos = (hull[i][0] * GRID_STEP, hull[i][1] * GRID_STEP) prevhullPos = (hull[i - 1][0] * GRID_STEP, hull[i - 1][1] * GRID_STEP) - pygame.draw.line(screen, (0,0,0), hullPos, prevhullPos, 2) + + if preview: + pygame.draw.line(screen, (150,150,150), hullPos, prevhullPos, 3) + else: + pygame.draw.line(screen, (0,0,0), hullPos, prevhullPos, 3) + +# get point neighbors +def pointNeighbors(pos): + neighbors = [ + (pos[0], pos[1] - 1), + (pos[0] - 1, pos[1]), + (pos[0] + 1, pos[1]), + (pos[0], pos[1] + 1), + (pos[0] - 1, pos[1] - 1), + (pos[0] + 1, pos[1] - 1), + (pos[0] - 1, pos[1] + 1), + (pos[0] + 1, pos[1] + 1) + ] + + return neighbors + +# return true if the point lies in bounds, false otherwise +def validPoint(pos): + if (pos[0] < 0 or pos[0] > SCREEN_WIDTH / GRID_STEP): + return False + + if (pos[1] < 0 or pos[1] > SCREEN_HEIGHT / GRID_STEP): + return False + + if inList([shiphull[i] for i in range(2, len(shiphull))], pos): + return False + + return True + + +# draw hull cursor +def hullCursor(pos): + posX = pos[0] * GRID_STEP + posY = pos[1] * GRID_STEP + + pygame.draw.circle(screen, (255,0,0), (posX, posY), 3) + pygame.draw.circle(screen, (255,255,255), (posX, posY), 5, 1) +# check whether a point is in a point list +def inList(list, point): + for p in list: + if p == point: + return True + + return False + +# find the shortest grid path from the most recent point in the path to a new point +# point is the goal point +def hullPath(point): + queue = [[shiphull[-1]]] + visited = [shiphull[-1]] + while len(queue): + curpath = queue.pop(0) + + if curpath[-1] == point: + return curpath + + for p in pointNeighbors(curpath[-1]): + if validPoint(p) and not inList(visited, p): + queue.append(curpath + [p]) + visited.append(p) + + return [] + # main shipdraw loop while not done: @@ -61,6 +132,17 @@ while not done: pygame.draw.circle(screen, (100,100,100), (x, y), 2) # draw ship hull - drawOutline(shiphull) - + drawOutline(shiphull, False) + + # draw grid cursor for both the mouse position and last placed hull point + # also draw preview to next point + if drawingHull: + unitMousePos = tuple(round(i / GRID_STEP) for i in pygame.mouse.get_pos()) + hullCursor(unitMousePos) + if len(shiphull) > 0: + hullCursor(shiphull[-1]) + + # draw path preview + drawOutline(hullPath(unitMousePos), True) + pygame.display.flip() \ No newline at end of file