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() self.jukebox.play()
if not self.initial_goto_level: if not self.initial_goto_level:
self.objs.append(main_menu.MainMenu(self, graphics_dir)) self.menu = main_menu.MainMenu(self, graphics_dir)
else: else:
exec 'from level%d import Level%d as level' % (self.initial_goto_level, self.goto_level(self.initial_goto_level)
self.initial_goto_level)
self.goto_level(level(self, graphics_dir))
def run(self): def run(self):
t = pygame.time.get_ticks() t = pygame.time.get_ticks()
@ -84,7 +82,13 @@ class Game(object):
def goto_level(self, level): 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) self.objs.append(self.level)
@ -109,12 +113,17 @@ class Game(object):
if hasattr(obj, 'update'): if hasattr(obj, 'update'):
obj.update(e, t, dt) obj.update(e, t, dt)
self.menu.update(e, t, dt)
def draw(self): def draw(self):
""" """
Draw all game objects. Draw all game objects.
""" """
self.window.fill((0, 0, 0)) self.window.fill((0, 0, 0))
for obj in self.objs: for obj in self.objs:
if hasattr(obj, 'draw'): if hasattr(obj, 'draw'):
obj.draw(self.window) obj.draw(self.window)
self.menu.draw(self.window)
pygame.display.flip() pygame.display.flip()

View File

@ -53,12 +53,6 @@ class Level1(level.Level):
self.draw_background() 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_pos(64 * 10, 48 * 10)
self.player.set_init_pos() self.player.set_init_pos()
@ -126,8 +120,10 @@ class Level1(level.Level):
for i in range(8)] for i in range(8)]
for i in range(8): for i in range(8):
r = range(8)
r.remove(i)
self.objects.append(risingblocks[i]) self.objects.append(risingblocks[i])
n = random.randint(0, 7) n = random.choice(r)
self.objects.append(lever.Lever(self, self.objects.append(lever.Lever(self,
task4_pos[0] + 64 * i, task4_pos[0] + 64 * i,
task4_pos[1] - 48 * 2, task4_pos[1] - 48 * 2,
@ -141,7 +137,7 @@ class Level1(level.Level):
self.objects[-1].set_init_pos() self.objects[-1].set_init_pos()
risingblocks[i].set_init_pos() risingblocks[i].set_init_pos()
risingblocks[n].set_init_pos() risingblocks[n].set_init_pos()
n = random.randint(0, 7) n = random.choice(r)
self.objects.append(lever.Lever(self, self.objects.append(lever.Lever(self,
task4_pos[0] + 64 * i, task4_pos[0] + 64 * i,
task4_pos[1], task4_pos[1],

View File

@ -71,7 +71,6 @@ class ColorWell(block.Block):
self.well_colour = r * 255, g * 255, b * 255 self.well_colour = r * 255, g * 255, b * 255
self.img.fill(self.well_colour) self.img.fill(self.well_colour)
class Level3(level.Level): class Level3(level.Level):
def __init__(self, game, graphics_dir, paused=False): def __init__(self, game, graphics_dir, paused=False):
level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20), level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20),

View File

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