def setup(sk): # Create a Canvas as a GUI dialog cv = Canvas((384, 256)).config(bg="#f0f0ff", weight=1) # Vertical positioning 16 pixels below last item added down = lambda cv: 16 + cv[-1].height text = dict(color=BLUE, font=FONT, fontStyle=BOLD, padding=4) if TextInputCanvas: # v2.2.dev ti = TextInputCanvas(336, "", "Type Some Text...", **text) else: # v2.0-2.1 ti = TextInput("", "Type Some Text...").config(**text) x, y = cv.center[0] - 8, 16 cv["Input"] = ti.bind(onaction, onchange=onaction).config(anchor=TOP, pos=(x, y), bg="white", weight=1) # Add a Radio box y += down(cv) cfg = {"font": FONT, "fontSize": 14} text = "Option A", "Option B", "Option C" box = Radio(text, txtConfig=cfg).bind(onchange=radioChange) cv["ABC"] = box.config(pos=(x, y), anchor=TOP, selected=1) # Add an Options box y += down(cv) text = "Option X", "Option Y", "Option Z" box = Options(text, txtConfig=cfg).config(pos=(x, y), anchor=TOP) cv["XYZ"] = box.bind(onaction=optionsChange) # Add Buttons y += down(cv) cv["Button Box"] = buttons(cfg).config(anchor=TOP, pos=(x, y)) # Modify canvas and sketch size based on content cv.resize((cv.width, y + down(cv)), False) w, h = cv.size sk.size = w + 48, h + 48 # Add a Slider slider = Slider((16, cv.height), [BLUE], 100, 0, 100) slider.config(pos=(cv.width, 0), anchor=TOPRIGHT, bg=GREY, weight=1) cv["Slider"] = slider.bind(onchange=sliderChange) # Create a popup menu img = Image.fromBytes(sc8prData("alien")) items = [("Action", img, None), ("Back", None, R_TRIANGLE)] cv.menu = Menu(items, txtConfig=cfg).config(pos=cv.center) cv.menu.bind(onaction=menuAction) # Add the dialog to the sketch sk["Dialog"] = cv.bind(resize=nothing).config(pos=sk.center)
def play(self, caption="sc8pr", icon=None, mode=True): "Initialize pygame and run the main drawing / event handling loop" # Initialize pygame.init() self._clock = pygame.time.Clock() pygame.key.set_repeat(400, 80) _pd.set_caption(caption) try: try: icon = pygame.image.load(icon) except: icon = Image.fromBytes(sc8prData("alien")).image _pd.set_icon(icon) except: logError() w, h = self._size self._fixedAspect = w / h mode = self._pygameMode(mode) self._mode = mode self.image = _pd.set_mode(self._size, mode) self.key = None self.mouse = pygame.event.Event(pygame.USEREVENT, code=None, pos=(0,0), description="Sketch startup") # Run setup try: if hasattr(self, "setup"): self.setup() else: main = sys.modules["__main__"] if hasattr(main, "setup"): main.setup(self) except: logError() # Drawing/event loop while not self.quit: try: self.frameCount += 1 br = self.dirtyRegions flip = br is None self.draw() if not flip: br += self.dirtyRegions flip = self._largeArea() self._clock.tick(self.frameRate) if flip: _pd.flip() else: _pd.update(br) if self.capture is not None: self.capture.capture(self) if self.ondraw: self.ondraw() self._evHandle() except: logError() pygame.quit() mod = sys.modules.get("sc8pr.text") if mod: mod.Font.dumpCache() return self
def __init__(self, colors=None): img = Image.fromBytes(sc8prData("robot")) if colors: # Replace body and nose colors px = pygame.PixelArray(img.image) body0, nose0, body, nose = rgba("red", "blue", *colors) orig = body0, nose0 if body in orig or nose in orig: colors = body0, nose0, body, nose body0 = _tempColor(px, body0, *colors) nose0 = _tempColor(px, nose0, *colors) if nose != nose0: px.replace(nose0, nose) if body != body0: px.replace(body0, body) img = img.tiles(2) super().__init__(img)
def setup(sk): # Create a Canvas as a GUI dialog cv = Canvas((384,256)).config(bg="#f0f0ff", weight=1) # Vertical positioning 12 pixels below last item added down = lambda cv: 16 + cv[-1].height # Add a TextInput x, y = cv.center[0], 16 cv["Input"] = TextInput("", "Type Some Text...").config(anchor=TOP, font=FONT, fontStyle=BOLD, pos=(x,y), color=BLUE, bg="white", padding=4).bind(onaction) # Add a Radio box y += down(cv) cfg = {"font":FONT, "fontSize":14} text = "Option A", "Option B", "Option C" radio = Radio(text, txtConfig=cfg).bind(onchange=radioChange) cv["ABC"] = radio.config(pos=(x,y), anchor=TOP, selected=1) # Add an Options box y += down(cv) text = "Option X", "Option Y", "Option Z" radio = Options(text, txtConfig=cfg).config(pos=(x,y), anchor=TOP) cv["XYZ"] = radio.bind(onaction=optionsChange) # Add Buttons y += down(cv) cv["Button Box"] = buttons(cfg).config(anchor=TOP, pos=(x,y)) # Modify canvas and sketch size based on content cv.resize((cv.width, y + down(cv)), False) w, h = cv.size sk.size = w + 48, h + 48 # Add a Slider slider = Slider((16, cv.height), BLUE, 100, 0, 100) slider.config(pos=(cv.width, 0), anchor=TOPRIGHT, bg=GREY, weight=1) cv += slider.bind(onchange=sliderChange) # Create a popup menu img = Image.fromBytes(sc8prData("alien")) items = [("Action", img, None), ("Back", None, R_TRIANGLE)] cv.menu = Menu(items, txtConfig=cfg).config(pos=cv.center) cv.menu.bind(resize=nothing, onaction=menuAction) # Add the dialog to the sketch cv.cover = sk.cover() sk["Dialog"] = cv.bind(resize=nothing).config(pos=sk.center)
def setup(game): "Create Tic-Tac-Toe board with 100 pixel squares and 20 pixel margins" # Load costumes for X and O sprites, and logo img = Image(resolvePath("img/xo.png", __file__)).tiles(3) game.alien = Image.fromBytes(sc8prData("alien")).config(height=36) # Create and position sprites, one per square for s in range(9): pos = 70 + 100 * (s % 3), 70 + 100 * (s // 3) game += Sprite(img).bind(contains=Graphic.contains, onclick=clickSquare).config(pos=pos, width=100) # Draw the board and start game for pts in [((20,120), (320,120)), ((20,220), (320,220)), ((120,20), (120,320)), ((220,20), (220,320))]: game += Line(*pts) startGame(game)
def _radioTiles(): "Images for creating radio check boxes" if Button._radio is None: Button._radio = Image.fromBytes(sc8prData("radio")) return Button._radio.tiles(5)
def _checkTiles(): "Images for creating check boxes" if Button._check is None: Button._check = Image.fromBytes(sc8prData("checkbox")) return Button._check.tiles(5)
def _yesNoImg(n): if Button._yesNo is None: Button._yesNo = Image.fromBytes(sc8prData("yesNo")).tiles(2) return Button._yesNo[0 if n else 1]
def play(self, caption="sc8pr", icon=None, mode=True): "Initialize pygame and run the main drawing / event handling loop" # Initialize pygame.init() self._clock = pygame.time.Clock() pygame.key.set_repeat(400, 80) _pd.set_caption(caption) try: try: icon = pygame.image.load(icon) except: icon = Image.fromBytes(sc8prData("alien")).image _pd.set_icon(icon) except: logError() w, h = self._size self._fixedAspect = w / h mode = self._pygameMode(mode) self._mode = mode self.image = _pd.set_mode(self._size, mode) self.key = None self.mouse = pygame.event.Event(pygame.USEREVENT, code=None, pos=(0, 0), description="Sketch startup") # Run setup try: if hasattr(self, "_defer_coords"): self.setCoords(*self._defer_coords) del self._defer_coords if hasattr(self, "setup"): self.setup() else: main = sys.modules["__main__"] if hasattr(main, "setup"): main.setup(self) except: logError() # Drawing/event loop while not self.quit: try: self.frameCount += 1 br = self.dirtyRegions flip = br is None self.ondrawList = [] self.draw() if not flip: br += self.dirtyRegions flip = self._largeArea() self._clock.tick(self.frameRate) if flip: _pd.flip() else: _pd.update(br) if self.capture is not None: self.capture.capture(self) for gr in self.ondrawList: try: gr.ondraw() except: logError() if self.ondraw: self.ondraw() self._evHandle() except: logError() pygame.quit() mod = sys.modules.get("sc8pr.text") if mod: mod.Font.dumpCache() return self