def main(): scr = psp2d.Screen() img = psp2d.Image('background.png') simg1 = psp2d.Image(160, 272) simg1.blit(0, 0, 160, 272, img, 0, 0) simg2 = psp2d.Image(160, 272) simg2.blit(320, 0, 160, 272, img, 0, 0) tr = psp2d.Transform(psp2d.TR_PLUS, 20.0) tr.apply(simg1) img.blit(0, 0, 160, 272, simg1, 0, 0) tr = psp2d.Transform(psp2d.TR_MULT, 0.5) tr.apply(simg2) img.blit(0, 0, 160, 272, simg2, 320, 0) scr.blit(0, 0, img.width, img.height, img, 0, 0) scr.swap() while True: pad = psp2d.Controller() if pad.circle: break
def main(): img = psp2d.Image('orig.jpg') scr = psp2d.Screen() class CBProgress(ProgressBar): def update(self, value, total): super(CBProgress, self).update(value, total) scr.clear(psp2d.Color(0, 0, 0)) self.draw(scr, 0, 0) scr.swap() p = CBProgress('Monochrome') t0 = time.time() psp2d.MonochromeInPlaceFilter(200).apply(img, p.update) print 'Monochrome: %.2f seconds' % (time.time() - t0) img.saveToFile('result01.png') p = CBProgress('Erosion') t0 = time.time() img = psp2d.ErosionFilter(psp2d.StructuringElement(3)).apply(img, p.update) print 'Erosion: %.2f seconds' % (time.time() - t0) img.saveToFile('result02.png') p = CBProgress('Dilation') t0 = time.time() img = psp2d.DilationFilter(psp2d.StructuringElement(3)).apply( img, p.update) print 'Dilation: %.2f seconds' % (time.time() - t0) img.saveToFile('result03.png') rset = RectangleSet() p = CBProgress('Cutting') t0 = time.time() for y in xrange(img.height): for x in xrange(img.width): if not rset.isIn(x, y): p = img.getPixel(x, y) if p.red == 0 and p.green == 0 and p.blue == 0: rset.add(Rectangle(*img.boundingRectangle(x, y))) p.update(y, img.height) print 'Rectangles: %.2f seconds' % (time.time() - t0) for i, r in enumerate(RectangleSet(rset).rectangles): del img # Or else, MemoryError... img = psp2d.Image('orig.jpg') r.draw(img) img.saveToFile('result%02d.png' % (i + 4))
def test_blit(self): screen = psp2d.Screen() img = psp2d.Image('background.png') screen.blit(img) screen.swap() img = psp2d.Image(480, 272) img.blit(screen) img.saveToFile('test.png') h = self.hashFile('test.png') self.failIf(h != '73f23b452de36ca58ab5a65d72c2ebd5', h)
def main(): bg = psp2d.Image('background.png') img = psp2d.Image('sprite.png') scr = psp2d.Screen() scr.blit(img, dx=0, dy=0, blend=True) scr.blit(img, dx=240, dy=136, dw=128, dh=128, blend=True) scr.blit(img, dx=0, dy=136, dw=32, blend=True) scr.swap() while True: pad = psp2d.Controller() if pad.circle: break
def create_spritesheet(urls): sprites = [] for u in urls: if type(u) is str: sprites.append(psp2d.Image(u)) elif type(u) is list: r = [] for uu in u: r.append(psp2d.Image(uu)) sprites.append(r) #Direction = north = 0 elif (type(u) is tuple): sprites.append((psp2d.Image(u[0]), psp2d.Image(u[1]))) #Direction = north = 0 return sprites
def main(): scr = psp2d.Screen() img = psp2d.Image('background.png') img2 = psp2d.Image(200, 200) img2.clear(psp2d.Color(255, 0, 0, 128)) img.blit(img2, dx = 30, dy = 30, blend = True) scr.blit(img) scr.swap() while True: pad = psp2d.Controller() if pad.circle: break
def main(): screen = psp2d.Screen() font = psp2d.Font('font.png') bgimg = psp2d.Image('background.png') screen.blit(bgimg) font.drawText(screen, 0, 0, 'Connecting...') screen.swap() def cb(s): screen.blit(bgimg) if s >= 0: font.drawText(screen, 0, 0, 'State: %d/4' % s) else: font.drawText(screen, 0, 0, 'Connected. IP: %s' % pspnet.getIP()) screen.swap() pspnet.connectToAPCTL(1, cb) a = Acceptor() a.create_socket(socket.AF_INET, socket.SOCK_STREAM) a.bind(('', 23)) a.listen(5) thr = threading.Thread(target=runloop) thr.setDaemon(True) thr.start() while True: pad = psp2d.Controller() if pad.circle: break
def main(): scr = psp2d.Screen() fnt = psp2d.Font('font_small.png') dy = fnt.textHeight('') + 5 while True: pad = psp2d.Controller() img = psp2d.Image(480, 272) img.clear(psp2d.Color(0, 0, 0)) fnt.drawText(img, 0, 0, 'Analog X: %d' % pad.analogX) fnt.drawText(img, 0, dy, 'Analog Y: %d' % pad.analogY) fnt.drawText(img, 0, 2 * dy, 'Square: %d' % int(pad.square)) fnt.drawText(img, 0, 3 * dy, 'Circle: %d' % int(pad.circle)) fnt.drawText(img, 0, 4 * dy, 'Cross: %d' % int(pad.cross)) fnt.drawText(img, 0, 5 * dy, 'Triangle: %d' % int(pad.triangle)) fnt.drawText(img, 0, 6 * dy, 'Left: %d' % int(pad.left)) fnt.drawText(img, 0, 7 * dy, 'Right: %d' % int(pad.right)) fnt.drawText(img, 0, 8 * dy, 'Up: %d' % int(pad.up)) fnt.drawText(img, 0, 9 * dy, 'Down: %d' % int(pad.down)) fnt.drawText(img, 0, 10 * dy, 'Left trigger: %d' % int(pad.l)) fnt.drawText(img, 0, 11 * dy, 'Right trigger: %d' % int(pad.r)) scr.blit(img) scr.swap()
def main(): screen = psp2d.Screen() font = psp2d.Font('font.png') bgimg = psp2d.Image('background.png') screen.blit(bgimg) font.drawText(screen, 0, 0, 'Connecting...') screen.swap() def cb(s): screen.blit(bgimg) if s >= 0: font.drawText(screen, 0, 0, 'State: %d/4' % s) else: font.drawText(screen, 0, 0, 'Connected. IP: %s' % pspnet.getIP()) screen.swap() pspnet.connectToAPCTL(1, cb) srv = MSServer(('', 80), MSHandler) thr = threading.Thread(target=srv.serve_forever) thr.setDaemon(True) thr.start() while True: pad = psp2d.Controller() if pad.circle: break pspnet.disconnectAPCTL()
def Paint(self, drw, x, y, w, h, focused): img = Widget.Paint(self, drw, x, y, w, h, focused) # Arrows if self.first != 0: img.blit(self.up, dx=(w - 16) // 2, dy=0, blend=True) if self.first + self.height < len(self.choices): img.blit(self.down, dx=(w - 16) // 2, dy=h - 16, blend=True) # Choices dy = (h - 32) // self.height curY = 16 bg = psp2d.Image(w, dy) for idx in xrange(self.first, min(self.first + self.height, len(self.choices))): if idx == self.selection: bg.clear(psp2d.Color(255, 0, 0, 200)) else: bg.clear(psp2d.Color(0, 0, 0, 0)) self.font.drawText(bg, 0, 0, str(self.choices[idx])) img.blit(bg, dx=0, dy=curY, blend=True) curY += dy drw.blit(img, dx=x, dy=y, blend=True)
def __init__(self): psp2d.Timer.__init__(self, 1000 / 25) # 25 fps self.img = psp2d.Image('sprite.png') self.x = 0 self.y = 200 self.u = 2
def __init__(self, clearer=None, height=10, font=None): Widget.__init__(self, clearer) self.height = height self.choices = [] self.up = psp2d.Image('up.png') self.down = psp2d.Image('down.png') self.selection = None self.first = None if font is None: self.font = psp2d.Font('font_small.png') else: self.font = font self.minW = 16 self.minH = 32 + self.font.textHeight(' ') * height
def __init__(self, msg, name='progress'): self.img = psp2d.Image(name + '.png') self.bgi = psp2d.GrayFilter().apply(self.img) self.msg = msg self.frac = 0 self.w, self.h = self.img.size
def realize(self, font): """You should call this when you're finished adding items to the menu.""" self.w = 0 self.h = 0 for item in self.items: w, h = item.getDimension(font) w += 20 h += 10 self.w = max(self.w, w) self.h = max(self.h, h) self.abg = psp2d.Image(self.w, self.h) self.abg.clear(psp2d.Color(0, 0, 255, 200)) self.bg = psp2d.Image(self.w, self.h) self.bg.clear(psp2d.Color(0, 0, 255, 100))
def test_to_image(self): "Draw text to image (with alpha != 0)" img = psp2d.Image(200, 200) img.clear(psp2d.Color(127, 127, 127, 127)) self.font.drawText(img, 0, 0, 'Jérôme') img.saveToFile('test.png') h = self.hashFile('test.png') self.failIf(h != '9757122d8962cac6317da2f561b3a822', h)
def main(msg='Hello, world!'): screen = psp2d.Screen() bgimg = psp2d.Image('background.png') font = psp2d.Font('font.png') ## pspsnd.setMusicVolume(128) ## music = pspsnd.Music('stranglehold.xm', loop = True) ## music.start() widths = [] totalw = 0 for c in msg: widths.append(font.textWidth(c)) totalw += font.textWidth(c) y = (272 - font.textHeight(msg)) / 2 x = (480 - totalw) / 2 angle = 0.0 menu = Menu(10, 10) amplitude = FloatMenuItem(20, 0.5, 'Amplitude') step = FloatMenuItem(0.2, 0.02, 'Step') slope = FloatMenuItem(0.2, 0.02, 'Slope') menu.addItem(amplitude) menu.addItem(step) menu.addItem(slope) menu.realize(font) while True: screen.blit(bgimg) menu.draw(font, screen) dx = 0 for idx, c in enumerate(msg): font.drawText( screen, x + dx, y + amplitude.value * math.sin(angle + slope.value * idx), c) dx += widths[idx] screen.swap() angle += step.value pad = psp2d.Controller() if pad.circle: break menu.update(pad)
def main(): scr = psp2d.Screen() img = psp2d.Image('background.png') def change(x, y, color): color.red = min(255, color.red + 20) color.green = min(255, color.green + 20) color.blue = min(255, color.blue + 20) return True simg = psp2d.Image(20, 20) simg.blit(100, 100, 20, 20, img, 0, 0) tr = psp2d.Transform(change) tr.apply(simg) img.blit(0, 0, 20, 20, simg, 100, 100) scr.blit(0, 0, img.width, img.height, img, 0, 0) scr.swap() while True: pad = psp2d.Controller() if pad.circle: break
def main(): scr = psp2d.Screen() img = psp2d.Image('background.png') scr.blit(img) font = psp2d.Font('font.png') msg = 'Hello, world!' font.drawText(scr, (480 - font.textWidth(msg)) / 2, 20, msg) scr.swap() while True: pad = psp2d.Controller() if pad.circle: break
def danzeff_load(): global initialized, guiStringsSize, keyBits if (initialized): return for a in range(guiStringsSize): keyBits[a] = psp2d.Image(guiStrings[a]) if (keyBits[a] == None): for b in range(a): keyBits[b] = None initialized = False return initialized = True return
def main(): img = psp2d.Image('sprite.png') sprites = [Sprite(img) for unused in xrange(30)] screen = psp2d.Screen() batch = psp2d.BlitBatch() for sprite in sprites: batch.add(sprite) batching = False while True: if batching: while True: screen.clear(psp2d.Color(0, 0, 0)) for sprite in sprites: sprite.nextFrame() batch.blit() screen.swap() pad = psp2d.Controller() if pad.cross: batching = False time.sleep(0.2) break else: while True: screen.clear(psp2d.Color(0, 0, 0)) for sprite in sprites: sprite.nextFrame() img, x, y = sprite.data screen.blit(img, dx=x, dy=y, blend=True) screen.swap() pad = psp2d.Controller() if pad.cross: batching = True time.sleep(0.2) break
def main(msgs): screen = psp2d.Screen() bgimg = psp2d.Image('background.png') font = psp2d.Font('font.png') widths = [] totalw = [] for msg in msgs: widths.append([]) totalw.append(0) for c in msg: widths[-1].append(font.textWidth(c)) totalw[-1] += font.textWidth(c) y = (272 - font.textHeight('')) / 2 xs = [] for tw in totalw: xs.append((480 - tw) / 2) angle = 0.0 while True: screen.blit(bgimg) dy = 0 for i, msg in enumerate(msgs): dx = 0 for idx, c in enumerate(msg): font.drawText(screen, xs[i] + dx, y + dy + 20 * math.sin(angle + 0.2 * idx), c) dx += widths[i][idx] dy += font.textHeight('') + 20 screen.swap() angle += 0.2 pad = psp2d.Controller() if pad.circle: break
def Paint(self, drw, x, y, w, h, focused): img = psp2d.Image(w, h) self.clearer.Clear(img, focused) return img
def main(): bgimg = psp2d.Image('background.png') choice = ChoiceCtrl(height=10) description = TextCtrl() btn = ButtonCtrl(label='Quit', bitmap=psp2d.Image('quit.png')) def updateDescription(obj): description.text = obj.getDescription() def launchDemo(obj): obj.main() def quit(): raise KeyboardInterrupt('quit') choice.connect('SelectionChanged', updateDescription) choice.connect('Activated', launchDemo) btn.connect('Activated', quit) class ModuleProxy(object): def __init__(self, mdl): self.mdl = mdl def __str__(self): return self.mdl.getName() def getDescription(self): return self.mdl.getDescription() def main(self): self.mdl.main() names = os.listdir('.') names.sort() for name in names: if name.endswith('.py') and name != 'script.py': mdl = loadModule(os.path.abspath(name)) if hasattr(mdl, 'getName'): choice.Append(ModuleProxy(mdl)) vsz = BoxSizer(direction=VERTICAL, border=5) vsz.Add(choice) vsz.Add(btn, 1, vsz.EXPAND) hsz = BoxSizer(direction=HORIZONTAL, border=5) hsz.Add(vsz, 0, hsz.EXPAND) hsz.Add(description, 1, hsz.EXPAND) screen = psp2d.Screen() oldpad = psp2d.Controller() while True: screen.blit(bgimg) hsz.Paint(screen, 0, 0, 480, 272, True) screen.swap() pad = psp2d.Controller() if pad != oldpad: hsz.OnKey(pad) oldpad = pad
def Paint(self, drw, x, y, w, h, focused): img = psp2d.Image(w, h) img.clear(psp2d.Color(0, 0, 0, 0)) totalStretch = reduce(operator.add, [k[1] for k in self.widgets], 0) minW, minH = self.GetMinSize() remainW = w - minW remainH = h - minH curX = self.border curY = self.border for idx, (widget, stretch, flags) in enumerate(self.widgets): minW, minH = widget.GetMinSize() if self.direction == HORIZONTAL: if flags & self.EXPAND: ch = h - self.border * 2 else: ch = minH if stretch: cw = minW + remainW * stretch // totalStretch else: cw = minW if flags & self.ALIGN_CENTER: cx = (cw - minW) // 2 cy = (h - minH) // 2 else: if flags & self.ALIGN_RIGHT: cx = cw - minW else: # Default left cx = 0 if flags & self.ALIGN_BOTTOM: cy = h - minH else: # Default top cy = 0 widget.Paint(img, curX + cx, curY + cy, cw, ch, focused and self.focus == idx) curX += cw + self.border else: if flags & self.EXPAND: cw = w - self.border * 2 else: cw = minW if stretch: ch = minH + remainH * stretch // totalStretch else: ch = minH if flags & self.ALIGN_CENTER: cx = (w - minW) // 2 cy = (ch - minH) // 2 else: if flags & self.ALIGN_RIGHT: cx = cw - minW else: # Default left cx = 0 if flags & self.ALIGN_BOTTOM: cy = h - minH else: # Default top cy = 0 widget.Paint(img, curX + cx, curY + cy, cw, ch, focused and self.focus == idx) curY += ch + self.border drw.blit(img, dx=x, dy=y, blend=True)
def draw(pgName): image = psp2d.Image(480, 272) image.clear(BLANK) screen.blit(image); screen.swap() image = psp2d.Image(pgName) screen.blit(image, dx=x, dy=y); screen.swap()
import psp2d, pspos pspos.setclocks(333,166) # To set both CPU and Bus clock screen = psp2d.Screen() BLANK = psp2d.Color(0,0,0) state=x=y=0 image = psp2d.Image(480, 272) image.clear(BLANK) screen.blit(image); screen.swap() snake="pg.png" def draw(pgName): image = psp2d.Image(480, 272) image.clear(BLANK) screen.blit(image); screen.swap() image = psp2d.Image(pgName) screen.blit(image, dx=x, dy=y); screen.swap() while state==0: pad = psp2d.Controller() if pad.right: x = x + 5 draw(snake) elif pad.left: x = x - 5 draw(snake) elif pad.up: y = y - 5 draw(snake) elif pad.down: y = y + 5 draw(snake) elif pad.circle: state=1
def __init__(self): self.image = psp2d.Image('buttons/res/up.png')
def _draw_button_on_screen(self, button): self.image = psp2d.Image(button.image) self.screen.blit(self.image) self.screen.swap()
def _view_background_colored_to(self, color): self.image = psp2d.Image(480, 272) self.image.clear(color) self.screen.blit(self.image) self.screen.swap()
def _clear_screen(self): self.image = psp2d.Image(480, 272) self.image.clear(self.BLACK) self.screen.blit(self.image)