From e3ed19c24622c71880f40e0b150609cc161529a4 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 28 Mar 2016 12:56:27 -0500 Subject: [PATCH 1/6] Start on #30 Removes set, changeBy, and get functions, except for: - Costumes, since those are handled specially - goTo (although this may also be removed) --- slither/__init__.py | 51 --------------------------------------------- 1 file changed, 51 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 7aea278..ebb2736 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -112,62 +112,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): '''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 From 13801a6579d21ded0c95977c3416525ff37b5f09 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 28 Mar 2016 13:01:24 -0500 Subject: [PATCH 2/6] Rename goTo to goto --- slither/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slither/__init__.py b/slither/__init__.py index ebb2736..942126f 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -112,7 +112,7 @@ def hide(self): '''Hide the sprite.''' self.showing = False - def goTo(self, xpos, ypos): + def goto(self, xpos, ypos): '''Go to xpos, ypos.''' self.xpos = xpos self.ypos = ypos From ba6d07cec3b78cf3c57e6386ec4ab73e46b4fe14 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 28 Mar 2016 13:05:27 -0500 Subject: [PATCH 3/6] Fix tests Fixes the tests to use the new syntax. --- examples/basicTest.py | 8 ++++---- examples/bgTest.py | 6 ++++-- examples/imageBounceTest.py | 10 ++++++---- examples/turnAndMove.py | 10 +++++----- examples/zindextest.py | 12 ++++++------ 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/examples/basicTest.py b/examples/basicTest.py index e318ec1..81d5cdb 100644 --- a/examples/basicTest.py +++ b/examples/basicTest.py @@ -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) diff --git a/examples/bgTest.py b/examples/bgTest.py index d4e70ee..8df9fd9 100644 --- a/examples/bgTest.py +++ b/examples/bgTest.py @@ -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") @@ -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) diff --git a/examples/imageBounceTest.py b/examples/imageBounceTest.py index f240dbf..8ef0bb1 100644 --- a/examples/imageBounceTest.py +++ b/examples/imageBounceTest.py @@ -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) diff --git a/examples/turnAndMove.py b/examples/turnAndMove.py index a1e16ca..aca871d 100644 --- a/examples/turnAndMove.py +++ b/examples/turnAndMove.py @@ -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 @@ -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) diff --git a/examples/zindextest.py b/examples/zindextest.py index 292b8a8..0af5e7a 100644 --- a/examples/zindextest.py +++ b/examples/zindextest.py @@ -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) From 36e4068ed3607d8276c376c7d82a426ac57b6a64 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 28 Mar 2016 19:27:45 -0400 Subject: [PATCH 4/6] I made zindex a property, which means that the sprite list will be automatically be reordered when a sprites zindex is changed. --- slither/__init__.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 942126f..ccae0da 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -101,9 +101,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 @@ -160,7 +172,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.''' From 58ac94ab3caf3069c3b1dfa733359f2d697a1575 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Tue, 29 Mar 2016 14:15:38 -0500 Subject: [PATCH 5/6] Remove get/set functions from stage --- slither/__init__.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 942126f..587a7c7 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -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.''' @@ -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. From b0822adde70f0a9c8579056747d1bd39072d24e4 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Tue, 29 Mar 2016 14:18:54 -0500 Subject: [PATCH 6/6] NOW remove set/get from stage --- slither/__init__.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index ccae0da..76378b8 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -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.''' @@ -80,14 +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()