Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
![Slither's logo](https://cloud.githubusercontent.com/assets/13385064/14028163/82c374ea-f1d1-11e5-84b7-8ebc442b2d70.png)

[![Codacy Badge](https://api.codacy.com/project/badge/grade/6c21312189334c1782a49e152d0f4b78)](https://www.codacy.com/app/stanleybookowl/Slither)

## What is Slither?
Slither is a Python module that uses PyGame to bring Scratch-like features to Python.
[Scratch](scratch.mit.edu) is a programming language developed by MIT that uses block-based scripts to introduce kids to programming.
Expand Down Expand Up @@ -43,5 +45,20 @@ Finally, check to see if you have the newest version of Slither.

If you've done all three of those things and it still doesn't work, file an issue.

## Credits
Scratch is property of The Lifelong Kindergarten Group. I am not affiliated with the LLK, nor do I own Scratch.
# Contributors
A list of all contributors who have helped or are helping.

### @Tymewalk
Started Slither, added several groundwork features.

### @BookOwl
Helped majorly rewrite Slither so it's easier to code in. Additionally helped suggest changes and fix bugs, which made Slither much better.

### @Omegabyte
Began and currently works on documentation, also added sound support.

### @IronManMark20
Is working on adding SVG support to slither.

# Credits
Scratch is property of The Lifelong Kindergarten Group. The slither devs are not affiliated with the LLK, nor do they own Scratch.
Binary file modified examples/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 22 additions & 9 deletions examples/imageBounceTest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import slither

circle = slither.Sprite()
circle.addCostume("circle.png", "circle")
circle.setCostumeByName("circle")
circle.goto(0,0)

slither.slitherStage.setColor(255, 255, 255)
def create_circle():
circle = slither.Sprite()
circle.addCostume("circle.png", "circle")
circle.setCostumeByName("circle")
circle.goto(0,0)
return circle
circle1, circle2, circle3 = create_circle(), create_circle(), create_circle()
circle4 = create_circle()
circle4.goto(400,300)
circle4.debug = True
slither.slitherStage.bgcolor = (255, 255, 255)

def run_a_frame():
circle.xpos += 1
circle.ypos += 1
circle.direction += 1
circle1.xpos += 0.5
circle1.ypos += 0.5
circle1.direction += 1

circle2.xpos += 0.5
circle2.direction += 1

circle3.ypos += 0.5
circle3.direction += 1

circle4.direction += 1

slither.setup()
slither.runMainLoop(run_a_frame)
2 changes: 0 additions & 2 deletions examples/turnAndMove.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

snakey.goto(0, 0)

slither.slitherStage.setColor(40, 222, 40)

slither.setup() # Begin slither

def handlequit():
Expand Down
58 changes: 38 additions & 20 deletions slither/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# SLITHER FOR PYTHON 2 AND 3
# Hi there, code divers!
# There's a lot of cool stuff here that has comments so you can understand what I'm doing.
# There's a lot of cool stuff here that has comments so you can understand what's going on.
# You can even mess around with it yourself :)
# If you think your messing around might help, go to:
# https://github.com/Tymewalk/Slither
# https://github.com/PySlither/Slither
# and make a pull request!

# Contributors: If your code doesn't seem straightforward, make comments so other people know what's going on
# Also make sure to credit sources like StackOverflow.

import pygame
import sys, os
# import time # For future timer

WIDTH, HEIGHT = (800, 600)
SCREEN_SIZE = (WIDTH, HEIGHT)

sprites = [] # List of all sprites
clock = pygame.time.Clock() # Used to control framerate
Expand All @@ -25,12 +30,9 @@

# Convienience functions
# Taken from http://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame
# This function is broken, issue #9 remains
def rotateCenter(image, angle):
"""rotate a Surface, maintaining position."""
loc = image.get_rect().center #rot_image is not defined
rot_sprite = pygame.transform.rotate(image, angle)
rot_sprite.get_rect().center = loc
return rot_sprite

# Stage class
Expand All @@ -54,26 +56,40 @@ def addCostume(self, costumePath, costumeName):
def deleteCostumeByName(self, name):
'''Delete a costume by name.'''
self.costumes.pop(name, None)
setCostumeByName(self.costumeName) # Make sure we recalculate the costume number!
self.recalculateNumberFromName(self.costumeName) # Make sure we recalculate the costume data!

def deleteCostumeByNumber(self, number):
'''Delete a costume by number.'''
if number < len(self.costumes.keys()):
costumeName = self.costumes.keys()[number]
self.deleteCostumeByName(costumeName)
self.deleteCostumeByName(costumeName) # TODO: Fix this stupid "get name from number" thing

@property
def costumeNumber(self):
'''The number of the costume the sprite is showing'''
return self._costumeNumber

def setCostumeByName(self, name):
'''Set a costume by its name.'''
if name in self.costumes:
self.currentCostume = self.costumes[name]
@costumeNumber.setter
def costumeNumber(self, val):
if number < len(self.costumes.keys()): # TODO: use mod to wrap around
self.costumeName = list(self.costumes.keys())[val]
self.costumeNumber = val

@property
def costumeName(self):
'''The name of the costume the sprite is showing'''
return self._costumeName

@costumeName.setter
def costumeName(self, val):
if val in self.costumes:
self.recalculateCostumeDataFromName(val)

def recalculateCostumeDataFromName(self, name):
self.costumeName = name
self.costumeNumber = list(self.costumes.keys()).index(name)
self.currentCostume = self.costumes[self.costumeNumber]

def setCostumeByNumber(self, number):
'''Set a costume by its number.'''
if number < len(self.costumes.keys()):
costumeName = list(self.costumes.keys())[number]
self.setCostumeByName(costumeName)

slitherStage = Stage()

Expand Down Expand Up @@ -145,7 +161,7 @@ def setup(caption=sys.argv[0]):
'''Sets up PyGame and returns a screen object that can be used with blit().'''
global globalscreen
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen = pygame.display.set_mode(SCREEN_SIZE)
caption = pygame.display.set_caption(caption)
globalscreen = screen
return screen
Expand All @@ -166,7 +182,7 @@ def blit(screen):
screen.fill(slitherStage.bgColor)

if slitherStage.currentCostume:
screen.blit(pygame.transform.scale(slitherStage.currentCostume, (800,600)), (0, 0))
screen.blit(pygame.transform.scale(slitherStage.currentCostume, SCREEN_SIZE, (0, 0)))

for obj in sprites:
if obj.isVisible(): # Check if the object is showing before we do anything
Expand All @@ -177,7 +193,9 @@ def blit(screen):
image = pygame.transform.scale(image, (int(imageSize[0] * obj.scale), int(imageSize[1] * obj.scale)))
if not obj.direction == 0:
image = rotateCenter(image, obj.direction)
screen.blit(image, (obj.xpos, obj.ypos))
new_rect = image.get_rect()
new_rect.center = (obj.xpos, obj.ypos)
screen.blit(image, new_rect)

pygame.display.flip()

Expand Down