diff --git a/Installing-ImageMagick.md b/Installing-ImageMagick.md
new file mode 100644
index 0000000..82baaae
--- /dev/null
+++ b/Installing-ImageMagick.md
@@ -0,0 +1,23 @@
+# Installing ImageMagick
+In order for Slither to have support for SVG files, you need to have ImageMagick installed. Here are some installation instructions for common platforms.
+
+## On Windows
+Download and install the correct binary from http://www.imagemagick.org/script/binary-releases.php#windows
+
+## On OSX/MacOS
+If you have homebrew installed, the easiest way is to run `brew install imagemagick` in your terminal.
+
+If you have MacPorts installed, you can run `sudo port install ImageMagick` to install ImageMagick.
+
+Otherwise, follow the instructions at http://www.imagemagick.org/script/binary-releases.php#macosx
+
+## On GNU/Linux
+
+### Debian
+Run `sudo aptitude update && aptitude install imagemagick`
+
+### Ubuntu
+Run `sudo apt-get install imagemagick`
+
+### Other systems
+Use your package manager to install ImageMagick if it has it, otherwise try http://www.imagemagick.org/script/binary-releases.php#unix
diff --git a/README.md b/README.md
index 482c004..365ba4c 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@ If that doesn't work, run `python setup.py bdist_wheel`, and then run `pip insta
If it still fails, make sure you have `setuptools` fully updated and properly installed.
Remember to add `import slither` in your script.
-Note that you must have PyGame installed in order to use Slither.
+Note that you must have PyGame installed in order to use Slither. Also, in order to have SVG support, you need to [install ImageMagick](Installing-ImageMagick.md).
## Why use Slither?
Those just getting started with Python after using Scratch may want to go straight from one GUI to another. Slither allows those people to quickly make simple projects in a matter of minutes, while also learning Python.
diff --git a/setup.py b/setup.py
index 89f6ec8..ec5f88d 100644
--- a/setup.py
+++ b/setup.py
@@ -78,7 +78,8 @@
# your project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
- install_requires=[],
+ install_requires=[
+ ],
# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
diff --git a/slither/examples/mover.py b/slither/examples/mover.py
index a70770f..f5533c0 100644
--- a/slither/examples/mover.py
+++ b/slither/examples/mover.py
@@ -29,7 +29,7 @@ def run_a_frame():
controls["speed"] = max(controls["speed"] - 0.2, 0)
# Set the sprites direction...
sprite.direction = controls["turn"]
- # ... and move it speed steps
+ # ... and move it speed steps
sprite.moveSteps(controls["speed"])
# Add wraparound checks.
if sprite.xpos > slither.WIDTH:
@@ -47,5 +47,3 @@ def run_a_frame():
slither.setup("Move!")
# and start it running!
slither.runMainLoop(run_a_frame)
-
-
diff --git a/slither/examples/turnAndMove.py b/slither/examples/turnAndMove.py
index af22db6..ff8e3a1 100644
--- a/slither/examples/turnAndMove.py
+++ b/slither/examples/turnAndMove.py
@@ -1,4 +1,5 @@
import slither
+import pygame
snakey = slither.Sprite()
snakey.costumeName = "costume0"
diff --git a/slither/slither.py b/slither/slither.py
index 59c4c82..5466d8c 100644
--- a/slither/slither.py
+++ b/slither/slither.py
@@ -8,6 +8,31 @@
import pygame
import sys, os, collections, warnings, math
+import tempfile, shutil, atexit, subprocess
+try:
+ from shlex import quote
+except ImportError:
+ from pipes import quote
+
+_DEVNULL = open(os.devnull, "wb")
+
+# Check for ImageMagick
+if subprocess.call("identify -version", shell=True, stdout=_DEVNULL) != 0:
+ svgSupport = False
+ warnings.warn("Could not find ImageMagick, so there is no SVG support\n"
+ "See https://github.com/PySlither/Slither/blob/master/Installing-ImageMagick.md "
+ "for instructions on installing ImageMagick")
+else:
+ svgSupport = True
+
+tempdir = tempfile.mkdtemp(prefix="PySlither-")
+@atexit.register
+def _clean_temp_dir():
+ "Cleans the temp dir when the program quits"
+ shutil.rmtree(tempdir)
+
+class NoSVGSupportError(Exception):
+ pass
WIDTH, HEIGHT = (800, 600)
SCREEN_SIZE = (WIDTH, HEIGHT)
@@ -109,10 +134,53 @@ def setVisible(status):
def isFocused():
return bool(pygame.mouse.get_focused())
+#Costume classes
+class SVGCostume:
+ "A class that handles resizing the SVG correctly"
+ def __init__(self, costumePath, scale):
+ if not svgSupport:
+ raise NoSVGSupportError("You do not have ImageMagick installed correctly")
+ self.costumePath = costumePath
+ self.scale = scale
+ command = 'convert {} -format "%w %h" info:'.format(
+ '"' + quote(os.path.join(scriptdir, self.costumePath))[1:-1] + '"')
+ #print(command)
+ d = subprocess.check_output(command,
+ shell=True)
+ self.width, self.height = map(int, d.split())
+ #print(self.width, self.height, sep=", ")
+ self.createImage()
+
+ def createImage(self):
+ name = os.path.splitext(os.path.basename(self.costumePath))[0] + ".png"
+ in_ = os.path.join(scriptdir, self.costumePath)
+ path = os.path.join(tempdir, name)
+ subprocess.check_output("convert -density {den} -resize {w}x{h} {in_} {out}".format(
+ den=72*self.scale+5,
+ w=self.scale*self.width,
+ h=self.scale*self.height,
+ in_='"'+quote(in_)[1:-1]+'"',
+ out='"'+quote(path)[1:-1]+'"'),
+ shell=True)
+ self.img = pygame.image.load(path)
+
+ def resize(self, scale):
+ self.scale = scale
+ self.createImage()
+
+class PNGCostume:
+ "Dummy class to make SVG and PNG costumes the same"
+ def __init__(self, img):
+ self.img = img
+ def resize(self, scale):
+ pass
+
+
+
# Stage class
class Stage(object):
def __init__(self):
- self.snakey = pygame.image.load(os.path.join(os.path.dirname(__file__), "snakey.png"))
+ self.snakey = PNGCostume(pygame.image.load(os.path.join(os.path.dirname(__file__), "snakey.png")))
self.costumes = collections.OrderedDict({"costume0" : self.snakey})
self._costumeNumber = 0
self._costumeName = "costume0"
@@ -122,7 +190,11 @@ def __init__(self):
# Functions shared by sprites
def addCostume(self, costumePath, costumeName):
'''Add a costume based on a given path and name.'''
- costume = pygame.image.load(os.path.join(scriptdir, costumePath))
+ if os.path.splitext(costumePath)[1] in (".svg", ".svgx"):
+ costume = SVGCostume(costumePath, self.scale if hasattr(self, "scale") else 1)
+ else:
+ path = os.path.join(scriptdir, costumePath)
+ costume = PNGCostume(pygame.image.load(path))
self.costumes[costumeName] = costume
self._costumeName = costumeName # Switch to the new costume
@@ -172,7 +244,8 @@ def __init__(self):
self.ypos = 0 # Y Position
self.direction = 0 # Direction is how much to change the direction, hence why it starts at 0 and not 90
self.show = True
- self.scale = 1 # How much to multiply it by in the scale
+ self.showBoundingBox = False
+ self._scale = 1 # How much to multiply it by in the scale
self._zindex = 0 # How high up are we in the "z" axis?
sprites.append(self) # Add this sprite to the global list of sprites
@@ -188,6 +261,15 @@ def zindex(self, val):
self._zindex = val
reorderSprites()
+ @property
+ def scale(self):
+ return self._scale
+
+ @scale.setter
+ def scale(self, val):
+ self._scale = val
+ self.currentCostume.resize(self._scale)
+
def goto(self, xpos, ypos):
'''Go to xpos, ypos.'''
self.xpos = xpos
@@ -209,8 +291,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.img.get_rect()
+ theirRect = collideSprite.currentCostume.img.get_rect()
ourRect.center = (self.xpos, self.ypos)
theirRect.center = (collideSprite.xpos, collideSprite.ypos)
return ourRect.colliderect(theirRect)
@@ -302,13 +384,13 @@ def blit(screen):
screen.fill(slitherStage.bgColor)
if slitherStage.currentCostume:
- screen.blit(pygame.transform.scale(slitherStage.currentCostume, SCREEN_SIZE), (0, 0))
+ screen.blit(pygame.transform.scale(slitherStage.currentCostume.img, SCREEN_SIZE), (0, 0))
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
+ image = obj.currentCostume.img # So we can modify it and blit the modified version easily
# These next few blocks of code check if the object has the defaults before doing anything.
- if not obj.scale == 1:
+ if not obj.scale == 1 and not isinstance(obj.currentCostume, SVGCostume):
imageSize = image.get_size()
image = pygame.transform.scale(image, (int(imageSize[0] * obj.scale), int(imageSize[1] * obj.scale)))
if not obj.direction == 0:
@@ -316,6 +398,8 @@ def blit(screen):
new_rect = image.get_rect()
new_rect.center = (obj.xpos, obj.ypos)
screen.blit(image, new_rect)
+ if obj.showBoundingBox:
+ pygame.draw.rect(screen, (0,0,0), new_rect, 5)
#pygame.display.flip()
diff --git a/slither/tests/assets/SVG Logo.svg b/slither/tests/assets/SVG Logo.svg
new file mode 100644
index 0000000..e4b7e58
--- /dev/null
+++ b/slither/tests/assets/SVG Logo.svg
@@ -0,0 +1,259 @@
+
diff --git a/slither/tests/basicTest.py b/slither/tests/basicTest.py
index fb523b8..90e9492 100644
--- a/slither/tests/basicTest.py
+++ b/slither/tests/basicTest.py
@@ -1,3 +1,5 @@
+#! usr/bin/env python3
+# basicTest.py
import slither
snakey = slither.Sprite()
diff --git a/slither/tests/bgTest.py b/slither/tests/bgTest.py
index 4c3e705..82ec61f 100644
--- a/slither/tests/bgTest.py
+++ b/slither/tests/bgTest.py
@@ -1,3 +1,5 @@
+#! usr/bin/env python3
+# bgTest.py
import math
import slither
diff --git a/slither/tests/imageBounceTest.py b/slither/tests/imageBounceTest.py
index 7f1a36f..5f6e1f6 100644
--- a/slither/tests/imageBounceTest.py
+++ b/slither/tests/imageBounceTest.py
@@ -1,3 +1,5 @@
+#! usr/bin/env python3
+# imageBounceTest.py
import slither
def create_circle():
diff --git a/slither/tests/isTouchingTest.py b/slither/tests/isTouchingTest.py
index 5bdc81a..8e3a8b8 100644
--- a/slither/tests/isTouchingTest.py
+++ b/slither/tests/isTouchingTest.py
@@ -1,3 +1,4 @@
+#! usr/bin/env python3
# isTouchingTest.py
import slither
diff --git a/slither/tests/mouseTest.py b/slither/tests/mouseTest.py
index 86f3937..cd74aec 100644
--- a/slither/tests/mouseTest.py
+++ b/slither/tests/mouseTest.py
@@ -2,6 +2,7 @@
# mouseTest.py
import slither
+from slither import Mouse
def run_a_frame():
print(Mouse.buttonsPressed())
diff --git a/slither/tests/moveStepsTest.py b/slither/tests/moveStepsTest.py
index e5de8af..2a8995e 100644
--- a/slither/tests/moveStepsTest.py
+++ b/slither/tests/moveStepsTest.py
@@ -1,3 +1,4 @@
+#! usr/bin/env python3
# moveStepsTest.py
import slither
diff --git a/slither/tests/soundtest.py b/slither/tests/soundtest.py
index 8485f92..d195943 100644
--- a/slither/tests/soundtest.py
+++ b/slither/tests/soundtest.py
@@ -1,5 +1,9 @@
+#! usr/bin/env python3
+# soundtest.py
# Sound works, make sure your volume is up.
import slither
+import time
sound = slither.slitherSound.loadSound('assets/boom.wav')
sound.play()
+time.sleep(2)
diff --git a/slither/tests/svgTest.py b/slither/tests/svgTest.py
new file mode 100644
index 0000000..08c41ff
--- /dev/null
+++ b/slither/tests/svgTest.py
@@ -0,0 +1,30 @@
+# usr/bin/env python3
+# svgTest.py - Test Slither's SVG support
+
+import slither
+
+if slither.svgSupport:
+ print("We have SVG support!")
+else:
+ print("No SVGs today :(")
+
+svg = slither.Sprite()
+svg.addCostume("assets/SVG Logo.svg", "svg")
+svg.costumeNumber = 1
+svg.scale = 1
+svg.showBoundingBox = False
+svg.goto(100, 300)
+
+svg2 = slither.Sprite()
+svg2.addCostume("assets/SVG Logo.svg", "svg")
+svg2.costumeNumber = 1
+svg2.scale = 5
+svg2.showBoundingBox = False
+svg2.goto(500, 300)
+
+slither.setup() # Begin slither
+
+def run_a_frame():
+ pass
+
+slither.runMainLoop(run_a_frame)
diff --git a/slither/tests/textTest.py b/slither/tests/textTest.py
index 5c49cf5..ecc3683 100644
--- a/slither/tests/textTest.py
+++ b/slither/tests/textTest.py
@@ -1,3 +1,5 @@
+#! usr/bin/env python3
+# textTest.py
import slither
slither.slitherStage.bgColor = (40, 222, 40)
diff --git a/slither/tests/zindextest.py b/slither/tests/zindextest.py
index 7cf08f6..bcc1664 100644
--- a/slither/tests/zindextest.py
+++ b/slither/tests/zindextest.py
@@ -1,3 +1,5 @@
+#! usr/bin/env python3
+# zindextest.py
import slither
# Normally, since Snakey was made before SoExcited, SoExcited would be rendered after Snakey and be put on top.