From e41f162f8f24c335c7c515885d7792c4aeca9185 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 10:17:03 -0500 Subject: [PATCH 1/9] Add isTouching() method --- slither/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/slither/__init__.py b/slither/__init__.py index fb55800..c49606e 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -136,6 +136,13 @@ 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 + + return ourRect.colliderect(theirRect) + pygame.mixer.init(44100, -16, 2, 2048) class Sound(): From 5666d4acf2498ccc3a53354a2fc4c5fc9354809c Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 11:35:11 -0500 Subject: [PATCH 2/9] Add parentheses to end of get_rect Whoops. I was setting the variable with the function rather than calling the function. --- slither/__init__.py | 4 ++-- slither/tests/moveStepsTest.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index c49606e..57ce1eb 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -138,8 +138,8 @@ def delete(self): def isTouching(self, collideSprite): '''Detects if one sprite is touching another.''' - ourRect = self.currentCostume.get_rect - theirRect = collideSprite.currentCostume.get_rect + ourRect = self.currentCostume.get_rect() + theirRect = collideSprite.currentCostume.get_rect() return ourRect.colliderect(theirRect) 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) From 6222e22690496ddf595b388cd1473efcd24c7aad Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 11:36:54 -0500 Subject: [PATCH 3/9] Add touching test --- slither/tests/isTouchingTest.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 slither/tests/isTouchingTest.py diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py new file mode 100644 index 0000000..ea27d55 --- /dev/null +++ b/slither/tests/isTouchingTest.py @@ -0,0 +1,17 @@ +# isTouchingTest.py +import slither + +toucher = slither.Sprite() +toucher.addCostume("assets/arrow.png", "arrow") +toucher.costumeName = "arrow" +toucher.goto(0, 75) + +snakey = slither.Sprite() +snakey.goto(slither.WIDTH // 2, 75) + +def run_a_frame(): + toucher.moveSteps(5) + print toucher.isTouching(snakey) + +slither.setup() +slither.runMainLoop(run_a_frame) From 3bbc594cc558a357395c3f0a38bc81c562db44e8 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 11:40:05 -0500 Subject: [PATCH 4/9] Fix touching test Now it should be more descriptive. --- slither/__init__.py | 1 - slither/tests/isTouchingTest.py | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 57ce1eb..1546ced 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -140,7 +140,6 @@ def isTouching(self, collideSprite): '''Detects if one sprite is touching another.''' ourRect = self.currentCostume.get_rect() theirRect = collideSprite.currentCostume.get_rect() - return ourRect.colliderect(theirRect) pygame.mixer.init(44100, -16, 2, 2048) diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py index ea27d55..7cb1e26 100644 --- a/slither/tests/isTouchingTest.py +++ b/slither/tests/isTouchingTest.py @@ -11,7 +11,10 @@ def run_a_frame(): toucher.moveSteps(5) - print toucher.isTouching(snakey) + if toucher.isTouching(snakey): + print "Touching!" + else: + print "Not touching." slither.setup() slither.runMainLoop(run_a_frame) From 5cd31ef1d3ffc88db3da8e94cced7883450b8492 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 11:44:07 -0500 Subject: [PATCH 5/9] Remove "recalculateCostumeDataFromName" A really long-named function used exactly one, overly-confusing time. --- slither/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 1546ced..0379789 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] From 6252829edaab6a6bf26f1e1a019809dc871d7ad8 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 11:48:18 -0500 Subject: [PATCH 6/9] Debugging stuff So we can see what's going on. --- slither/__init__.py | 3 ++- slither/tests/isTouchingTest.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 0379789..e7758a3 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -138,7 +138,8 @@ def isTouching(self, collideSprite): '''Detects if one sprite is touching another.''' ourRect = self.currentCostume.get_rect() theirRect = collideSprite.currentCostume.get_rect() - return ourRect.colliderect(theirRect) + # Debugging + return (ourRect.colliderect(theirRect), ourRect, theirRect) pygame.mixer.init(44100, -16, 2, 2048) diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py index 7cb1e26..f93f5b1 100644 --- a/slither/tests/isTouchingTest.py +++ b/slither/tests/isTouchingTest.py @@ -10,11 +10,13 @@ snakey.goto(slither.WIDTH // 2, 75) def run_a_frame(): - toucher.moveSteps(5) - if toucher.isTouching(snakey): + toucher.moveSteps(3) + if toucher.isTouching(snakey)[0]: print "Touching!" else: print "Not touching." + print toucher.isTouching(snakey)[1] + print toucher.isTouching(snakey)[2] slither.setup() slither.runMainLoop(run_a_frame) From 39c9ad33c87a09b918588c44f0e64557b4d53821 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 11:54:39 -0500 Subject: [PATCH 7/9] Remove debugging stuff I know what's going on now. --- slither/__init__.py | 4 ++-- slither/tests/isTouchingTest.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index e7758a3..c89c49a 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -138,8 +138,8 @@ def isTouching(self, collideSprite): '''Detects if one sprite is touching another.''' ourRect = self.currentCostume.get_rect() theirRect = collideSprite.currentCostume.get_rect() - # Debugging - return (ourRect.colliderect(theirRect), ourRect, theirRect) + + return ourRect.colliderect(theirRect) pygame.mixer.init(44100, -16, 2, 2048) diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py index f93f5b1..35f3ec5 100644 --- a/slither/tests/isTouchingTest.py +++ b/slither/tests/isTouchingTest.py @@ -11,12 +11,10 @@ def run_a_frame(): toucher.moveSteps(3) - if toucher.isTouching(snakey)[0]: + if toucher.isTouching(snakey): print "Touching!" else: print "Not touching." - print toucher.isTouching(snakey)[1] - print toucher.isTouching(snakey)[2] slither.setup() slither.runMainLoop(run_a_frame) From bda738b2b69374296b06f0cb5d94d34cca8276f5 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Wed, 1 Jun 2016 12:10:48 -0500 Subject: [PATCH 8/9] Fix problems The function works as expected now. --- slither/__init__.py | 3 ++- slither/tests/isTouchingTest.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index c89c49a..8e68daa 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -138,7 +138,8 @@ 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) diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py index 35f3ec5..35a1c4f 100644 --- a/slither/tests/isTouchingTest.py +++ b/slither/tests/isTouchingTest.py @@ -4,10 +4,10 @@ toucher = slither.Sprite() toucher.addCostume("assets/arrow.png", "arrow") toucher.costumeName = "arrow" -toucher.goto(0, 75) +toucher.goto(0, 100) snakey = slither.Sprite() -snakey.goto(slither.WIDTH // 2, 75) +snakey.goto(slither.WIDTH // 2, 100) def run_a_frame(): toucher.moveSteps(3) From f17fef4cf07c16edd7d87cf564c275870b562589 Mon Sep 17 00:00:00 2001 From: BookOwl Date: Thu, 2 Jun 2016 16:06:58 -0400 Subject: [PATCH 9/9] Fix print statements in isTouchingTest.py --- slither/tests/isTouchingTest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py index 35a1c4f..5bdc81a 100644 --- a/slither/tests/isTouchingTest.py +++ b/slither/tests/isTouchingTest.py @@ -12,9 +12,9 @@ def run_a_frame(): toucher.moveSteps(3) if toucher.isTouching(snakey): - print "Touching!" + print("Touching!") else: - print "Not touching." + print("Not touching.") slither.setup() slither.runMainLoop(run_a_frame)