diff --git a/slither/__init__.py b/slither/__init__.py index fb55800..8e68daa 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -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 @@ -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] @@ -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(): diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py new file mode 100644 index 0000000..5bdc81a --- /dev/null +++ b/slither/tests/isTouchingTest.py @@ -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) diff --git a/slither/tests/moveStepsTest.py b/slither/tests/moveStepsTest.py index df4a5b2..e5de8af 100644 --- a/slither/tests/moveStepsTest.py +++ b/slither/tests/moveStepsTest.py @@ -12,4 +12,4 @@ def run_a_frame(): slither.setup() slither.setFPS(30) # Slow slither down -slither.runMainLoop(run_a_frame) \ No newline at end of file +slither.runMainLoop(run_a_frame)