# usr/bin/env python3 # keysDownTest.py - Test slither.keysDown() import slither def run_a_frame(): print(slither.keysDown()) slither.setup() slither.runMainLoop(run_a_frame)
#! usr/bin/env python3 # fullScreenTest.py import slither, pygame snakey = slither.Sprite() snakey.costumeName = "costume0" # You can set the default costume (costume0) by name... snakey.goto(400, 300) slither.slitherStage.bgColor = (40, 222, 40) slither.setup() # Begin slither def run_a_frame(): snakey.direction += 1 @slither.registerCallback(pygame.MOUSEBUTTONUP) def handle_mousedown(event): slither.toggleFullScreen() 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
import slither i = 0 def run_a_frame(): global i slither.setCaption("-"*(i+1)) i = (i + 1) % 60 slither.setup("") slither.runMainLoop(run_a_frame)
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, pygame, time snakey = slither.Sprite() snakey.setCostumeByName("costume0") snakey.goTo(0, 0) slither.slitherStage.setColor(40, 222, 40) screen = slither.setup() # Begin slither continueLoop = True while continueLoop: slither.blit(screen) # Display snakey.changeXBy(1) snakey.changeYBy(1) snakey.changeDirectionBy(1) # Handle quitting for event in pygame.event.get(): if event.type == pygame.QUIT: continueLoop = False time.sleep(0.01)
import slither, time # Get slither and time functions slither.setup( 'Press S for sound', 400, 400 ) # Setup slither with the caption 'Press S for sound' and resolution 400px by 400px def loop(): # Define main loop try: # Error handling wrapper if slither.keysDown()[0] == 's': # Run if key s is pressed... sound = slither.slitherSound.loadSound( 'boom.wav') # Load the sound 'boom.wav' sound.play() # Play the sound time.sleep(2) # Wait 2 seconds except: # Run code if there's an error pass # Do nothing slither.runMainLoop(loop) # Run main loop
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
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
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
import slither i = 0 def run_a_frame(): global i slither.setCaption("-" * (i + 1)) i = (i + 1) % 60 slither.setup("") slither.runMainLoop(run_a_frame)
import slither, time # Get slither and time functions slither.setup('Press S for sound', 400, 400) # Setup slither with the caption 'Press S for sound' and resolution 400px by 400px def loop(): # Define main loop try: # Error handling wrapper if slither.keysDown()[0] == 's': # Run if key s is pressed... sound = slither.slitherSound.loadSound('./assets/boom.wav') # Load the sound 'boom.wav' sound.play() # Play the sound time.sleep(2) # Wait 2 seconds except: # Run code if there's an error pass # Do nothing slither.runMainLoop(loop) # Run main loop
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('./assets/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
# 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"] # ... and move it speed steps sprite.moveSteps(controls["speed"]) # Add wraparound checks. if sprite.xpos > slither.WIDTH: sprite.xpos = 0 elif sprite.xpos < 0: sprite.xpos = slither.WIDTH if sprite.ypos > slither.HEIGHT: sprite.ypos = 0 elif sprite.ypos < 0: sprite.ypos = slither.HEIGHT # Print instructions print("Use the left and right arrows to turn.\nUse the space key to go forward") # Setup slither... slither.setup("Move!") # and start it running! slither.runMainLoop(run_a_frame)