10
10
# Also make sure to credit sources like StackOverflow.
11
11
12
12
import pygame
13
- import sys , os
13
+ import sys , os , collections , warnings
14
14
15
15
WIDTH , HEIGHT = (800 , 600 )
16
16
SCREEN_SIZE = (WIDTH , HEIGHT )
26
26
} # Create a dict of callbacks that do nothing
27
27
globalscreen = None
28
28
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 ("." )
30
34
31
35
# Convienience functions
32
36
# 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):
39
43
class Stage ():
40
44
def __init__ (self ):
41
45
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 })
43
47
self .costumeNumber = 0
44
48
self .costumeName = "costume0"
45
49
self .currentCostume = None
@@ -63,32 +67,31 @@ def deleteCostumeByNumber(self, number):
63
67
if number < len (self .costumes .keys ()):
64
68
costumeName = self .costumes .keys ()[number ]
65
69
self .deleteCostumeByName (costumeName ) # TODO: Fix this stupid "get name from number" thing
66
-
70
+
67
71
@property
68
72
def costumeNumber (self ):
69
73
'''The number of the costume the sprite is showing'''
70
74
return self ._costumeNumber
71
75
72
76
@costumeNumber .setter
73
77
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
75
79
self .costumeName = list (self .costumes .keys ())[val ]
76
- self .costumeNumber = val
80
+ self ._costumeNumber = val
77
81
78
82
@property
79
83
def costumeName (self ):
80
84
'''The name of the costume the sprite is showing'''
81
85
return self ._costumeName
82
-
86
+
83
87
@costumeName .setter
84
88
def costumeName (self , val ):
85
89
if val in self .costumes :
86
90
self .recalculateCostumeDataFromName (val )
87
91
88
92
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 ]
92
95
93
96
94
97
slitherStage = Stage ()
@@ -139,23 +142,28 @@ def delete(self):
139
142
'''Remove the sprite from the global sprites list, causing it not to be drawn.'''
140
143
sprites .remove (self )
141
144
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()
159
167
160
168
def setup (caption = sys .argv [0 ]):
161
169
'''Sets up PyGame and returns a screen object that can be used with blit().'''
@@ -182,7 +190,7 @@ def blit(screen):
182
190
screen .fill (slitherStage .bgColor )
183
191
184
192
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 ))
186
194
187
195
for obj in sprites :
188
196
if obj .isVisible (): # Check if the object is showing before we do anything
0 commit comments