def setup(sk): "Add all content to the sketch" # Add text to the sketch font = {"font": Font.mono(), "fontStyle": BOLD} sk["Score"] = Text(0).config(anchor=TOPLEFT, color="red", **font) text = "Pummel the Chimp, and Win $$$" sk += Text(text).config(pos=(sk.width - 1, 0), anchor=TOPRIGHT, **font).config(width=0.75 * sk.width) # Add fist folder = resolvePath("examples/data", pygame.__file__) + "/" img = loadImage(folder + "fist.bmp") sk += Image(img).config(pos=sk.center, anchor=TOP).bind(ondraw) # Add chimp sprite img = loadImage(folder + "chimp.bmp") sk["Chimp"] = Sprite(img).config(pos=(48, 48), vel=(10, 0), bounce=BOTH).bind(ondraw=chimpDraw) # Load audio files audio = "punch.wav", "whiff.wav" sk.sounds = [pygame.mixer.Sound(folder + f) for f in audio] # Bind click event handler; hide cursor sk.bind(onmousedown) sk.cursor = False
def setup(sk): "Add all content to the sketch" # Add text to the sketch font = {"font":Font.mono(), "fontStyle":BOLD} sk["Score"] = Text(0).config(anchor=TOPLEFT, color="red", **font) text = "Pummel the Chimp, and Win $$$" sk += Text(text).config(pos=(sk.width-1,0), anchor=TOPRIGHT, **font).config(width=0.75*sk.width) # Add fist folder = resolvePath("examples/data", pygame.__file__) + "/" img = loadImage(folder + "fist.bmp") sk += Image(img).config(pos=sk.center, anchor=TOP).bind(ondraw) # Add chimp sprite img = loadImage(folder + "chimp.bmp") sk["Chimp"] = Sprite(img).config(pos=(48,48), vel=(10,0), bounce=BOTH).bind(ondraw=chimpDraw) # Load audio files audio = "punch.wav", "whiff.wav" sk.sounds = [pygame.mixer.Sound(folder + f) for f in audio] # Bind click event handler; hide cursor sk.bind(onclick) sk.cursor = False
def setup(sk): # Load image originals fldr = resolvePath("img", __file__) sk.imgs = [ Image("{}/{}.png".format(fldr, f)) for f in ("target", "paper", "scissors") ] # Create button controls and add images to buttons w, h = sk.size x = w / 4 - 44 for img in sk.imgs: btn = Button((40, 40), 2).config(anchor=BOTTOM, pos=(x, h - 4)) btn += Image(img).config(pos=btn.center, height=32) sk += btn.bind(onclick) # Bind event handlers x += 44 # Initialize scores to zero font = {"font": Font.mono(), "fontSize": 40, "fontStyle": BOLD} sk["Score1"] = Text(0).config(color="red", anchor=TOPLEFT, pos=(4, 4), **font) sk["Score2"] = Text(0).config(color="blue", anchor=TOPRIGHT, pos=(w - 4, 4), **font) # Create status text sk["Status"] = Text("Make a choice!").config(pos=(0.75 * w, h - 24), font=Font.sans(), fontSize=32, color="red")
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 setup(sk): # Load image originals fldr = resolvePath("img", __file__) sk.imgs = [Image("{}/{}.png".format(fldr, f)) for f in ("target", "paper", "scissors")] # Create button controls and add images to buttons w, h = sk.size x = w / 4 - 44 for img in sk.imgs: btn = Button((40, 40), 2).config(anchor=BOTTOM, pos=(x, h-4)) btn += Image(img).config(pos=btn.center, height=32) sk += btn.bind(onclick) # Bind event handlers x += 44 # Initialize scores to zero font = {"font":Font.mono(), "fontSize":40, "fontStyle":BOLD} sk["Score1"] = Text(0).config(color="red", anchor=TOPLEFT, pos=(4, 4), **font) sk["Score2"] = Text(0).config(color="blue", anchor=TOPRIGHT, pos=(w-4, 4), **font) # Create status text sk["Status"] = Text("Make a choice!").config(pos=(0.75*w, h-24), font=Font.sans(), fontSize=32, color="red")
def play(size=(720, 480), record="", auto=4096): sk = Game(size).config(imgFldr=resolvePath("img", __file__)) if record: sk.capture = Video().config(interval=2).autoSave(record, auto) sk.play("Asteroid Shield", sk.imgFldr + "/target.png") if record: sk.capture.autoSave()
def __init__(self): img = Image(resolvePath("img/ball.png", __file__)) SoccerBall.ballImage = img super().__init__(img) self.config(height=30, mass=1, drag=0.00025, bounce=BOTH)