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):
self.anim = 'boulder_up'
tile_sharer = self.share_tile(block.ArrowBlock)
if tile_sharer:
self.direction = tile_sharer.direction
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:
self.reset_pos()

View File

@ -24,10 +24,12 @@
A generic level.
"""
import pygame
import player
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.tiles = []
@ -42,6 +44,11 @@ class Level(object):
self.player = player.Player(self, 0, 0)
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):
pass
@ -58,8 +65,7 @@ class Level(object):
self.camera_y = (self.player.y - screen_size[1] / 2 - 24)
def draw(self, window):
for tile in self.tiles:
tile.draw(window)
window.blit(self.background, (0 - self.camera_x, -48 - self.camera_y))
for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)):
obj.draw(window)

View File

@ -40,7 +40,8 @@ import logic.rollingstone
class Level1(level.Level):
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
@ -49,6 +50,8 @@ class Level1(level.Level):
self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
self.draw_background()
# for i in range(10):
# self.objects.append(block.Block(self, random.randint(0, 10)*64,
# random.randint(0, 10)*48,
@ -59,24 +62,11 @@ class Level1(level.Level):
self.objects.append(b)
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, nsteps, directions = (
logic.rollingstone.generate_simple_unsolved_solvable_extra(10, 10, 7, 20))
arrowblocks = []
for i in range(10):
for j in range(10):
if (i, j) in playfield:
@ -88,17 +78,21 @@ class Level1(level.Level):
self.imgs['block1'],
movable=True))
else:
arrowblocks.append(
(0, -1)
if playfield[(i, j)] is logic.rollingstone.Up
else (0, 1)
if playfield[(i, j)] is logic.rollingstone.Down
else (1, 0)
if playfield[(i, j)] is logic.rollingstone.Right
else (-1, 0))
arrow_blocks = []
n = 0
for i in directions:
arrow_blocks.append(block.ArrowBlock(self,
playfield_pos[0] - 64,
playfield_pos[1] + 48 * n,
i.next_pos((0, 0))))
n += 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))
print(arrowblocks)
def load(self):
"""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
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 = {}, []
for pos, val in playfield.items():
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(
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)
def print_playfield(playfield, width, height, hide_directions):