예제 #1
0

def onmousedown(sk, ev):
    "Event handler for mouse clicks"
    chimp = sk["Chimp"]
    if chimp.spin == 0 and chimp.contains(sk.mouse.pos):
        chimp.oldVel = chimp.vel
        chimp.config(spin=-8, vel=(0, 0))
        sk["Score"] += 1
        sound = 0
    else:
        sound = 1
    sk.sounds[sound].play()


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 ondraw(fist):
    "Make fist follow the mouse"
    fist.pos = fist.sketch.mouse.pos


# Run the sketch
Sketch((468, 60)).bind(setup).play("Monkey Fever")
예제 #2
0
# along with "sc8pr".  If not, see <http://www.gnu.org/licenses/>.

"""
This program demonstrates how a Graphic can be used in place
of a cursor. The program relies on the Graphic.hoverable
property which has been added in the development version of
sc8pr (v2.2.dev) but has not been released as of v2.1. If used
in an earlier sc8pr version, the "cursor" will block mouse events
to other graphic objects.
"""

from sc8pr import Sketch
from sc8pr.sprite import Sprite

def updateFakeCursor(gr):
    "Run default ondraw and configure to follow the mouse"
    h = type(gr).ondraw
    if h: h(gr)
    gr.config(pos=gr.sketch.mouse.pos, layer=-1)

def fakeCursor(sk, gr):
    "Use a graphic to mimic the cursor"
    sk.cursor = False
    sk += gr.bind(ondraw=updateFakeCursor).config(hoverable=False, wrap=0)

def setup(sk):
    gr = Sprite("img/ship.png").config(width=32, spin=1)
    fakeCursor(sk, gr)

Sketch().play("Fake Cursor")
예제 #3
0
        pass  # Nothing to remove in first round!

    # Add images for current round
    p1, p2 = btn.layer, randint(0, 2)
    w, h = sk.size
    x, y, h = w / 4, h / 2, 0.6 * h
    sk["Player1"] = Image(sk.imgs[p1]).config(height=h, pos=(x, y))
    sk["Player2"] = Image(sk.imgs[p2]).config(height=h, pos=(3 * x, y))

    # Determine the winner and update scores/status
    if p1 == p2: win = "It's a draw!"
    elif p1 - p2 in (1, -2):
        win = "You win!"
        sk["Score1"] += 1
    else:
        win = "You lose!"
        sk["Score2"] += 1
    sk["Status"].config(data=win, layer=-1)


def onkeydown(sk, ev):
    "Redirect keydown event to button onclick handler"
    try:
        onclick(sk["rps".index(ev.unicode.lower())], ev)
    except:
        pass


# Play the game!
Sketch().bind(setup, onkeydown).play("Rock, Paper, Scissors")
예제 #4
0
def menuAction(menu, ev):
    "Event handler for Menu"
    n = menu.buttonNumber(ev.target)
    print(menu, n)
    if n == 1:  # Item 1 = Back
        for btn in menu:
            btn.status = "normal"
        cv = menu.canvas
        menu.remove()
        cv["Cover"].remove()


def optionsChange(gr, ev):
    "Event handler for Options"
    target = ev.target
    print(gr, target.layer, target.selected)


def sliderChange(gr, ev):
    "Event handler for Slider"
    m = ["Click", "Scroll", "Drag", "Key"][ev.method]
    print(gr, round(gr.val), m)


def onclick(sk, ev):
    print("Click:", ev.target)


# Play the sketch
Sketch().bind(setup, onclick).play("GUI Demo")
예제 #5
0
def play():
    Sketch((340, 340)).bind(setup, ondraw).play(TITLE)
예제 #6
0
                                                 vars={
                                                     "k": 4,
                                                     "x0": 0,
                                                     "A": 1.5
                                                 })

    # Add tick marks and labels to the x-axis
    p["_xtick"] = p.xtick(pi_4, ends)
    attr = dict(marker="{:.2f}", fontSize=16, omitZero=True)
    p["_xlabel"] = p.xtick(pi_4, ends, **attr).transform(shift=(0, -0.15))

    # Add tick marks and labels to the y-axis
    ends = [-1.5, 1.6]
    p["_ytick"] = p.ytick(0.5, ends)
    p["_ylabel"] = p.ytick(0.5, ends, **attr).transform(shift=(-0.3, 0))


def ondraw(p):
    "Modify wave variables"

    # Shift wave by 0.01 and reduce amplitude
    v = p["Wave"].vars
    v["x0"] += 0.01
    v["A"] *= 0.999

    # Mark plot as stale for re-rendering
    p.stale = True


Sketch().play("Plot Animation")