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
8 changes: 4 additions & 4 deletions examples/basicTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
SoExcited.addCostume("SoExcited.png", "avatar")
SoExcited.setCostumeByNumber(0) # ...or you can use a number.

SoExcited.goTo(300, 300)
SoExcited.setScaleTo(0.33) # May look grainy when used on too low a scale.
SoExcited.goTt(300, 300)
SoExcited.scale = 0.33 # May look grainy when used on too low a scale.

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

slither.setup() # Begin slither

def run_a_frame():
snakey.changeXBy(1)
SoExcited.changeDirectionBy(1)
snakey.xpos += 1
SoExcited.direction += 1

slither.runMainLoop(run_a_frame)
6 changes: 4 additions & 2 deletions examples/bgTest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import slither, time, math

snakey = slither.Sprite()
snakey.setCostumeByName("costume0")
snakey.setYTo(360)
snakey.ypos = 360

slither.slitherStage.setColor(40, 222, 40)
slither.slitherStage.addCostume("bg2.png", "bg")
slither.slitherStage.setCostumeByName("bg")
Expand All @@ -11,7 +13,7 @@
i = 0
def run_a_frame():
global i
snakey.setXTo((math.sin(i) * 200) + 300)
snakey.xpos = ((math.sin(i) * 200) + 300)
i += 0.05

slither.runMainLoop(run_a_frame)
10 changes: 6 additions & 4 deletions examples/imageBounceTest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import slither

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

slither.slitherStage.setColor(255, 255, 255)

def run_a_frame():
circle.changeXBy(1)
circle.changeYBy(1)
circle.changeDirectionBy(1)
circle.xpos += 1
circle.ypos += 1
circle.direction += 1

slither.setup()
slither.runMainLoop(run_a_frame)
10 changes: 5 additions & 5 deletions examples/turnAndMove.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
snakey = slither.Sprite()
snakey.setCostumeByName("costume0")

snakey.goTo(0, 0)
snakey.goto(0, 0)

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

slither.setup() # Begin slither

def handlequit():
print("Quiting...")
print("Quitting...")
return True
slither.registerCallback(pygame.QUIT, handlequit) # This uses the direct call form

Expand All @@ -19,8 +19,8 @@ def handlemouseup(event):
print("Mouseup:", event.pos, event.button)

def run_a_frame():
snakey.changeXBy(1)
snakey.changeYBy(1)
snakey.changeDirectionBy(1)
snakey.xpos += 1
snakey.ypos += 1
snakey.direction += 1

slither.runMainLoop(run_a_frame)
12 changes: 6 additions & 6 deletions examples/zindextest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
SoExcited.addCostume("SoExcited.png", "avatar")
SoExcited.setCostumeByNumber(0)

SoExcited.changeZIndexBy(-1) # But when SoExcited's z-index is set to below that of Snakey's, SoExcited gets rendered before (and thus below) Snakey.
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.setScaleTo(0.33)
SoExcited.goto(300, 300)
SoExcited.scale = 0.33

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

slither.setup()

def run_a_frame():
snakey.changeXBy(1)
snakey.changeYBy(1)
SoExcited.changeDirectionBy(1)
snakey.xpos += 1
snakey.ypos += 1
SoExcited.direction += 1

slither.runMainLoop(run_a_frame)
85 changes: 16 additions & 69 deletions slither/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ def __init__(self):
self.currentCostume = None
self.bgColor = (255, 255, 255)

# Stage-specific functions
def setColor(self, r, g, b):
'''Set the stage's color. Only works on the stage, not sprites.'''
self.bgColor = (r, g, b)

# Functions shared by sprites
def addCostume(self, costumePath, costumeName):
'''Add a costume based on a given path and name.'''
Expand Down Expand Up @@ -80,15 +75,6 @@ def setCostumeByNumber(self, number):
costumeName = list(self.costumes.keys())[number]
self.setCostumeByName(costumeName)

def getCostumeNumber(self):
'''Get the costume number of the sprite.'''
return self.costumeNumber

def getCostumeName(self):
'''Get the costume name of the sprite.'''
return self.costumeName


slitherStage = Stage()

# The Sprite inherits things such as the costumes from the stage so everything can be kept in one place.
Expand All @@ -101,9 +87,21 @@ def __init__(self):
self.direction = 0 # Direction is how much to change the direction, hence why it starts at 0 and not 90
self.showing = True
self.scale = 1 # How much to multiply it by in the scale
self.zIndex = 0 # How high up are we in the "z" axis?
self.zindex = 0 # How high up are we in the "z" axis?
sprites.append(self) # Add this sprite to the global list of sprites


@property
def zindex(self):
"The location of the sprite in the z-axis"
return self._zindex

@zindex.setter
def zindex(self, val):
#if val < 0 or int(val) != val:
# raise ValueError("zindex must be a non-negative integer")
self._zindex = val
reorderSprites()

def show(self):
'''Show the sprite.'''
self.showing = True
Expand All @@ -112,62 +110,11 @@ def hide(self):
'''Hide the sprite.'''
self.showing = False

def changeXBy(self, amount):
'''Change the sprite's X Position by (amount).'''
self.xpos += amount

def changeYBy(self, amount):
'''Change the sprite's Y Position by (amount).'''
self.ypos += amount

def goTo(self, xpos, ypos):
def goto(self, xpos, ypos):
'''Go to xpos, ypos.'''
self.xpos = xpos
self.ypos = ypos

def setXTo(self, xpos):
'''Set the sprite's X Position to (amount).'''
self.xpos = xpos

def setYTo(self, ypos):
'''Set the sprite's Y Position to (amount).'''
self.ypos = ypos

def changeScaleBy(self, amount):
'''Change the sprite's scale by (amount).'''
self.scale += amount

def setScaleTo(self, scale):
'''Set the sprite's scale to (amount).'''
self.scale = scale

def getScale(self):
'''Get the sprite's current scale.'''
return self.scale

def setDirection(self, direction):
self.direction = direction

def changeDirectionBy(self, amount):
self.direction += amount

def getDirection(self):
return self.direction

def changeZIndexBy(self, amount):
'''Change the sprite's z-index by (amount).'''
self.zIndex += amount
reorderSprites()

def setZIndexTo(self, zIndex):
'''Set the sprite's z-index to (amount).'''
self.zIndex = zIndex
reorderSprites()

def getZIndex(self):
'''Get the sprite's current z-index.'''
return self.zIndex

def isVisible(self):
'''Check if the object is visible, not just showing.'''
return self.showing and self.scale > 0
Expand Down Expand Up @@ -211,7 +158,7 @@ def setFPS(fps):

def reorderSprites():
global sprites
sprites = sorted(sprites, key=(lambda s: s.zIndex))
sprites = sorted(sprites, key=(lambda s: s.zindex))

def blit(screen):
'''Takes a screen as an argument and draws objects to the screen. THIS MUST BE CALLED FOR SLITHER TO DISPAY OBJECTS.'''
Expand Down