def editLevel(self): '''Places objects on the level''' mousepos = pygame.mouse.get_pos() mousepos = alignToGrid((mousepos[0] - 195, mousepos[1])) objname = self.selbox.getValue() if objname != '': objimg = self.objdict[objname] self.objlist.append(placedObject(objname, gamefunc.imgLoad(objimg), \ mousepos, self.bgimage))
def __init__(self, app, objdict, selbox): self.app = app self.objdict = objdict self.selbox = selbox self.levelmap = gamefunc.imgLoad('levelmap.png')[0] self.image = self.levelmap.copy() self.bgimage = self.levelmap.copy() self.imagewidget = gui.Image(self.image, x=200, y=0) self.imagewidget.connect(CLICK, self.clickedOn) self.app.add(self.imagewidget) self.state = 'chooselevel' self.selbox.b1.connect(CLICK, self.saveLevel) self.selbox.b2.connect(CLICK, self.clearLevel) self.selbox.b3.connect(CLICK, self.toChooseLevel)
def main(): clock = pygame.time.Clock() objdict = dbload('objdict.db') imgcache = {'':'noimage'} window = pygame.display.set_mode((1224, 768)) imgcache = imgCacheFill(objdict, imgcache) gui.init(myscreen = window) app = gui.App(width=1224, height=768) spritegroup = pygame.sprite.Group() cursor = mouseSprite(gamefunc.imgLoad('ball1.png')) spritegroup.add(cursor) objselbox = objSelectBox(app, objdict) leveleditor = levelEditingArea(app, objdict, objselbox) objimg = 'noimage' oldobjimg = 'noimage' running = True while running: clock.tick(50) events = pygame.event.get() for event in events: if event.type == QUIT: running = False try: objimg = objdict[objselbox.getValue()] except: pass if leveleditor.state == 'editlevel': if objimg == oldobjimg: cursor.update() else: cursor.update(imgcache[objimg]) else: cursor.update('noimage') oldobjimg = objimg app.run(events) leveleditor.imagewidget.dirty = True app.draw() spritegroup.clear(leveleditor.image, leveleditor.bgimage) spritegroup.draw(leveleditor.image) gui.update_display()
def openLevel(self, levelname): self.bgimage.fill((255,255,255)) self.image.fill((255,255,255)) self.objlist = [] levelpath = os.path.join('levels', levelname) try: levelfile = open(levelpath, 'r') except: return leveldata = pickle.load(levelfile) print "Loading: ", leveldata for objname, datalist in leveldata.iteritems(): objimg = self.objdict[objname] objimgrect = gamefunc.imgLoad(objimg) for i in datalist: print "rect.topleft: ", i self.objlist.append(placedObject(objname, objimgrect,\ i[0], self.bgimage)) pygame.display.get_surface().blit(self.bgimage, (195,0)) self.image.blit(self.bgimage, (0,0)) pygame.display.flip()
def imgCacheFill(objdict, imgcache): for objname, imgname in objdict.iteritems(): imgcache[imgname] = gamefunc.imgLoad(imgname)[0] return imgcache