# .----------------------------------[ icedragon@quickfox.org ]--. # # | sample-circles.py | FS2 Circle Generation Script [EXAMPLE] | # # |--------------------------------------------------------------| # # | This script aims to demonstrate how to work with LIBFS2 and | # # | create 10 randomly sized circles wirh random colors in them. | # # | Just like the PORTGEN file, this is something to show how to | # # | work with the basics of the library and create graphical FS2 | # # | files from scratch. # `--------------------------------------------------------------' # ###--# Imports #--### # Imports the two classes we'll use - FS2_File and FS2_Shape along # with FLAG_ENCODED pseudo-constant. from libfs2 import * from random import randint ###--# Startup #--### # Assign an FS2 file class to a variable so we can deal with it. fs2 = FS2_File() # Generating 10 shapes. for nShape in range(10): # CIRCLE GENERATION: Forming a random radius of our circle and a list of colors # for each circular layer, not something you should be concerned about, just # for the sake of example and fun. =] radius = randint( 10,100 ) rList = [] for r in range( radius * 2 / 8 ): area = randint(2,31) * 8 for color in range( area, area + 8 ): rList.append( color ) # Creating a new shape with height/width big enough to fit the circle. shape = FS2_Shape( (radius*2,radius*2) ) # Since this is an FS2, we need to form an order of our own so all the shapes # here won't be replacing shape 0 in some FSH file. We can do it, but it's not # nice. ;) shape.shpReplace = nShape # Going through each pixel, checking if we're within the circle bounds and draw # a border with specific colors. for x in range(radius * 2): for y in range(radius * 2): num = (x*x + y*y)/radius # If we're within the area of the circle + its border... if num <= radius: # Setting 4 mirrored points in the graphical field, a point in each # of the 4 areas that will complete a circle 4 times faster than # doing it with one point and going through the entire array. Again, # the method should not bother you, the commands used should. points = [(radius+x,radius+y), (radius+x,radius-y), (radius-x,radius+y), (radius-x,radius-y)] for point in points: shape.setPixel( point, rList[num] ) # Shape generation complete - add it to the list of shapes in the file. fs2.shapes.append( shape ) # Informing of our progress. print "+ %2d: %3dx%3d (r=%2d) circle added!" % (nShape, shape.width, shape.height, radius) # Adding a tail to the file for personalization. Not a good thing, but Furc won't mind... ;) fs2.tail = "Created by Artex/IceDragon [http://icerealm.theden.ws/info/]" # When all is done, save the FS2 file to disk and terminate. fs2.saveFile('circles.fs2') print "Done."