Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: AdamAtomic/flixel
base: master
...
head fork: narlynephi/flixel
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Commits on Sep 17, 2012
@narlynephi narlynephi Better flx.py with argparse
Better help:

usage: flx2.py [-h] [--width N] [--height N] [--zoom N] [--src SRC]
               [--preloader PRELOADER] [--menustate MENUSTATE]
               [--playstate PLAYSTATE] [--noflex] [--noact]
               PROJECT




Script to setup basic Flixel project files




positional arguments:
  PROJECT               Name of the new project




optional arguments:
  -h, --help            show this help message and exit
  --width N             Width of your game in 'true' pixels (ignoring zoom)
  --height N            Height of your game in 'true' pixels (ignoring zoom)
  --zoom N              How chunky you want your pixels
  --src SRC             Name of the source folder under the project folder (if
                        there is one!)
  --preloader PRELOADER
                        Name of the preloader class
  --menustate MENUSTATE
                        Name of the menu state class
  --playstate PLAYSTATE
                        Name of the play state class
  --noflex              Don't create a Default.css (for use with Flex Builder)
  --noact               Show where files will be created and what options will
                        be used

Example of the --noact flag:

Will run with the following options:
	width      : 320
	height     : 240
	zoom       : 2
	noflex     : NO
	src        : 'src'
	preloader  : 'Preloader'
	play state : 'PlayState'
	menu state : 'MenuState'
	project    : 'TestProject'
	




The following files will be written to:
	src/Preloader.as
	src/PlayState.as
	src/TestProject.as
	src/MenuState.as
	src/Default.css

There's also slightly different behavior with what directory structures the script expects
11adeb0
Showing with 197 additions and 140 deletions.
  1. +197 −140 flx.py
