Pygrr PolyArt (part 3)

Here we are, the grand finale! Let's finish it off by coding saving, opening and exporting to Pygrr...

Let's switch around the UI a bit to begin with. It made no sense to not save the colours of the object, so let's add that to the top left, and let's create some file options on the right hand side! Also, note the icon of the application... Pygrr!

Cool stuff! Now, what do we do... Let's work on "clearing" the data first. This should just wipe the screen, and replace it with this default square you can see. We don't want people accidentally clicking it, so let's set up a nice confirmation box. Tkinter has a nice class for this: "messagebox".

Heyhey! Now for saving... 

For this, I'm going to use my programming crush - JSON. JSON stands for JavaScript Object Notation, which is (from Wikipedia):

JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values).

Well, that's a lot of jargon to unpack there... Let me translate. JSON is a file format used for storing data in a way that's easy for humans to read. It stores data in a dictionary format, meaning that it will store a key (name of an attribute), and the definition for that. Hope that helps!

So, why do I love it? I don't know, it's just so easy to work with, and the end result looks absolutely gorgeous! Let's create an example format for .pygrr files.

{

    "type": "POLYGON",

    "smooth": true,

    "fill": "light green",

    "outline": "black",

    "width": 3,

    "points": [

        [

            -5.0,

            5.0

        ],

        [

            -5.0,

            -5.0

        ],

        [

            5.0,

            -5.0

        ],

        [

            5.0,

            5.0

        ]

    ]

}

Doesn't it look amazing!? Try and see if you can understand the file from that! 

As there'll be different types of custom models, for example, complex polygons or pixel art, these all are gonna fit under the .pygrr extension, so I need a "type" attribute. This tells Pygrr what type of model it is! Writing this data to a file is pretty easy, so let's do the UI for this...

Awesome! And that JSON data is all saved perfectly, now, let's allow users to "open" a file. For this, we'll scan all the .pygrr files in the current directory, and display all of them that have the "POLYGON" type - as that's all this program can open.

Now we hook up some messageboxes, and we're done with this program! Onto importing into Pygrr!

For this, we'll make another function in the Model subclass. We'll call it "from_file", and we'll pass in the name of the file, and, optionally, how much to scale the file by. (just noticed I didn't record me making this model... Oops!)

import pygrr


player = pygrr.Polygon()

bone_model = pygrr.Model.from_file("bone.pygrr",scale=10)

player.set_model(bone_model)


while True: # this is the game loop!

    pygrr.next_frame()

Let's check this code works:

Epic! Let's record a nice vid of me creating a model and importing it!

Well, I'm not the best at art... As you can see towards the end of the video, off-centreing the object has a big impact on the way it rotates, as the anchor point is the centre...

Almost at alpha release now!

Isaac, over and out...

Popular posts from this blog

Blog post #0 - Hello, world!

The principles of Pygrr

An introduction to Pygrr...