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
14 changes: 10 additions & 4 deletions slither/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def costumeNumber(self):
def costumeNumber(self, val):
val = val % len(self.costumes)
self.costumeName = list(self.costumes.keys())[val]
self.currentCostume = self.costumes[self.costumeName]
self._costumeNumber = val

@property
Expand All @@ -83,10 +84,7 @@ def costumeName(self):
@costumeName.setter
def costumeName(self, val):
if val in self.costumes:
self.recalculateCostumeDataFromName(val)

def recalculateCostumeDataFromName(self, name):
self._costumeName = name
self._costumeName = val
self.currentCostume = self.costumes[self.costumeName]


Expand Down Expand Up @@ -136,6 +134,14 @@ def delete(self):
'''Remove the sprite from the global sprites list, causing it not to be drawn.'''
sprites.remove(self)

def isTouching(self, collideSprite):
'''Detects if one sprite is touching another.'''
ourRect = self.currentCostume.get_rect()
theirRect = collideSprite.currentCostume.get_rect()
ourRect.center = (self.xpos, self.ypos)
theirRect.center = (collideSprite.xpos, collideSprite.ypos)
return ourRect.colliderect(theirRect)

pygame.mixer.init(44100, -16, 2, 2048)

class Sound():
Expand Down
20 changes: 20 additions & 0 deletions slither/tests/isTouchingTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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)
2 changes: 1 addition & 1 deletion slither/tests/moveStepsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def run_a_frame():

slither.setup()
slither.setFPS(30) # Slow slither down
slither.runMainLoop(run_a_frame)
slither.runMainLoop(run_a_frame)