Cleaned up some of the level-start code.

This commit is contained in:
Sakse Dalum 2012-08-08 23:00:39 +02:00
parent 43b3cd791c
commit 003f79d272
4 changed files with 23 additions and 22 deletions

View File

@ -66,11 +66,9 @@ class Game(object):
self.jukebox.play()
if not self.initial_goto_level:
self.objs.append(main_menu.MainMenu(self, graphics_dir))
self.menu = main_menu.MainMenu(self, graphics_dir)
else:
exec 'from level%d import Level%d as level' % (self.initial_goto_level,
self.initial_goto_level)
self.goto_level(level(self, graphics_dir))
self.goto_level(self.initial_goto_level)
def run(self):
t = pygame.time.get_ticks()
@ -84,7 +82,13 @@ class Game(object):
def goto_level(self, level):
self.level = level
graphics_dir = os.path.join(self.directory, "resources", "graphics")
self.menu = game_menu.GameMenu(self, graphics_dir)
exec 'from level%d import Level%d as level' % (level, level)
self.level = level(self, graphics_dir)
self.objs.append(self.level)
@ -109,12 +113,17 @@ class Game(object):
if hasattr(obj, 'update'):
obj.update(e, t, dt)
self.menu.update(e, t, dt)
def draw(self):
"""
Draw all game objects.
"""
self.window.fill((0, 0, 0))
for obj in self.objs:
if hasattr(obj, 'draw'):
obj.draw(self.window)
self.menu.draw(self.window)
pygame.display.flip()

View File

@ -53,12 +53,6 @@ class Level1(level.Level):
self.draw_background()
# for i in range(10):
# self.objects.append(block.Block(self, random.randint(0, 10)*64,
# random.randint(0, 10)*48,
# self.imgs['block1'],
# movable=True))
self.player.set_pos(64 * 10, 48 * 10)
self.player.set_init_pos()
@ -126,8 +120,10 @@ class Level1(level.Level):
for i in range(8)]
for i in range(8):
r = range(8)
r.remove(i)
self.objects.append(risingblocks[i])
n = random.randint(0, 7)
n = random.choice(r)
self.objects.append(lever.Lever(self,
task4_pos[0] + 64 * i,
task4_pos[1] - 48 * 2,
@ -141,7 +137,7 @@ class Level1(level.Level):
self.objects[-1].set_init_pos()
risingblocks[i].set_init_pos()
risingblocks[n].set_init_pos()
n = random.randint(0, 7)
n = random.choice(r)
self.objects.append(lever.Lever(self,
task4_pos[0] + 64 * i,
task4_pos[1],

View File

@ -56,7 +56,7 @@ class ActionBlock(block.Block):
if (self.x, self.y) != self._last_pos and not self.is_moving:
self.action(self)
self._last_pos = self.x, self.y
worldobject.WorldObject.update(self, e, t, dt)
class ColorWell(block.Block):
@ -70,7 +70,6 @@ class ColorWell(block.Block):
def set_colour(self, r, g, b):
self.well_colour = r * 255, g * 255, b * 255
self.img.fill(self.well_colour)
class Level3(level.Level):
def __init__(self, game, graphics_dir, paused=False):
@ -97,7 +96,7 @@ class Level3(level.Level):
blocks = [ActionBlock(self, 64 * i, 0, action=None, movable=True)
for i in range(1, 4)]
self.objects.extend(blocks)
wells = [ColorWell(self, 64 * 5, 0)]
self.objects.extend(wells)
@ -114,10 +113,10 @@ class Level3(level.Level):
well_colours = logic.colourboxes.get_colours(cur_boxes)
for i in range(len(wells)):
wells[i].set_colour(*well_colours[i])
for b in blocks:
b.action = update_wells
self.draw_background()

View File

@ -59,10 +59,7 @@ class MainMenu(object):
for event in e:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.game.goto_level(level1.Level1(self.game, self.img_dir))
self.game.objs.remove(self)
self.game.objs.append(game_menu.GameMenu(self.game,
self.img_dir))
self.game.goto_level(1)
if event.key == pygame.K_ESCAPE:
self.game.stop()