Skip to content

Commit db54b1d

Browse files
committed
Merge pull request #47 from PySlither/update-examples
Fixed some bugs, updated and moved the examples
2 parents 4f268e6 + bcaa375 commit db54b1d

17 files changed

+59
-56
lines changed

examples/bg.png

-44.6 KB
Binary file not shown.

examples/soundtest.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

setup.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# To use a consistent encoding
88
#from codecs import open
99
from os import path
10+
import glob
1011

1112
here = path.abspath(path.dirname(__file__))
1213

@@ -20,7 +21,7 @@
2021
# Versions should comply with PEP440. For a discussion on single-sourcing
2122
# the version across setup.py and the project code, see
2223
# https://packaging.python.org/en/latest/single_source_version.html
23-
version='0.2.0',
24+
version='0.2.1',
2425

2526
description='A Python module that uses PyGame to bring Scratch-like features to Python',
2627
long_description='A Python module that uses PyGame to bring Scratch-like features to Python\nPyGame IS required for this module to work.',
@@ -64,7 +65,7 @@
6465

6566
# You can just specify the packages manually here if your project is
6667
# simple. Or you can use find_packages().
67-
packages=["slither"],
68+
packages=["slither", "slither.examples"],
6869

6970
# Alternatively, if you want to distribute just a my_module.py, uncomment
7071
# this:
@@ -87,7 +88,8 @@
8788
# installed, specify them here. If using Python 2.6 or less, then these
8889
# have to be included in MANIFEST.in as well.
8990
package_data={
90-
'slither': ['snakey.png']
91+
'slither': ['snakey.png'],
92+
'slither.examples': list(map(path.basename, glob.glob("slither/examples/*.png") + glob.glob("slither/examples/*.wav")))
9193
},
9294

9395
# Although 'package_data' is the preferred approach, in some case you may

slither/__init__.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Also make sure to credit sources like StackOverflow.
1111

1212
import pygame
13-
import sys, os
13+
import sys, os, collections, warnings
1414

1515
WIDTH, HEIGHT = (800, 600)
1616
SCREEN_SIZE = (WIDTH, HEIGHT)
@@ -26,7 +26,11 @@
2626
} # Create a dict of callbacks that do nothing
2727
globalscreen = None
2828

29-
scriptdir = os.path.dirname(os.path.realpath(__import__("__main__").__file__))
29+
try:
30+
scriptdir = os.path.dirname(os.path.realpath(__import__("__main__").__file__))
31+
except AttributeError:
32+
warnings.warn("Couldn't find scripts dir, some functions may not work.")
33+
scriptdir = os.path.realpath(".")
3034

3135
# Convienience functions
3236
# Taken from http://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame
@@ -39,7 +43,7 @@ def rotateCenter(image, angle):
3943
class Stage():
4044
def __init__(self):
4145
self.snakey = pygame.image.load(os.path.join(os.path.dirname(__file__), "snakey.png"))
42-
self.costumes = {"costume0" : self.snakey}
46+
self.costumes = collections.OrderedDict({"costume0" : self.snakey})
4347
self.costumeNumber = 0
4448
self.costumeName = "costume0"
4549
self.currentCostume = None
@@ -63,32 +67,31 @@ def deleteCostumeByNumber(self, number):
6367
if number < len(self.costumes.keys()):
6468
costumeName = self.costumes.keys()[number]
6569
self.deleteCostumeByName(costumeName) # TODO: Fix this stupid "get name from number" thing
66-
70+
6771
@property
6872
def costumeNumber(self):
6973
'''The number of the costume the sprite is showing'''
7074
return self._costumeNumber
7175

7276
@costumeNumber.setter
7377
def costumeNumber(self, val):
74-
if number < len(self.costumes.keys()): # TODO: use mod to wrap around
78+
if val < len(self.costumes.keys()): # TODO: use mod to wrap around
7579
self.costumeName = list(self.costumes.keys())[val]
76-
self.costumeNumber = val
80+
self._costumeNumber = val
7781

7882
@property
7983
def costumeName(self):
8084
'''The name of the costume the sprite is showing'''
8185
return self._costumeName
82-
86+
8387
@costumeName.setter
8488
def costumeName(self, val):
8589
if val in self.costumes:
8690
self.recalculateCostumeDataFromName(val)
8791

