From e78d6b890a1d069365fae6460d1e78ba1e7d1eae Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Tue, 29 Mar 2016 14:28:06 -0500 Subject: [PATCH 01/13] Random comment changes So I can start the pull request. --- slither/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index f571fb8..37d363d 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -1,6 +1,6 @@ # SLITHER FOR PYTHON 2 AND 3 # Hi there, code divers! -# There's a lot of cool stuff here that has comments so you can understand what I'm doing. +# There's a lot of cool stuff here that has comments so you can understand what this is doing. # You can even mess around with it yourself :) # If you think your messing around might help, go to: # https://github.com/Tymewalk/Slither @@ -28,7 +28,7 @@ # This function is broken, issue #9 remains def rotateCenter(image, angle): """rotate a Surface, maintaining position.""" - loc = image.get_rect().center #rot_image is not defined + loc = image.get_rect().center # rot_image is not defined rot_sprite = pygame.transform.rotate(image, angle) rot_sprite.get_rect().center = loc return rot_sprite @@ -48,7 +48,7 @@ def addCostume(self, costumePath, costumeName): '''Add a costume based on a given path and name.''' costume = pygame.image.load(os.path.join(scriptdir, costumePath)) self.costumes[costumeName] = costume - if len(self.costumes.keys()) == 1: # Just added first costume to stage/sprite + if len(self.costumes.keys()) == 1: # Just added first costume to stage/sprite... self.setCostumeByName(costumeName) # ...So switch to it! def deleteCostumeByName(self, name): @@ -73,7 +73,8 @@ def setCostumeByNumber(self, number): '''Set a costume by its number.''' if number < len(self.costumes.keys()): costumeName = list(self.costumes.keys())[number] - self.setCostumeByName(costumeName) + self.setCostumeByName(costumeName) # Wait, if we have the number, we use the (really not needed but convienient) name? + # WHY DID I DO THIS?! slitherStage = Stage() From bb11331672ddb72fef96f002b0f4792e7c644495 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Tue, 29 Mar 2016 14:38:09 -0500 Subject: [PATCH 02/13] Costume properties test Just testing this. --- slither/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 37d363d..31a7102 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -43,7 +43,7 @@ def __init__(self): self.currentCostume = None self.bgColor = (255, 255, 255) - # Functions shared by sprites + # Costume adding and removing def addCostume(self, costumePath, costumeName): '''Add a costume based on a given path and name.''' costume = pygame.image.load(os.path.join(scriptdir, costumePath)) @@ -60,8 +60,16 @@ def deleteCostumeByNumber(self, number): '''Delete a costume by number.''' if number < len(self.costumes.keys()): costumeName = self.costumes.keys()[number] - self.deleteCostumeByName(costumeName) - + self.deleteCostumeByName(costumeName) # TODO: Fix this stupid "get name from number" thing + @property + def costumeNumber(self): + '''The number of the costume the sprite is showing''' + return self._costumeNumber + + @costumeNumber.setter + def costumeNumber(self, val): + self.setCostumeByNumber(val) + def setCostumeByName(self, name): '''Set a costume by its name.''' if name in self.costumes: From 579aa01e70774bc347fd2a54769e3539cd6a921c Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Tue, 29 Mar 2016 15:05:47 -0500 Subject: [PATCH 03/13] Add costumeName as a property --- slither/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/slither/__init__.py b/slither/__init__.py index 31a7102..8e319aa 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -61,6 +61,7 @@ def deleteCostumeByNumber(self, number): if number < len(self.costumes.keys()): costumeName = self.costumes.keys()[number] self.deleteCostumeByName(costumeName) # TODO: Fix this stupid "get name from number" thing + @property def costumeNumber(self): '''The number of the costume the sprite is showing''' @@ -69,7 +70,17 @@ def costumeNumber(self): @costumeNumber.setter def costumeNumber(self, val): self.setCostumeByNumber(val) - + + @property + def costumeName(self): + '''The name of the costume the sprite is showing''' + return self._costumeName + + @costumeName.setter + def costumeNumber(self, val): + self.setCostumeByName(val) + + # TODO: Fix these def setCostumeByName(self, name): '''Set a costume by its name.''' if name in self.costumes: From 14d150d49640f18e4863e03b4ca48462c4aa8132 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 30 Mar 2016 15:32:37 -0400 Subject: [PATCH 04/13] Add the codacy badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a41087d..1bddb4d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ ![Slither's logo](https://cloud.githubusercontent.com/assets/13385064/14028163/82c374ea-f1d1-11e5-84b7-8ebc442b2d70.png) +[![Codacy Badge](https://api.codacy.com/project/badge/grade/6c21312189334c1782a49e152d0f4b78)](https://www.codacy.com/app/stanleybookowl/Slither) + ## What is Slither? Slither is a Python module that uses PyGame to bring Scratch-like features to Python. [Scratch](scratch.mit.edu) is a programming language developed by MIT that uses block-based scripts to introduce kids to programming. From 4368f8032f498672f6e381d38e2ffc4a9043db9e Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 30 Mar 2016 15:42:40 -0400 Subject: [PATCH 05/13] Rewrite some of the README --- README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1bddb4d..98836c9 100644 --- a/README.md +++ b/README.md @@ -45,5 +45,20 @@ Finally, check to see if you have the newest version of Slither. If you've done all three of those things and it still doesn't work, file an issue. -## Credits -Scratch is property of The Lifelong Kindergarten Group. I am not affiliated with the LLK, nor do I own Scratch. +# Contributors +A list of all contributors who have helped or are helping. + +### @Tymewalk +Started Slither, added several groundwork features. + +### @BookOwl +Helped majorly rewrite Slither so it's easier to code in. Additionally helped suggest changes and fix bugs, which made Slither much better. + +### @Omegabyte +Began and currently works on documentation, also added sound support. + +### @IronManMark20 +Is working on adding SVG support to slither. + +# Credits +Scratch is property of The Lifelong Kindergarten Group. The slither devs are not affiliated with the LLK, nor do they own Scratch. From dccd3a78451a41efd71cf01ecbe1d6626d82fc9a Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 30 Mar 2016 16:33:43 -0400 Subject: [PATCH 06/13] Update imageBounceTest.py to the new API --- examples/imageBounceTest.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/examples/imageBounceTest.py b/examples/imageBounceTest.py index 8ef0bb1..333b989 100644 --- a/examples/imageBounceTest.py +++ b/examples/imageBounceTest.py @@ -1,16 +1,25 @@ import slither -circle = slither.Sprite() -circle.addCostume("circle.png", "circle") -circle.setCostumeByName("circle") -circle.goto(0,0) +def create_circle(): + circle = slither.Sprite() + circle.addCostume("circle.png", "circle") + circle.setCostumeByName("circle") + circle.goto(0,0) + return circle +circle1, circle2, circle3 = create_circle(), create_circle(), create_circle() -slither.slitherStage.setColor(255, 255, 255) +slither.slitherStage.bgcolor = (255, 255, 255) def run_a_frame(): - circle.xpos += 1 - circle.ypos += 1 - circle.direction += 1 + circle1.xpos += 1 + circle1.ypos += 1 + circle1.direction += 1 + + circle2.xpos += 1 + circle2.direction += 1 + + circle3.ypos += 1 + circle3.direction += 1 slither.setup() slither.runMainLoop(run_a_frame) From b2373dc0c577f9052b51484ec8d3ca41769b3ddd Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 31 Mar 2016 10:42:38 -0400 Subject: [PATCH 07/13] Fix issue #9 --- examples/circle.png | Bin 411 -> 404 bytes examples/imageBounceTest.py | 14 +++++++++----- slither/__init__.py | 7 ++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/circle.png b/examples/circle.png index 9fea960d9cbe721102e8d0afd7fae54639860007..f5d8b4da30528fb50e83fb21bbfbd91a8922c795 100644 GIT binary patch delta 321 zcmV-H0lxm51C#@hB#|)~3c>&Y4#EKyC`y2lQzw5CNklKbOq?E$<&k&UfAvCFt%0zmoL=C#9(1SGWrRyX;KwBxI6LjH~N@(t; zi~eCNr=2u>gl8Of(7=%%vAauyEV#$LgU|p=5}PU=wCEbQL9#VJD{8iE&9b6K4;E$+ zD;9s=U)12q8N`~O47}-3EP51+9>tC=us^C^ESUIGl(IVAQ@`# zU||NaV&VIhphnBq+%rgq4p?-}wvwQOmL#~EpaE7S+y&WVeJMtT32cCd(JdbpHSZH*^6$^2*w{kO0v6G9kL8C8$PoW2CILlNgJwRJ2 zq7J(7OeG9g(?wS}O1F}R-(int1r5B@7S&T4WWgHGUW5i%k~pN)Xwf}x7fF` z-bD<#Es~%D4`$v)teE-zicq0tOP;w%f*LHk=a@vO(UJtG5gK4c!rdY@>L(Ezbf+!a zrx6->hdufgD>VEnM;KltL6@AV1X*>2E<8gi@{Sd{T*F!BJ0lC|K@v64nNmOxC3RVr Y4}Saz*%|J{vj6}907*qoM6N<$f;F3q2><{9 diff --git a/examples/imageBounceTest.py b/examples/imageBounceTest.py index 333b989..bb21cab 100644 --- a/examples/imageBounceTest.py +++ b/examples/imageBounceTest.py @@ -7,19 +7,23 @@ def create_circle(): circle.goto(0,0) return circle circle1, circle2, circle3 = create_circle(), create_circle(), create_circle() - +circle4 = create_circle() +circle4.goto(400,300) +circle4.debug = True slither.slitherStage.bgcolor = (255, 255, 255) def run_a_frame(): - circle1.xpos += 1 - circle1.ypos += 1 + circle1.xpos += 0.5 + circle1.ypos += 0.5 circle1.direction += 1 - circle2.xpos += 1 + circle2.xpos += 0.5 circle2.direction += 1 - circle3.ypos += 1 + circle3.ypos += 0.5 circle3.direction += 1 + circle4.direction += 1 + slither.setup() slither.runMainLoop(run_a_frame) diff --git a/slither/__init__.py b/slither/__init__.py index f571fb8..6f5a337 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -28,9 +28,7 @@ # This function is broken, issue #9 remains def rotateCenter(image, angle): """rotate a Surface, maintaining position.""" - loc = image.get_rect().center #rot_image is not defined rot_sprite = pygame.transform.rotate(image, angle) - rot_sprite.get_rect().center = loc return rot_sprite # Stage class @@ -171,13 +169,16 @@ def blit(screen): for obj in sprites: if obj.isVisible(): # Check if the object is showing before we do anything image = obj.currentCostume # So we can modify it and blit the modified version easily + orig_rect = image.get_rect() # These next few blocks of code check if the object has the defaults before doing anything. if not obj.scale == 1: imageSize = image.get_size() image = pygame.transform.scale(image, (int(imageSize[0] * obj.scale), int(imageSize[1] * obj.scale))) if not obj.direction == 0: image = rotateCenter(image, obj.direction) - screen.blit(image, (obj.xpos, obj.ypos)) + new_rect = image.get_rect() + new_rect.center = (obj.xpos, obj.ypos) + screen.blit(image, new_rect) pygame.display.flip() From b609735a4cb080b7e5475d3c0f28cfc9dd84903b Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 31 Mar 2016 10:55:13 -0400 Subject: [PATCH 08/13] Removed an unused line of code --- slither/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/slither/__init__.py b/slither/__init__.py index 6f5a337..5ec8805 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -169,7 +169,6 @@ def blit(screen): for obj in sprites: if obj.isVisible(): # Check if the object is showing before we do anything image = obj.currentCostume # So we can modify it and blit the modified version easily - orig_rect = image.get_rect() # These next few blocks of code check if the object has the defaults before doing anything. if not obj.scale == 1: imageSize = image.get_size() From 55d07e9d6adfe0bc1be7f85b469cfd8421e69897 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 31 Mar 2016 11:19:17 -0400 Subject: [PATCH 09/13] Made the screen size tuple a constant, so that it is easier to change --- slither/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 5ec8805..d94c165 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -8,7 +8,9 @@ import pygame import sys, os -# import time # For future timer + +WIDTH, HEIGHT = (800, 600) +SCREEN_SIZE = (WIDTH, HEIGHT) sprites = [] # List of all sprites clock = pygame.time.Clock() # Used to control framerate @@ -143,7 +145,7 @@ def setup(caption=sys.argv[0]): '''Sets up PyGame and returns a screen object that can be used with blit().''' global globalscreen pygame.init() - screen = pygame.display.set_mode((800, 600)) + screen = pygame.display.set_mode(SCREEN_SIZE) caption = pygame.display.set_caption(caption) globalscreen = screen return screen @@ -164,7 +166,7 @@ def blit(screen): screen.fill(slitherStage.bgColor) if slitherStage.currentCostume: - screen.blit(pygame.transform.scale(slitherStage.currentCostume, (800,600)), (0, 0)) + screen.blit(pygame.transform.scale(slitherStage.currentCostume, SCREEN_SIZE, (0, 0))) for obj in sprites: if obj.isVisible(): # Check if the object is showing before we do anything From e5b42db249dd94a0d7652881a8bba8ed78772d3e Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Thu, 31 Mar 2016 10:41:55 -0500 Subject: [PATCH 10/13] Fix small test problem\nBTW rotation works now, thanks @BookOwl --- examples/turnAndMove.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/turnAndMove.py b/examples/turnAndMove.py index aca871d..f436203 100644 --- a/examples/turnAndMove.py +++ b/examples/turnAndMove.py @@ -5,8 +5,6 @@ snakey.goto(0, 0) -slither.slitherStage.setColor(40, 222, 40) - slither.setup() # Begin slither def handlequit(): From 8db78fae63bcd10244364a0a6ed5cd5c98948c31 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Thu, 31 Mar 2016 10:52:56 -0500 Subject: [PATCH 11/13] FINALLY fix it So this should do it... hopefully. --- slither/__init__.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index f9b8697..8aa7b7b 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -69,31 +69,22 @@ def costumeNumber(self): @costumeNumber.setter def costumeNumber(self, val): - self.setCostumeByNumber(val) + if number < len(self.costumes.keys()): # TODO: use mod to wrap around + self.costumeName = list(self.costumes.keys())[val] + self.costumeNumber = val @property def costumeName(self): '''The name of the costume the sprite is showing''' return self._costumeName - + @costumeName.setter - def costumeNumber(self, val): - self.setCostumeByName(val) - - # TODO: Fix these - - def setCostumeByName(self, name): - '''Set a costume by its name.''' - if name in self.costumes: - self.currentCostume = self.costumes[name] - self.costumeName = name - self.costumeNumber = list(self.costumes.keys()).index(name) - - def setCostumeByNumber(self, number): - '''Set a costume by its number.''' - if number < len(self.costumes.keys()): - costumeName = list(self.costumes.keys())[number] - self.setCostumeByName(costumeName) + def costumeName(self, val): + if val in self.costumes: + self.currentCostume = self.costumes[val] + self.costumeName = val + self.costumeNumber = list(self.costumes.keys()).index(val) + slitherStage = Stage() From 60ce6fc3404eb113046cbb766c88197d3ab64831 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Thu, 31 Mar 2016 10:58:36 -0500 Subject: [PATCH 12/13] Fix problem with deleting It's better this way. --- slither/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 8aa7b7b..81c4532 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -54,7 +54,7 @@ def addCostume(self, costumePath, costumeName): def deleteCostumeByName(self, name): '''Delete a costume by name.''' self.costumes.pop(name, None) - setCostumeByName(self.costumeName) # Make sure we recalculate the costume number! + self.recalculateNumberFromName(self.costumeName) # Make sure we recalculate the costume data! def deleteCostumeByNumber(self, number): '''Delete a costume by number.''' @@ -81,9 +81,13 @@ def costumeName(self): @costumeName.setter def costumeName(self, val): if val in self.costumes: - self.currentCostume = self.costumes[val] - self.costumeName = val - self.costumeNumber = list(self.costumes.keys()).index(val) + self.recalculateCostumeDataFromName(val) + + def recalculateCostumeDataFromName(self, name): + self.costumeName = name + self.costumeNumber = list(self.costumes.keys()).index(name) + self.currentCostume = self.costumes[self.costumeNumber] + slitherStage = Stage() From f2313eeae736d691e8059fb6a1add8377d34becd Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Thu, 31 Mar 2016 11:03:52 -0500 Subject: [PATCH 13/13] Minor comment fixes Just something I did while checking to see if there's any other errors. --- slither/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/slither/__init__.py b/slither/__init__.py index 81c4532..f0c5a6b 100644 --- a/slither/__init__.py +++ b/slither/__init__.py @@ -1,11 +1,14 @@ # SLITHER FOR PYTHON 2 AND 3 # Hi there, code divers! -# There's a lot of cool stuff here that has comments so you can understand what I'm doing. +# There's a lot of cool stuff here that has comments so you can understand what's going on. # You can even mess around with it yourself :) # If you think your messing around might help, go to: -# https://github.com/Tymewalk/Slither +# https://github.com/PySlither/Slither # and make a pull request! +# Contributors: If your code doesn't seem straightforward, make comments so other people know what's going on +# Also make sure to credit sources like StackOverflow. + import pygame import sys, os @@ -27,7 +30,6 @@ # Convienience functions # Taken from http://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame -# This function is broken, issue #9 remains def rotateCenter(image, angle): """rotate a Surface, maintaining position.""" rot_sprite = pygame.transform.rotate(image, angle) @@ -87,8 +89,7 @@ def recalculateCostumeDataFromName(self, name): self.costumeName = name self.costumeNumber = list(self.costumes.keys()).index(name) self.currentCostume = self.costumes[self.costumeNumber] - - + slitherStage = Stage()