lonestar

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

commit 3e1da9b27ced2e3808b649279ab5ad0575aeda4d
parent e08154a9f36542982ac66d8e10c8d75d6322df51
Author: Josh Moore <jxm5210@rit.edu>
Date:   Sun, 24 Nov 2019 18:15:59 -0500

Add basic file saving to shipdraw

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

diff --git a/utils/shipdraw/shipdraw.py b/utils/shipdraw/shipdraw.py @@ -1,5 +1,8 @@ import pygame +import sys +import xml.etree.ElementTree as ET + SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 GRID_STEP = 25 @@ -108,6 +111,32 @@ def hullPath(point): visited.append(p) return [] + +# determine whether or not the user can save the ship they've drawn +# the ship must have a finished hull +def canSaveShip(): + if drawingHull: + print("You must finish drawing a hull before saving.") + return False + + return True + +# export the drawn ship to an XML file for import into LoneStar +def saveShip(): + if not canSaveShip(): + return + + ship = ET.Element('ship') + + hull = ET.SubElement(ship, 'hull') + + + # write the ship to the file + data = ET.tostring(ship, encoding='unicode') + file = open("Ships/" + sys.argv[1] + ".ship", "w") + file.write(data) + + print("Ship '" + sys.argv[1] + "' saved!") # main shipdraw loop @@ -119,6 +148,9 @@ while not done: if event.key == pygame.K_g: # toggle grid drawgrid = not drawgrid + if event.key == pygame.K_s: + # save the ship + saveShip() elif event.key == pygame.K_ESCAPE: # quit ShipDraw done = True