8892
def recalculateCostumeDataFromName(self, name):
89-
self.costumeName = name
90-
self.costumeNumber = list(self.costumes.keys()).index(name)
91-
self.currentCostume = self.costumes[self.costumeNumber]
93+
self._costumeName = name
94+
self.currentCostume = self.costumes[self.costumeName]
9295

9396

9497
slitherStage = Stage()
@@ -139,23 +142,28 @@ def delete(self):
139142
'''Remove the sprite from the global sprites list, causing it not to be drawn.'''
140143
sprites.remove(self)
141144

142-
class Sound():
143-
# Based on pygame examples
144-
def loadSound(self, name):
145-
'''Load a sound. Set this function to a variable then call variable.play()'''
146-
class NoneSound:
147-
def play(self): pass
148-
if not pygame.mixer or not pygame.mixer.get_init():
149-
return NoneSound()
150-
fullname = name
151-
try:
152-
sound = pygame.mixer.Sound(fullname)
153-
except pygame.error as e:
154-
print ('Cannot load sound: %s' % fullname)
155-
raise e
156-
return sound
157-
158-
slitherSound = Sound()
145+
# DOES NOT WORK!
146+
# class Sound():
147+
# # Based on pygame examples
148+
# def loadSound(self, name):
149+
# '''Load a sound. Set this function to a variable then call variable.play()'''
150+
# try:
151+
# pygame.mixer.get_init()
152+
# except:
153+
# pass
154+
# class NoneSound:
155+
# def play(self): pass
156+
# if not pygame.mixer:
157+
# return NoneSound()
158+
# fullname = os.path.join(scriptdir, name)
159+
# try:
160+
# sound = pygame.mixer.Sound(fullname)
161+
# except pygame.error as e:
162+
# print ('Cannot load sound: %s' % fullname)
163+
# raise e
164+
# return sound
165+
#
166+
# slitherSound = Sound()
159167

160168
def setup(caption=sys.argv[0]):
161169
'''Sets up PyGame and returns a screen object that can be used with blit().'''
@@ -182,7 +190,7 @@ def blit(screen):
182190
screen.fill(slitherStage.bgColor)
183191

184192
if slitherStage.currentCostume:
185-
screen.blit(pygame.transform.scale(slitherStage.currentCostume, SCREEN_SIZE, (0, 0)))
193+
screen.blit(pygame.transform.scale(slitherStage.currentCostume, SCREEN_SIZE), (0, 0))
186194

187195
for obj in sprites:
188196
if obj.isVisible(): # Check if the object is showing before we do anything
File renamed without changes.
File renamed without changes.

slither/examples/__init__.py

Whitespace-only changes.

examples/basicTest.py renamed to slither/examples/basicTest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import slither, pygame, time
22

33
snakey = slither.Sprite()
4-
snakey.setCostumeByName("costume0") # You can set the default costume (costume0) by name...
4+
snakey.costumeName = "costume0" # You can set the default costume (costume0) by name...
5+
snakey.ypos = 100
56

67
SoExcited = slither.Sprite()
78
SoExcited.addCostume("SoExcited.png", "avatar")
8-
SoExcited.setCostumeByNumber(0) # ...or you can use a number.
9+
SoExcited.costumeNumber = 1 # ...or you can use a number.
910

10-
SoExcited.goTt(300, 300)
11+
SoExcited.goto(300, 300)
1112
SoExcited.scale = 0.33 # May look grainy when used on too low a scale.
1213

13-
slither.slitherStage.setColor(40, 222, 40)
14+
slither.slitherStage.bgColor = (40, 222, 40)
1415

1516
slither.setup() # Begin slither
1617

File renamed without changes.

examples/bgTest.py renamed to slither/examples/bgTest.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import slither, time, math
1+
import slither, math
22

33
snakey = slither.Sprite()
4-
snakey.setCostumeByName("costume0")
5-
snakey.ypos = 360
4+
snakey.ypos = 460
65

7-
slither.slitherStage.setColor(40, 222, 40)
6+
slither.slitherStage.bgColor = (40, 222, 40)
87
slither.slitherStage.addCostume("bg2.png", "bg")
9-
slither.slitherStage.setCostumeByName("bg")
8+
slither.slitherStage.costumeName = "bg"
109

1110
slither.setup() # Begin slither'
1211

0 commit comments

Comments
 (0)