From 8e0c7b280b220c7fdb277aedba94ed293eeccf45 Mon Sep 17 00:00:00 2001 From: Ivan Tam Date: Wed, 14 Sep 2011 23:05:05 -0700 Subject: [PATCH] Updates flx.py to be more Pythonic * String literals for the file templates are now just one string instead of a list of strings. * Variables in the templates are interpolated into the strings rather than concatenated. * The project directory is created if it doesn't yet exist. * The script now generates proper file and directory paths for Windows, Mac, Linux. * The comment generated in Default.css is now commented out. --- flx.py | 203 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 111 insertions(+), 92 deletions(-) diff --git a/flx.py b/flx.py index 9b025dad..3e1a3095 100644 --- a/flx.py +++ b/flx.py @@ -1,12 +1,12 @@ #! /usr/local/bin/python -import os +import os, os.path import sys #BASIC SCRIPT PRESETS width = 320 # Width of your game in 'true' pixels (ignoring zoom) height = 240 # Height of your game in 'true' pixels zoom = 2 # How chunky you want your pixels -src = 'src/' # Name of the source folder under the project folder (if there is one!) +src = 'src' # Name of the source folder under the project folder (if there is one!) preloader = 'Preloader' # Name of the preloader class flexBuilder = True # Whether or not to generate a Default.css file menuState = 'MenuState' # Name of menu state class @@ -14,133 +14,152 @@ #Get name of project if len(sys.argv) <= 1: + print "Usage:" + print "\t%s " % sys.argv[0] sys.exit(0) + project = sys.argv[1] +#Make the directory structure if it doesn't yet exist +project_dir = os.path.join(project, src) +if not os.path.isdir(project_dir): + print "Creating project directories: %s" % project_dir + os.makedirs(project_dir) + #Generate basic game class -filename = project+'/'+src+project+'.as'; +filename = os.path.join(project, src, '%s.as' % project) try: fo = open(filename, 'w') except IOError: - print('Can\'t open '+filename+' for writing.') + print('Can\'t open %s for writing.' % filename) sys.exit(0) -lines = [] -lines.append('package\r\n') -lines.append('{\r\n') -lines.append('\timport org.flixel.*;\r\n') -lines.append('\t[SWF(width="'+str(width*zoom)+'", height="'+str(height*zoom)+'", backgroundColor="#000000")]\r\n') -lines.append('\t[Frame(factoryClass="Preloader")]\r\n') -lines.append('\r\n') -lines.append('\tpublic class '+project+' extends FlxGame\r\n') -lines.append('\t{\r\n') -lines.append('\t\tpublic function '+project+'()\r\n') -lines.append('\t\t{\r\n') -lines.append('\t\t\tsuper('+str(width)+','+str(height)+','+menuState+','+str(zoom)+');\r\n') -lines.append('\t\t}\r\n') -lines.append('\t}\r\n') -lines.append('}\r\n') -fo.writelines(lines) + +lines = """package +{ +\timport org.flixel.*; +\t[SWF(width="%s", height="%s", backgroundColor="#000000")] +\t[Frame(factoryClass="Preloader")] + +\tpublic class %s extends FlxGame +\t{ +\t\tpublic function %s() +\t\t{ +\t\t\tsuper(%s,%s,%s,%s); +\t\t} +\t} +} +""" % ( + str(width*zoom), + str(height*zoom), + project, + project, + str(width), + str(height), + menuState, + str(zoom) +) +fo.write(lines) fo.close() #Generate preloader -filename = project+'/'+src+preloader+'.as'; +filename = os.path.join(project, src, '%s.as' % preloader) try: fo = open(filename, 'w') except IOError: - print('Can\'t open '+filename+' for writing.') + print('Can\'t open %s for writing.' % filename) sys.exit(0) -lines = [] -lines.append('package\r\n') -lines.append('{\r\n') -lines.append('\timport org.flixel.system.FlxPreloader;\r\n') -lines.append('\r\n') -lines.append('\tpublic class '+preloader+' extends FlxPreloader\r\n') -lines.append('\t{\r\n') -lines.append('\t\tpublic function '+preloader+'()\r\n') -lines.append('\t\t{\r\n') -lines.append('\t\t\tclassName = "'+project+'";\r\n') -lines.append('\t\t\tsuper();\r\n') -lines.append('\t\t}\r\n') -lines.append('\t}\r\n') -lines.append('}\r\n') -fo.writelines(lines) + +lines = """package +{ +\timport org.flixel.system.FlxPreloader; + +\tpublic class %s extends FlxPreloader +\t{ +\t\tpublic function %s() +\t\t{ +\t\t\tclassName = "%s"; +\t\t\tsuper(); +\t\t} +\t} +}""" % (preloader, preloader, project) +fo.write(lines) fo.close() #Generate Default.css if flexBuilder: - filename = project+'/'+src+'Default.css'; + filename = os.path.join(project, src, 'Default.css') try: fo = open(filename, 'w') except IOError: - print('Can\'t open '+filename+' for writing.') + print('Can\'t open %s for writing.' % filename) sys.exit(0) - fo.write('Add this: "-defaults-css-url Default.css"\nto the project\'s additonal compiler arguments.') + fo.write('/* Add this: "-defaults-css-url Default.css"\nto the project\'s additonal compiler arguments. */') fo.close() #Generate game menu -filename = project+'/'+src+menuState+'.as'; +filename = os.path.join(project, src, '%s.as' % menuState) try: fo = open(filename, 'w') except IOError: - print('Can\'t open '+filename+' for writing.') + print('Can\'t open %s for writing.' % filename) sys.exit(0) -lines = [] -lines.append('package\r\n') -lines.append('{\r\n') -lines.append('\timport org.flixel.*;\r\n') -lines.append('\r\n') -lines.append('\tpublic class '+menuState+' extends FlxState\r\n') -lines.append('\t{\r\n') -lines.append('\t\toverride public function create():void\r\n') -lines.append('\t\t{\r\n') -lines.append('\t\t\tvar t:FlxText;\r\n') -lines.append('\t\t\tt = new FlxText(0,FlxG.height/2-10,FlxG.width,"'+project+'");\r\n') -lines.append('\t\t\tt.size = 16;\r\n') -lines.append('\t\t\tt.alignment = "center";\r\n') -lines.append('\t\t\tadd(t);\r\n') -lines.append('\t\t\tt = new FlxText(FlxG.width/2-50,FlxG.height-20,100,"click to play");\r\n') -lines.append('\t\t\tt.alignment = "center";\r\n') -lines.append('\t\t\tadd(t);\r\n') -lines.append('\t\t\t\r\n') -lines.append('\t\t\tFlxG.mouse.show();\r\n') -lines.append('\t\t}\r\n') -lines.append('\r\n') -lines.append('\t\toverride public function update():void\r\n') -lines.append('\t\t{\r\n') -lines.append('\t\t\tsuper.update();\r\n') -lines.append('\r\n') -lines.append('\t\t\tif(FlxG.mouse.justPressed())\r\n') -lines.append('\t\t\t{\r\n') -lines.append('\t\t\t\tFlxG.mouse.hide();\r\n') -lines.append('\t\t\t\tFlxG.switchState(new PlayState());\r\n') -lines.append('\t\t\t}\r\n') -lines.append('\t\t}\r\n') -lines.append('\t}\r\n') -lines.append('}\r\n') -fo.writelines(lines) + +lines = """package +{ +\timport org.flixel.*; + +\tpublic class %s extends FlxState +\t{ +\t\toverride public function create():void +\t\t{ +\t\t\tvar t:FlxText; +\t\t\tt = new FlxText(0,FlxG.height/2-10,FlxG.width,"%s"); +\t\t\tt.size = 16; +\t\t\tt.alignment = "center"; +\t\t\tadd(t); +\t\t\tt = new FlxText(FlxG.width/2-50,FlxG.height-20,100,"click to play"); +\t\t\tt.alignment = "center"; +\t\t\tadd(t); +\t\t\t +\t\t\tFlxG.mouse.show(); +\t\t} + +\t\toverride public function update():void +\t\t{ +\t\t\tsuper.update(); + +\t\t\tif(FlxG.mouse.justPressed()) +\t\t\t{ +\t\t\t\tFlxG.mouse.hide(); +\t\t\t\tFlxG.switchState(new PlayState()); +\t\t\t} +\t\t} +\t} +}""" % (menuState, project) +fo.write(lines) fo.close() #Generate basic game state -filename = project+'/'+src+playState+'.as'; +filename = os.path.join(project, src, '%s.as' % playState) try: fo = open(filename, 'w') except IOError: - print('Can\'t open '+filename+' for writing.') + print('Can\'t open %s for writing.' % filename) sys.exit(0) -lines = [] -lines.append('package\r\n') -lines.append('{\r\n') -lines.append('\timport org.flixel.*;\r\n') -lines.append('\r\n') -lines.append('\tpublic class '+playState+' extends FlxState\r\n') -lines.append('\t{\r\n') -lines.append('\t\toverride public function create():void\r\n') -lines.append('\t\t{\r\n') -lines.append('\t\t\tadd(new FlxText(0,0,100,"INSERT GAME HERE"));\r\n') -lines.append('\t\t}\r\n') -lines.append('\t}\r\n') -lines.append('}\r\n') + +lines = """package +{ +\timport org.flixel.*; + +\tpublic class %s extends FlxState +\t{ +\t\toverride public function create():void +\t\t{ +\t\t\tadd(new FlxText(0,0,100,"INSERT GAME HERE")); +\t\t} +\t} +}""" % playState fo.writelines(lines) fo.close() -print('Successfully generated game class, preloader, menu state, and play state.') \ No newline at end of file +print('Successfully generated game class, preloader, menu state, and play state.')