def ondraw(self): sk = self.sketch Sprite.ondraw(self) # Check whether a goal has been scored player = self.goal(sk) if player is not None: sk.goal(player) # Place ball for "faceoff" pos = sk.center while self.collideRobot(pos): pos = pos[0], pos[1] + 1 self.config(pos = pos, spin = 0, vel = (0,0))
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 drawLot(self): px = self.px self += Line(px(-2.7, 0), px(2.7, 0)).config(weight=10, stroke="blue") attr = dict(stroke="orange", weight=4) for x in range(-2, 3): self += Line(px(x, 2), px(x, 1)).config(**attr) self += Line(px(x, -2), px(x, -1)).config(**attr) self.flatten() img = Circle(0.4 * self.unit).config(fill="grey", weight=0).snapshot() for x in range(-3, 3): self += Sprite(img).config(pos=px(x + 0.5, 2.36), mass=50, drag=0.02, wrap=0) self += Sprite(img).config(pos=px(x + 0.5, -2.37), mass=50, drag=0.02, wrap=0) return self
def recordButton(self, y): "Compose the record button" sz = 21, 21 btn = Button(sz, ["#ffffff00", "#ffc0c0"]).config(anchor=LEFT, pos=(12, y), weight=0).bind(onclick) img = Circle(76).config(fill="red", weight=6).snapshot(), Image( (19, 19)) btn += Sprite(img).config(pos=btn.center, height=19) return btn
def __init__(self, sk): # Radio buttons text = self.options attr = {"font":sk.font, "fontSize":16} radio = [Radio(text, txtConfig=attr).config(anchor=TOPLEFT), Radio(text[1:], txtConfig=attr).config(anchor=TOPLEFT)] # Play button icon = Sprite(SoccerBall.ballImage).config(spin=1) play = Text("Play").config(**attr) play = Button((96,36), 2).textIcon(play, icon) x, y = icon.pos x += icon.width / 2 icon.config(anchor=CENTER, pos=(x,y)) # Titles attr.update(anchor=TOP) text = [Text(t + " Robot").config(color=t, **attr) for t in ("Red", "Yellow")] # Create canvas items = radio + text + [play] w = max(gr.width for gr in items) + 16 h = sum(gr.height for gr in items) + 72 super().__init__((w, h), "#d0d0d0") # Position controls xc = self.center[0] y = 8 for gr in [text[0], radio[0], text[1], radio[1]]: x = xc if isinstance(gr, Text) else 8 self += gr.config(pos=(x,y)) y += gr.height + 8 if gr is radio[0]: y += 16 self += play.config(pos=(xc,h-8), anchor=BOTTOM)
def setup(self): self.config(border="blue", weight=1, bg=self.field()) # Display the score attr = {"font":self.font, "fontSize":48} self += Text(0).config(pos=(8,8), anchor=TOPLEFT, color="red", **attr) self += Text(0).config(pos=(631,8), anchor=TOPRIGHT, color="yellow", **attr) self.score = list(self)[-2:] # Paint the back of the nets so the robots can see them w, h = self.size d = h / 8 r = 0.65 * boxSize(w, h)[1] h = (h - 1) / 2 self += Sprite(Image((2,2), "red")).config(pos=(-d, h), wrap=0) self += Sprite(Image((2,2), "yellow")).config(pos=(w+d, h), wrap=0) for s in self[-2:]: s.radiusFactor *= r / s.radius # Get a soccer ball self += SoccerBall().config(pos=self.center) # Start the simulation if hasattr(self, "brains"): self.start() else: self += Dialog(self).config(pos=self.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 drawHighScores(self, score): "Draw the Top 10 scores on the screen" w, h = self.size x = w / 6 y = h / 5 attr = dict(color="blue", font=FONT, fontSize=self.height / 15) for i in range(len(score)): if i == 5: x += 0.5 * w y = h / 5 s = score[i] s, name = s if len(name) > 12: name = name[:12] self += Text(data=s).config(anchor=RIGHT, pos=(x - 20, y), **attr) self += Text(data=name).config(anchor=LEFT, pos=(x, y), **attr) y += self[-1].height + 8 icon = Sprite(Asteroid.original).config(spin=0.4) okay = Text("Okay").config(font=FONT) self += Button((w / 7, h / 10)).bind(onclick=restart).textIcon( okay, icon, 10).config(pos=(self.center[0], 0.9 * h), anchor=BOTTOM, border="blue", weight=3)
def chimpDraw(chimp): "Stop spinning after a random amount of time" Sprite.ondraw(chimp) # Call default handler!! if chimp.spin and randint(1, 30) == 1: chimp.spin = chimp.angle = 0 chimp.vel = chimp.oldVel
def setup(sk): gr = Sprite("img/ship.png").config(width=32, spin=1) fakeCursor(sk, gr)