From: Brian D. <deb...@ho...> - 2004-04-26 03:12:41
|
#!/usr/bin/python from PythonCardPrototype import model,dialog import os,string """ Brian Debuire Python Builder for PyGame Applications Pretty simple...but nice : ) TODO LIST: - Handle file and dir overwriting - Fix the problem when selecting a background bitmap - Documentation - Handle more user events - Fix tab index - Bugs fix Any comments, ideas or critics please let me know deb...@ho... """ class PythonBuilder(model.Background): def on_openBackground(self, event): pass def on_buttonGenerate_mouseClick(self, event): """ If the file already exists it'll be overwrited! If the directory already exists, an error will be produced! Maybe this could be fixed with an exception. """ # Create the project directory os.mkdir(os.path.join('output',self.components.textFileName.text)) # Create the main project file f = open(os.path.join('output',self.components.textFileName.text,self.components.textFileName.text+'.py'),'w') f.write(self.filestaff()) # If selected, create a separated file with all classes and functions needed if self.components.checkBoxSepClasses.checked == 1: f = open(os.path.join('output',self.components.textFileName.text,self.components.textFileName.text+'Classes'+'.py'),'w') f.write(self.classes()) # Show info about the project location dialog.messageDialog(self, 'Project '+self.components.textFileName.text+' saved to \\Output\\'+self.components.textFileName.text+' directory', 'Python Builder Message',dialog.ICON_INFORMATION,dialog.BUTTON_OK) def on_buttonExit_mouseClick(self, event): self.Close() def filestaff(self): """ This is the skeleton of a pygame project file. Is there too many code lines??? """ text = "#! /usr/bin/python\n\n" text += "import pygame" if self.components.textModules.text != '': text += "," + self.components.textModules.text text += "\n\n" text += "from pygame.locals import *\n\n" if self.components.checkBoxSepClasses.checked == 0: if self.components.textClasses.text <> '': for c in string.splitfields(self.components.textClasses.text,','): text += "class "+c+":\n" text += " def __init__(self):\n" text += " pass\n\n" else: text += "import "+self.components.textFileName.text+"Classes.*\n\n" text += "def main():\n" text += " pygame.init()\n" for mode in self.components.choiceDisplay.items: if mode == self.components.choiceDisplay.selected: text += " screen = pygame.display.set_mode(("+mode+"))\n" break text += " pygame.display.set_caption(\'"+self.components.textCaption.text+"\')\n" if self.components.comboBack.selected == "black": text += " back = pygame.Surface(screen.get_size())\n" text += " back.fill((0,0,0))\n" elif self.components.comboBack.selected == "white": text += " back = pygame.Surface(screen.get_size())\n" text += " back.fill((255,255,255))\n" elif self.components.comboBack.selected == "bitmap": text += " back = load_image(\"*.*\")\n" text += " back.fill((0,0,0))\n" text += " screen.blit(back,(0,0))\n" text += " pygame.display.flip()\n\n\n\n" text += " while 1:\n" text += " for event in pygame.event.get():\n" text += " if event.type == KEYDOWN:\n" text += " return\n\n\n\n" text += "if __name__ == \'__main__\': main()" return text def classes(self): """ A method for the functions and classes """ text = "import pygame\n\n" text += "from pygame.locals import *\n\n" text += self.extras() if self.components.textClasses.text <> '': for c in string.splitfields(self.components.textClasses.text,','): text += "class "+c+":\n" text += " def __init__(self):\n" text += " pass\n\n" return text def extras(self): """ A method that contains extra functions """ text = "" if self.components.checkPng.checked == 1 or self.components.checkImage.checked == 1 or self.components.checkFont.checked == 1: text += "import os\n\n" os.mkdir(os.path.join('output',self.components.textFileName.text,self.components.textDir.text)) if self.components.checkPng.checked == 1: text += "def load_png(name):\n" text += " fullname = os.path.join(\'"+self.components.textDir.text+"\', name)\n" text += " try:\n" text += " image = pygame.image.load(fullname)\n" text += " if image.get_alpha is None:\n" text += " image = image.convert()\n" text += " else:\n" text += " image = image.convert_alpha()\n" text += " except pygame.error, message:\n" text += " print 'Unable to load image:', fullname\n" text += " raise SystemExit, message\n" text += " return image\n\n" if self.components.checkImage.checked == 1: text += "def load_image(name):\n" text += " fullname = os.path.join(\'"+self.components.textDir.text+"\',name)\n" text += " try:\n" text += " image = pygame.image.load(fullname)\n" text += " except pygame.error, message:\n" text += " print \"Unable to load image :\",fullname\n" text += " raise SystemExit, message\n" text += " image = image.convert()\n" text += " return image\n\n" if self.components.checkFont.checked == 1: text += "def load_font(name):\n" text += " try:\n" text += " fullname = os.path.join(\'"+self.components.textDir.text+"\',name)\n" text += " except pygame.error, message:\n" text += " print \"Unable to load font :\",fullname\n" text += " raise SystemExit, message\n" text += " return fullname\n\n" return text if __name__ == '__main__': app = model.PythonCardApp(PythonBuilder) app.MainLoop() |