16 self
.screen
= pygame
.display
.set_mode ((640, 480))
17 pygame
.display
.set_caption ("The Adventures of Butaba")
19 # initalize background graphics
20 self
.tileset
= pygame
.image
.load (os
.path
.join ("background", "tileset.png")).convert ()
22 # initialize object graphics
23 self
.redpotion
= pygame
.image
.load (os
.path
.join ("objects", "red-potion.png")).convert ()
24 self
.redpotion
.set_colorkey (pygame
.Color (0, 255, 0))
25 self
.goldcoins
= pygame
.image
.load (os
.path
.join ("objects", "gold-coins.png")).convert ()
26 self
.goldcoins
.set_colorkey (pygame
.Color (0, 255, 0))
27 self
.wand
= pygame
.image
.load (os
.path
.join ("objects", "wand.png")).convert ()
28 self
.wand
.set_colorkey (pygame
.Color (0, 255, 0))
29 self
.bulb
= pygame
.image
.load (os
.path
.join ("objects", "bulb.png")).convert ()
30 self
.bulb
.set_colorkey (pygame
.Color (0, 255, 0))
31 self
.lightning
= pygame
.image
.load (os
.path
.join ("objects", "lightning.png")).convert ()
32 self
.lightning
.set_colorkey (pygame
.Color (0, 255, 0))
34 self
.key
= pygame
.image
.load (os
.path
.join ("objects", "key.png")).convert ()
35 self
.key
.set_colorkey (pygame
.Color (0, 255, 0))
37 # initialize player graphics
38 self
.butabafront
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-front.png")).convert ()
39 self
.butabafront
.set_colorkey (pygame
.Color (0, 255, 0))
40 self
.butababack
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-back.png")).convert ()
41 self
.butababack
.set_colorkey (pygame
.Color (0, 255, 0))
42 self
.butabaleft
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-left.png")).convert ()
43 self
.butabaleft
.set_colorkey (pygame
.Color (0, 255, 0))
44 self
.butabaright
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-right.png")).convert ()
45 self
.butabaright
.set_colorkey (pygame
.Color (0, 255, 0))
49 # set current level and position of our character
50 self
.currentlevel
= self
.level1
52 self
.butaba
= butaba
.Butaba (5,0, butaba
.Butaba
.RIGHT
)
54 # set up the levels and their interactions
55 def setup_levels (self
):
57 self
.level1
= level
.Level (level
.LEVEL_1
)
58 self
.level1e
= level
.Level (level
.LEVEL_1E
,
59 objects
= [ object.Key (4, 3, self
.key
, level
.KEY_CHEST1
) ])
60 self
.level1
.levelright
= self
.level1e
61 self
.level1e
.levelleft
= self
.level1
67 self
.screen
.fill (pygame
.Color (0,0,0))
69 self
.draw_level_background (self
.currentlevel
)
71 self
.draw_level_objects (self
.currentlevel
)
74 # draw the status info
77 pygame
.display
.update ()
80 for event
in pygame
.event
.get ():
81 if event
.type == pygame
.QUIT
:
83 if event
.type == pygame
.KEYDOWN
:
84 if event
.key
== pygame
.K_UP
:
85 self
.move_butaba_up ()
86 elif event
.key
== pygame
.K_DOWN
:
87 self
.move_butaba_down ()
88 elif event
.key
== pygame
.K_LEFT
:
89 self
.move_butaba_left ()
90 elif event
.key
== pygame
.K_RIGHT
:
91 self
.move_butaba_right ()
93 def move_butaba_up (self
):
94 # first if butaba is not facing up, make him face up
95 if self
.butaba
.position
<> butaba
.Butaba
.BACK
:
96 self
.butaba
.position
= butaba
.Butaba
.BACK
99 # if butaba is trying to move off the top of the screen
100 if self
.butaba
.row
<= 0:
101 # if there is a level above set current level to that one
102 if self
.currentlevel
.leveltop
is not None:
103 # make sure there is no obstacle
104 lastrow
= len (self
.currentlevel
.leveltop
.background
) - 1
105 if self
.check_background_obstacle (self
.currentlevel
.leveltop
, lastrow
, self
.butaba
.col
) is False:
106 self
.currentlevel
= self
.currentlevel
.leveltop
107 self
.butaba
.row
= lastrow
108 # normal upward movement
109 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
-1, self
.butaba
.col
) is False:
112 def move_butaba_down (self
):
113 # first if butaba is not facing forward, make him face forward/down
114 if self
.butaba
.position
<> butaba
.Butaba
.FRONT
:
115 self
.butaba
.position
= butaba
.Butaba
.FRONT
118 # if butaba is trying to move off the bottom of the screen
119 if self
.butaba
.row
>= len (self
.currentlevel
.background
)-1:
120 # if there is a level below set current level to that one
121 if self
.currentlevel
.levelbottom
is not None:
122 # make sure there is no obstacle at that position
123 if self
.check_background_obstacle (self
.currentlevel
.levelbottom
, 0, self
.butaba
.col
) is False:
124 self
.currentlevel
= self
.currentlevel
.levelbottom
126 # normal downward movement
127 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
+1, self
.butaba
.col
) is False:
130 # check if a background tile is an obstacle
131 def check_background_obstacle (self
, level
, row
, col
):
132 if (level
.background
[row
][col
][2] == 1):
137 def move_butaba_left (self
):
138 # first if Butaba is not facing left, make him face left
139 if self
.butaba
.position
<> butaba
.Butaba
.LEFT
:
140 self
.butaba
.position
= butaba
.Butaba
.LEFT
143 # if butaba is trying to move off the left edge
144 if self
.butaba
.col
<= 0:
145 # if there is a level to the right set current level to that one
146 if self
.currentlevel
.levelleft
is not None:
147 # make sure there is no obstacle at that position of movement
148 # get the last column of the previous level
149 lastcol
= len (self
.currentlevel
.levelleft
.background
[0]) - 1
150 if self
.check_background_obstacle (self
.currentlevel
.levelleft
, self
.butaba
.row
, lastcol
) is False:
151 self
.currentlevel
= self
.currentlevel
.levelleft
152 self
.butaba
.col
= lastcol
153 # normal left movement
154 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
-1) is False:
157 def move_butaba_right (self
):
158 # First if Butaba is not facing right make him face right
159 if self
.butaba
.position
<> butaba
.Butaba
.RIGHT
:
160 self
.butaba
.position
= butaba
.Butaba
.RIGHT
163 # if butaba is trying to move off the right edge
164 if self
.butaba
.col
>= len (self
.currentlevel
.background
[0])-1:
165 # if there is a level to the right swap current level with that one
166 if self
.currentlevel
.levelright
is not None:
167 # make sure there is no obstacle at that position of movement
168 # get the last column of the previous level
169 if self
.check_background_obstacle (self
.currentlevel
.levelright
, self
.butaba
.row
, 0) is False:
170 self
.currentlevel
= self
.currentlevel
.levelright
172 # normal right movement
173 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
+ 1) is False:
176 def draw_butaba (self
):
177 if self
.butaba
.position
== butaba
.Butaba
.FRONT
:
178 self
.screen
.blit (self
.butabafront
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
179 elif self
.butaba
.position
== butaba
.Butaba
.BACK
:
180 self
.screen
.blit (self
.butababack
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
181 elif self
.butaba
.position
== butaba
.Butaba
.LEFT
:
182 self
.screen
.blit (self
.butabaleft
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
183 elif self
.butaba
.position
== butaba
.Butaba
.RIGHT
:
184 self
.screen
.blit (self
.butabaright
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
187 # Draw the status infodisplay
188 def draw_status (self
):
189 self
.screen
.blit (self
.redpotion
, (485, 10))
190 utility
.put_text (self
.screen
, 550, 25, 28, (255, 0, 0), "%d" % self
.butaba
.health
)
191 self
.screen
.blit (self
.goldcoins
, (485, 50))
192 utility
.put_text (self
.screen
, 550, 75, 28, (255, 255, 0), "%d" % self
.butaba
.gold
)
193 self
.screen
.blit (self
.wand
, (485, 120))
194 utility
.put_text (self
.screen
, 550, 130, 28, (0, 0, 255), "%d" % self
.butaba
.magic
)
195 self
.screen
.blit (self
.bulb
, (485, 180))
196 utility
.put_text (self
.screen
, 550, 190, 28, (0, 255, 0), "%d" % self
.butaba
.experience
)
197 self
.screen
.blit (self
.lightning
, (485, 240))
198 utility
.put_text (self
.screen
, 550, 250, 28, (255,255,255), "%d" % self
.butaba
.strength
)
201 # Draw the level background tiles on surface
202 def draw_level_background (self
, level
):
204 for row
in level
.background
:
206 for tilerow
, tilecol
, is_solid
in row
:
209 self
.screen
.blit (self
.tileset
, (j
*48, i
*48), pygame
.Rect (tilex
, tiley
, 48, 48))
214 # Draw the level objects
215 def draw_level_objects (self
, level
):
216 for obj
in level
.objects
:
217 if obj
.image
is not None:
218 self
.screen
.blit (obj
.image
, (obj
.col
*48, obj
.row
*48))