import slither import pygame snakey = slither.Sprite() snakey.costumeName = "costume0" snakey.goto(0, 0) slither.setup() # Begin slither def handlequit(): print("Quitting...") return True slither.registerCallback(pygame.QUIT, handlequit) # This uses the direct call form @slither.registerCallback(pygame.MOUSEBUTTONUP) # This uses the decorator form def handlemouseup(event): print("Mouseup:", event.pos, event.button) def run_a_frame(): snakey.xpos += 1 snakey.ypos += 1 snakey.direction += 1 slither.runMainLoop(run_a_frame)
import slither, random # Get functions from slither and random doge = [None] * 30 # Initialize array 'doge' dogecount = 1 # Initialize iterator 'dogecount' while dogecount != 30: # Produce 30 doges doge[dogecount] = slither.Sprite() # Make each doge a sprite doge[dogecount].addCostume('doge.png', 'doge') # Give them the costume 'doge' doge[dogecount].costumeName = 'doge' # Set the doges costume to 'doge' doge[dogecount].ypos = random.randint( 0, 400) # Make the doges go to a random y position doge[dogecount].xpos = random.randint( 0, 400) # Make the doges go to a random x position dogecount += 1 # Increase the iterator for reiteration slither.setup( 'Doge invasion!', 400, 400 ) # Setup slither, set caption to 'Doge invasion!' and set resolution to 400px by 400px def loop(): # Define main game loop called 'loop' pass # Do nothing... slither.runMainLoop(loop) # Run main game loop
#! usr/bin/env python3 # isTouchingTest.py import slither toucher = slither.Sprite() toucher.addCostume("assets/arrow.png", "arrow") toucher.costumeName = "arrow" toucher.goto(0, 100) snakey = slither.Sprite() snakey.goto(slither.WIDTH // 2, 100) def run_a_frame(): toucher.moveSteps(3) if toucher.isTouching(snakey): print("Touching!") else: print("Not touching.") slither.setup() slither.runMainLoop(run_a_frame)
# usr/bin/env python3 # svgTest.py - Test Slither's SVG support import slither if slither.svgSupport: print("We have SVG support!") else: print("No SVGs today :(") svg = slither.Sprite() svg.addCostume("assets/SVG Logo.svg", "svg") svg.costumeNumber = 1 svg.scale = 1 svg.showBoundingBox = False svg.goto(100, 300) svg2 = slither.Sprite() svg2.addCostume("assets/SVG Logo.svg", "svg") svg2.costumeNumber = 1 svg2.scale = 5 svg2.showBoundingBox = False svg2.goto(500, 300) slither.setup() # Begin slither def run_a_frame(): pass
#! usr/bin/env python3 # zindextest.py import slither # Normally, since Snakey was made before SoExcited, SoExcited would be rendered after Snakey and be put on top. snakey = slither.Sprite() SoExcited = slither.Sprite() SoExcited.addCostume("assets/SoExcited.png", "avatar") SoExcited.costumeNumber = 1 SoExcited.zindex = -1 # But when SoExcited's z-index is set to below that of Snakey's, SoExcited gets rendered before (and thus below) Snakey. SoExcited.goto(300, 300) SoExcited.scale = 0.33 slither.slitherStage.bgColor = (40, 40, 222) slither.setup() def run_a_frame(): snakey.xpos += 1 snakey.ypos += 1 SoExcited.direction += 1 slither.runMainLoop(run_a_frame)
import slither # Get slither functions background = slither.slitherStage # Make 'background' the stage background.addCostume('./assets/grass.png', 'grass') # Add costume 'grass' to background background.costumeName = 'grass' # Set the costume to grass snakey = slither.Sprite() # Make a sprite named snakey (default) snakey.goto(700, 200) # Make snakey go to xpos 700 and ypos 200 slither.setup( 'Run Snakey!', 800, 400 ) # Setup slither, set the caption to 'Run Snakey!' and set the resolution to 800px by 400px def myloop(): # Make a main loop named 'myloop' slither.blitText( 'Run snakey, Run!', 300, 20, 32 ) # Add the text 'Run snakey, Run!' to the screen on xpos 300, ypos 20 with font size 32 snakey.xpos -= 10 # Make snakey run forward if snakey.xpos == 100: # Trigger code if snakey is about to exit screen snakey.goto(700, 200) # Make snakey restart slither.runMainLoop(myloop) # Run the main game loop
def create_circle(): circle = slither.Sprite() circle.addCostume("assets/circle.png", "circle") circle.costumeName = "circle" circle.goto(0, 0) return circle
import slither # Get slither functions snakey = slither.Sprite() # Make a sprite called snakey snakey.costumeName = "costume0" # Set snakey's costume to costume0 snakey.goto(200, 200) # Make snakey go to x 200 and y 200 slither.setup( 'Hello World!', 400, 400 ) # Setup slither with caption 'Hello World!' and resolution 400px by 400px def run_a_frame(): # Define main game loop pass # Do nothing. This is only a simple project slither.runMainLoop(run_a_frame) # Start main game loop
# mover.py import pygame # We need pygame for the event constants and the key functions import slither from pygame.locals import * sprite = slither.Sprite() sprite.goto(slither.WIDTH // 2, slither.HEIGHT // 2) # Goto the middle of the screen sprite.addCostume("../tests/assets/arrow.png", "arrow") # Load the arrow costume sprite.costumeName = "arrow" # Set the costume. # This dict holds the data for how fast the arrow moves and what direction it is pointing controls = {"turn":0, "speed":0} @slither.registerCallback(pygame.MOUSEBUTTONDOWN) def handle_mousedown(event): # When the mouse button is pressed... print(controls) # ...print out the controls dict def run_a_frame(): keys = pygame.key.get_pressed() # Because this example relies on having keys pressed down, # we can't use the event handlers, and have to check the key states ourself if keys[K_LEFT]: controls["turn"] -= 4 elif keys[K_RIGHT]: controls["turn"] += 4 if keys[K_SPACE]: controls["speed"] = min(controls["speed"] + 2, 10) else: controls["speed"] = max(controls["speed"] - 0.2, 0) # Set the sprites direction... sprite.direction = controls["turn"]