View
337 flx.py
@@ -1,146 +1,203 @@
-#! /usr/local/bin/python
-import os
-import sys
+#!/usr/bin/env python
+
+import os, sys
+import argparse
+
+parser = argparse.ArgumentParser(description="Script to setup basic Flixel project files")
+
+# the only required argument of this script
+parser.add_argument("project", metavar="PROJECT", type=str, default=None,
+ help="Name of the new project")
+
+# optional arguments
+parser.add_argument("--width", metavar="N", type=int, default=320,
+ help="Width of your game in 'true' pixels (ignoring zoom)")
+parser.add_argument("--height", metavar="N", type=int, default=240,
+ help="Height of your game in 'true' pixels (ignoring zoom)")
+parser.add_argument("--zoom", metavar="N", type=int, default=2,
+ help="How chunky you want your pixels")
+parser.add_argument("--src", metavar="SRC", type=str, default="src",
+ help="Name of the source folder under the project folder (if there is one!)")
+parser.add_argument("--preloader", metavar="PRELOADER", type=str, default="Preloader",
+ help="Name of the preloader class")
+parser.add_argument("--menustate", metavar="MENUSTATE", type=str, default="MenuState",
+ help="Name of the menu state class")
+parser.add_argument("--playstate", metavar="PLAYSTATE", type=str, default="PlayState",
+ help="Name of the play state class")
+parser.add_argument("--noflex", action="store_true", default=False,
+ help="Don't create a Default.css (for use with Flex Builder)")
+parser.add_argument("--noact", action="store_true", default=False,
+ help="Show where files will be created and what options will be used")
+
+args = parser.parse_args()
#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!)
-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
-playState = 'PlayState' # Name of play state class
-
-#Get name of project
-if len(sys.argv) <= 1:
- sys.exit(0)
-project = sys.argv[1]
-
-#Generate basic game class
-filename = project+'/'+src+project+'.as';
-try:
- fo = open(filename, 'w')
-except IOError:
- print('Can\'t open '+filename+' for writing.')
- 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)
-fo.close()
-
-#Generate preloader
-filename = project+'/'+src+preloader+'.as';
-try:
- fo = open(filename, 'w')
-except IOError:
- print('Can\'t open '+filename+' for writing.')
- 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)
-fo.close()
-
-#Generate Default.css
-if flexBuilder:
- filename = project+'/'+src+'Default.css';
+width = args.width # Width of your game in 'true' pixels (ignoring zoom)
+height = args.height # Height of your game in 'true' pixels
+zoom = args.zoom # How chunky you want your pixels
+src = args.src # Name of the source folder under the project folder (if there is one!)
+preloader = args.preloader # Name of the preloader class
+menuState = args.menustate # Name of menu state class
+playState = args.playstate # Name of play state class
+flexBuilder = not args.noflex # Whether or not to generate a Default.css file
+project = args.project # name of the new project
+
+preloaderPath = os.path.join(src, preloader+".as")
+playStatePath = os.path.join(src, playState+".as")
+projectPath = os.path.join(src, project+".as")
+menuStatePath = os.path.join(src, menuState+".as")
+defaultCssPath = os.path.join(src, "Default.css")
+
+if args.noact:
+ settings = """
+Will run with the following options:
+ width : %d
+ height : %d
+ zoom : %d
+ noflex : %s
+ src : '%s'
+ preloader : '%s'
+ play state : '%s'
+ menu state : '%s'
+ project : '%s'
+ """ % (
+ width, height, zoom, "NO" if flexBuilder else "YES", src, preloader, playState, menuState, project,
+ )
+ print settings
+
+ paths = [preloaderPath, playStatePath, projectPath, menuStatePath]
+ if flexBuilder:
+ paths.append(defaultCssPath)
+
+ print("""
+The following files will be written to:
+%s
+ """ % "\n".join(["\t"+path for path in paths]))
+
+ exit(0)
+
+
+
+def generate_file(path, code):
try:
- fo = open(filename, 'w')
+ fo = open(path, 'w')
except IOError:
- print('Can\'t open '+filename+' for writing.')
- sys.exit(0)
- fo.write('Add this: "-defaults-css-url Default.css"\nto the project\'s additonal compiler arguments.')
+ print("Can't open '%s' for writing" % path)
+ sys.exit()
+
+ fo.write(code)
fo.close()
-#Generate game menu
-filename = project+'/'+src+menuState+'.as';
-try:
- fo = open(filename, 'w')
-except IOError:
- print('Can\'t open '+filename+' for writing.')
- 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)
-fo.close()
-
-#Generate basic game state
-filename = project+'/'+src+playState+'.as';
-try:
- fo = open(filename, 'w')
-except IOError:
- print('Can\'t open '+filename+' for writing.')
- 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')
-fo.writelines(lines)
-fo.close()
-
-print('Successfully generated game class, preloader, menu state, and play state.')
+ print("Wrote to '%s'" % path)
+
+
+# ---------------------------
+# Generate project code
+# ---------------------------
+project_code = """
+package
+{
+ import org.flixel.*;
+ [SWF(width="%d", height="%d", backgroundColor="#000000")]
+ [Frame(factoryClass="Preloader")]
+ public class %s extends FlxGame
+ {
+ public function %s()
+ {
+ super(%d, %d, %s, %d);
+ }
+ }
+}
+""" % (width, height, project, project, width, height, menuState, zoom)
+generate_file(projectPath, project_code)
+
+
+# ---------------------------
+# Generate preloader code
+# ---------------------------
+preloader_code = """
+package
+{
+ import org.flixel.system.FlxPreloader;
+ public class %s extends FlxPreloader
+ {
+ public function %s()
+ {
+ className = "%s";
+ super();
+ }
+ }
+}
+""" % (preloader, preloader, project)
+generate_file(preloaderPath, preloader_code)
+
+
+# ---------------------------
+# Generate Default.css
+# ---------------------------
+if flexBuilder:
+ default_css_code = 'Add this: "-defaults-css-url Default.css"\nto the project\'s additonal compiler arguments.'
+ generate_file(defaultCssPath, default_css_code)
+
+
+# ---------------------------
+# Generate menu state code
+# ---------------------------
+menu_state_code = """
+package
+{
+ import org.flixel.*;
+
+ public class %s extends FlxState
+ {
+ override public function create(): void
+ {
+ var text:FlxText;
+ text = new FlxText(0, FlxG.height/2-10, FlxG.width, "%s");
+ text.size = 16;
+ text.alignment = "center";
+ add(text);
+ text = new FlxText(FlxG.width/2-50, FlxG.height-20, 100, "click to play");
+ text.alignment = "center";
+ add(text);
+
+ FlxG.mouse.show();
+ }
+
+ public override function update():void
+ {
+ super.update();
+
+ if(FlxG.mouse.justPressed())
+ {
+ FlxG.mouse.hide();
+ FlxG.switchState(new PlayState());
+ }
+ }
+ }
+}
+""" % (menuState, project)
+generate_file(menuStatePath, menu_state_code)
+
+
+# ---------------------------
+# Generate play state code
+# ---------------------------
+play_state_code = """
+package
+{
+ import org.flixel.*;
+
+ public class %s extends FlxState
+ {
+ override public function create(): void
+ {
+ add(new FlxText(0, 0, 100, "INSERT GAME HERE"));
+ }
+ }
+}
+""" % (playState)
+generate_file(playStatePath, play_state_code)
+
+
+print("\nDone!")

No commit comments for this range

Something went wrong with that request. Please try again.