14 self
.screen
= pygame
.display
.set_mode ((640, 480))
15 pygame
.display
.set_caption ("The Adventures of Butaba")
17 # initalize background graphics
18 self
.tileset
= pygame
.image
.load (os
.path
.join ("background", "tileset.png")).convert ()
20 # initialize object graphics
21 self
.redpotion
= pygame
.image
.load (os
.path
.join ("objects", "red-potion.png")).convert ()
22 self
.redpotion
.set_colorkey (pygame
.Color (0, 255, 0))
23 self
.goldcoins
= pygame
.image
.load (os
.path
.join ("objects", "gold-coins.png")).convert ()
24 self
.goldcoins
.set_colorkey (pygame
.Color (0, 255, 0))
25 self
.wand
= pygame
.image
.load (os
.path
.join ("objects", "wand.png")).convert ()
26 self
.wand
.set_colorkey (pygame
.Color (0, 255, 0))
27 self
.bulb
= pygame
.image
.load (os
.path
.join ("objects", "bulb.png")).convert ()
28 self
.bulb
.set_colorkey (pygame
.Color (0, 255, 0))
29 self
.lightning
= pygame
.image
.load (os
.path
.join ("objects", "lightning.png")).convert ()
30 self
.lightning
.set_colorkey (pygame
.Color (0, 255, 0))
32 # initialize player graphics
33 self
.butabafront
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-front.png")).convert ()
34 self
.butabafront
.set_colorkey (pygame
.Color (0, 255, 0))
35 self
.butababack
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-back.png")).convert ()
36 self
.butababack
.set_colorkey (pygame
.Color (0, 255, 0))
37 self
.butabaleft
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-left.png")).convert ()
38 self
.butabaleft
.set_colorkey (pygame
.Color (0, 255, 0))
39 self
.butabaright
= pygame
.image
.load (os
.path
.join ("sprite", "butaba-right.png")).convert ()
40 self
.butabaright
.set_colorkey (pygame
.Color (0, 255, 0))
44 # set current level and position of our character
45 self
.currentlevel
= self
.level1
47 self
.butaba
= butaba
.Butaba (5,0, butaba
.Butaba
.RIGHT
)
49 # set up the levels and their interactions
50 def setup_levels (self
):
51 self
.level1
= level
.Level (level
.LEVEL_1
)
53 # function to draw text
54 def put_text (self
, x
, y
, size
, (r
,g
,b
), text
):
55 harisfont
= os
.path
.join ("font", "HarisComic-2.ttf")
56 surf
= pygame
.font
.Font (harisfont
, size
).render (text
, True, pygame
.Color (r
,g
,b
))
57 self
.screen
.blit (surf
, (x
, y
))
63 self
.screen
.fill (pygame
.Color (0,0,0))
65 self
.draw_level (self
.currentlevel
)
68 # draw the status info
69 self
.draw_information ()
71 pygame
.display
.update ()
74 for event
in pygame
.event
.get ():
75 if event
.type == pygame
.QUIT
:
77 if event
.type == pygame
.KEYDOWN
:
78 if event
.key
== pygame
.K_UP
:
79 self
.move_butaba_up ()
80 elif event
.key
== pygame
.K_DOWN
:
81 self
.move_butaba_down ()
82 elif event
.key
== pygame
.K_LEFT
:
83 self
.move_butaba_left ()
84 elif event
.key
== pygame
.K_RIGHT
:
85 self
.move_butaba_right ()
87 def move_butaba_up (self
):
88 # first if butaba is not facing up, make him face up
89 if self
.butaba
.position
<> butaba
.Butaba
.BACK
:
90 self
.butaba
.position
= butaba
.Butaba
.BACK
93 # if butaba is trying to move off the top of the screen
94 if self
.butaba
.row
<= 0:
95 # if there is a level above set current level to that one
96 if self
.currentlevel
.leveltop
is not None:
97 # make sure there is no obstacle
98 lastrow
= len (self
.currentlevel
.leveltop
.background
) - 1
99 if self
.check_background_obstacle (self
.currentlevel
.leveltop
, lastrow
, self
.butaba
.col
) is False:
100 self
.currentlevel
= self
.currentlevel
.leveltop
101 self
.butaba
.row
= lastrow
102 # normal upward movement
103 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
-1, self
.butaba
.col
) is False:
106 def move_butaba_down (self
):
107 # first if butaba is not facing forward, make him face forward/down
108 if self
.butaba
.position
<> butaba
.Butaba
.FRONT
:
109 self
.butaba
.position
= butaba
.Butaba
.FRONT
112 # if butaba is trying to move off the bottom of the screen
113 if self
.butaba
.row
>= len (self
.currentlevel
.background
)-1:
114 # if there is a level below set current level to that one
115 if self
.currentlevel
.levelbottom
is not None:
116 # make sure there is no obstacle at that position
117 if self
.check_background_obstacle (self
.currentlevel
.levelbottom
, 0, self
.butaba
.col
) is False:
118 self
.currentlevel
= self
.currentlevel
.levelbottom
120 # normal downward movement
121 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
+1, self
.butaba
.col
) is False:
124 # check if a background tile is an obstacle
125 def check_background_obstacle (self
, level
, row
, col
):
126 if (level
.background
[row
][col
][2] == 1):
131 def move_butaba_left (self
):
132 # first if Butaba is not facing left, make him face left
133 if self
.butaba
.position
<> butaba
.Butaba
.LEFT
:
134 self
.butaba
.position
= butaba
.Butaba
.LEFT
137 # if butaba is trying to move off the left edge
138 if self
.butaba
.col
<= 0:
139 # if there is a level to the right set current level to that one
140 if self
.currentlevel
.levelleft
is not None:
141 # make sure there is no obstacle at that position of movement
142 # get the last column of the previous level
143 lastcol
= len (self
.currentlevel
.levelleft
.background
[0]) - 1
144 if self
.check_background_obstacle (self
.currentlevel
.levelleft
, self
.butaba
.row
, lastcol
) is False:
145 self
.currentlevel
= self
.currentlevel
.levelleft
146 self
.butaba
.col
= lastcol
147 # normal left movement
148 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
-1) is False:
151 def move_butaba_right (self
):
152 # First if Butaba is not facing right make him face right
153 if self
.butaba
.position
<> butaba
.Butaba
.RIGHT
:
154 self
.butaba
.position
= butaba
.Butaba
.RIGHT
157 # if butaba is trying to move off the right edge
158 if self
.butaba
.col
>= len (self
.currentlevel
.background
[0])-1:
159 # if there is a level to the right swap current level with that one
160 if self
.currentlevel
.levelright
is not None:
161 # make sure there is no obstacle at that position of movement
162 # get the last column of the previous level
163 if self
.check_background_obstacle (self
.currentlevel
.levelright
, self
.butaba
.row
, 0) is False:
164 self
.currentlevel
= self
.currentlevel
.levelright
166 # normal right movement
167 elif self
.check_background_obstacle (self
.currentlevel
, self
.butaba
.row
, self
.butaba
.col
+ 1) is False:
170 def draw_butaba (self
):
171 if self
.butaba
.position
== butaba
.Butaba
.FRONT
:
172 self
.screen
.blit (self
.butabafront
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
173 elif self
.butaba
.position
== butaba
.Butaba
.BACK
:
174 self
.screen
.blit (self
.butababack
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
175 elif self
.butaba
.position
== butaba
.Butaba
.LEFT
:
176 self
.screen
.blit (self
.butabaleft
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
177 elif self
.butaba
.position
== butaba
.Butaba
.RIGHT
:
178 self
.screen
.blit (self
.butabaright
, (self
.butaba
.col
*48, self
.butaba
.row
*48))
180 # Draw the sidebar infodisplay
181 def draw_information (self
):
182 self
.screen
.blit (self
.redpotion
, (485, 10))
183 self
.put_text (550, 25, 28, (255, 0, 0), "%d" % self
.butaba
.health
)
184 self
.screen
.blit (self
.goldcoins
, (485, 50))
185 self
.put_text (550, 75, 28, (255, 255, 0), "%d" % self
.butaba
.gold
)
186 self
.screen
.blit (self
.wand
, (485, 120))
187 self
.put_text (550, 130, 28, (0, 0, 255), "%d" % self
.butaba
.magic
)
188 self
.screen
.blit (self
.bulb
, (485, 180))
189 self
.put_text (550, 190, 28, (0, 255, 0), "%d" % self
.butaba
.experience
)
190 self
.screen
.blit (self
.lightning
, (485, 240))
191 self
.put_text (550, 250, 28, (255,255,255), "%d" % self
.butaba
.strength
)
193 # Draw the level background tiles
194 def draw_level (self
, level
):
196 for row
in level
.background
:
198 for tilerow
, tilecol
, is_solid
in row
:
201 self
.screen
.blit (self
.tileset
, (j
*48, i
*48), pygame
.Rect (tilex
, tiley
, 48, 48))