lonestar

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

commit b715c358dd8b289c7e002f4bf23093bcf893adb7
parent a1e6be2da69f991c8c0fdb5de91f7001b08887f4
Author: Josh Moore <jxm5210@rit.edu>
Date:   Sat, 23 Nov 2019 14:25:06 -0500

Add shipdraw

Diffstat:
Autils/shipdraw/shipdraw.py | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+), 0 deletions(-)

diff --git a/utils/shipdraw/shipdraw.py b/utils/shipdraw/shipdraw.py @@ -0,0 +1,66 @@ +import pygame + +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 600 +GRID_STEP = 25 + +# pygame initialization +pygame.init() +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) +pygame.display.set_caption("LoneStar ShipDraw") +done = False + +drawgrid = True + +shiphull = [] +drawingHull = True + +# handle mouse clicks +def clicked(event): + global drawingHull + if drawingHull: + hullpointX = round(event.pos[0] / GRID_STEP) + hullpointY = round(event.pos[1] / GRID_STEP) + shiphull.append((hullpointX, hullpointY)) + + if len(shiphull) > 1 and shiphull[len(shiphull) - 1] == shiphull[0]: + drawingHull = False + +# draw the ship's outline (aka hull) +def drawOutline(hull): + 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) + + + +# main shipdraw loop +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_g: + # toggle grid + drawgrid = not drawgrid + elif event.key == pygame.K_ESCAPE: + # quit ShipDraw + done = True + elif event.type == pygame.MOUSEBUTTONDOWN: + # handle mouse clicks + clicked(event) + + # draw background + screen.fill((220,220,220)) + + # draw point grid + if drawgrid: + for x in range(0, SCREEN_WIDTH + 1, GRID_STEP): + for y in range(0, SCREEN_HEIGHT + 1, GRID_STEP): + pygame.draw.circle(screen, (100,100,100), (x, y), 2) + + # draw ship hull + drawOutline(shiphull) + + pygame.display.flip()+ \ No newline at end of file