Optimisation!

This commit is contained in:
Sakse Dalum 2012-08-08 19:53:48 +02:00
parent a4df741866
commit 5c1af793df
4 changed files with 33 additions and 33 deletions

View File

@ -62,11 +62,11 @@ class Boulder(worldobject.WorldObject):
if self.direction == (0, -1): if self.direction == (0, -1):
self.anim = 'boulder_up' self.anim = 'boulder_up'
tile_sharer = self.share_tile(block.ArrowBlock)
if tile_sharer:
self.direction = tile_sharer.direction
if self.rolling: if self.rolling:
if not self.is_moving:
tile_sharer = self.share_tile(block.ArrowBlock)
if tile_sharer:
self.direction = tile_sharer.direction
if not self.move(*self.direction) and not self.is_moving: if not self.move(*self.direction) and not self.is_moving:
self.reset_pos() self.reset_pos()

View File

@ -24,10 +24,12 @@
A generic level. A generic level.
""" """
import pygame
import player import player
class Level(object): class Level(object):
def __init__(self, game, graphics_dir, paused=False): def __init__(self, game, graphics_dir, size=(0, 0), paused=False):
self.__dict__.update(locals()) self.__dict__.update(locals())
self.tiles = [] self.tiles = []
@ -42,6 +44,11 @@ class Level(object):
self.player = player.Player(self, 0, 0) self.player = player.Player(self, 0, 0)
self.objects.append(self.player) self.objects.append(self.player)
def draw_background(self):
self.background = pygame.Surface(self.size)
for tile in self.tiles:
tile.draw(self.background)
def restart(self): def restart(self):
pass pass
@ -58,8 +65,7 @@ class Level(object):
self.camera_y = (self.player.y - screen_size[1] / 2 - 24) self.camera_y = (self.player.y - screen_size[1] / 2 - 24)
def draw(self, window): def draw(self, window):
for tile in self.tiles: window.blit(self.background, (0 - self.camera_x, -48 - self.camera_y))
tile.draw(window)
for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)): for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)):
obj.draw(window) obj.draw(window)

View File

@ -40,7 +40,8 @@ import logic.rollingstone
class Level1(level.Level): class Level1(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, paused) level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20),
paused=paused)
self.dimensions = 50, 50 self.dimensions = 50, 50
@ -49,6 +50,8 @@ class Level1(level.Level):
self.tiles.append( self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1'])) tile.Tile(self, i*64, j*48, self.imgs['ground1']))
self.draw_background()
# for i in range(10): # for i in range(10):
# self.objects.append(block.Block(self, random.randint(0, 10)*64, # self.objects.append(block.Block(self, random.randint(0, 10)*64,
# random.randint(0, 10)*48, # random.randint(0, 10)*48,
@ -59,24 +62,11 @@ class Level1(level.Level):
self.objects.append(b) self.objects.append(b)
self.objects.append(lever.Lever(self, 64*2, 0, [b.activate])) self.objects.append(lever.Lever(self, 64*2, 0, [b.activate]))
arrow_blocks = [block.ArrowBlock(self, 0, 48, (1, 0)),
block.ArrowBlock(self, 0, 48*2, (-1, 0)),
block.ArrowBlock(self, 0, 48*3, (0, 1)),
block.ArrowBlock(self, 0, 48*4, (0, -1))]
self.objects.extend(arrow_blocks)
self.objects.append(lever.Lever(self, 64*3, 0,
[arrow_block.activate
for arrow_block in arrow_blocks],
toggling=True))
playfield_pos = (64*2, 48*2) playfield_pos = (64*2, 48*2)
playfield, nsteps, directions = ( playfield, nsteps, directions = (
logic.rollingstone.generate_simple_unsolved_solvable_extra(10, 10, 7, 20)) logic.rollingstone.generate_simple_unsolved_solvable_extra(10, 10, 7, 20))
arrowblocks = []
for i in range(10): for i in range(10):
for j in range(10): for j in range(10):
if (i, j) in playfield: if (i, j) in playfield:
@ -88,17 +78,21 @@ class Level1(level.Level):
self.imgs['block1'], self.imgs['block1'],
movable=True)) movable=True))
else: arrow_blocks = []
arrowblocks.append( n = 0
(0, -1) for i in directions:
if playfield[(i, j)] is logic.rollingstone.Up arrow_blocks.append(block.ArrowBlock(self,
else (0, 1) playfield_pos[0] - 64,
if playfield[(i, j)] is logic.rollingstone.Down playfield_pos[1] + 48 * n,
else (1, 0) i.next_pos((0, 0))))
if playfield[(i, j)] is logic.rollingstone.Right n += 1
else (-1, 0))
self.objects.extend(arrow_blocks)
self.objects.append(lever.Lever(self, 64*3, 0,
[arrow_block.activate
for arrow_block in arrow_blocks],
toggling=True))
print(arrowblocks)
def load(self): def load(self):
"""Load all resources used in the level.""" """Load all resources used in the level."""

View File

@ -163,7 +163,7 @@ def generate_simple_unsolved_solvable_playfield(width, height, nturns, nstones):
Return a tuple of a playfield without direction objects, its number of Return a tuple of a playfield without direction objects, its number of
steps, and a list of the direction objects. steps, and a list of the direction objects.
""" """
playfield, steps = generate_simple_playfield(width, height, nturns, stones) playfield, steps = generate_simple_playfield(width, height, nturns, nstones)
new_playfield, directions = {}, [] new_playfield, directions = {}, []
for pos, val in playfield.items(): for pos, val in playfield.items():
if val is Blocker: if val is Blocker:
@ -180,7 +180,7 @@ def generate_simple_unsolved_solvable_extra(width, height, nturns, nstones):
""" """
playfield, steps, directions = generate_simple_unsolved_solvable_playfield( playfield, steps, directions = generate_simple_unsolved_solvable_playfield(
width, height, nturns, nstones) width, height, nturns, nstones)
missing_dir = list(all_directions - set(directions))[0] missing_dir = list(set(all_directions) - set(directions))[0]
return playfield, steps, directions + [missing_dir] * (len(directions) / 3) return playfield, steps, directions + [missing_dir] * (len(directions) / 3)
def print_playfield(playfield, width, height, hide_directions): def print_playfield(playfield, width, height, hide_directions):