changeset 3:b58123926938

Scrolled map
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 11 Mar 2013 09:44:59 +0100
parents 405e7d25f9f4
children 56b6fab47935
files fhrl/__main__.py
diffstat 1 files changed, 80 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/fhrl/__main__.py	Sun Mar 10 23:26:48 2013 +0100
+++ b/fhrl/__main__.py	Mon Mar 11 09:44:59 2013 +0100
@@ -39,25 +39,51 @@
         self.grid = [
             [0] * width for y in xrange(height)
         ]
-        for i in xrange(100):
+        for i in xrange(200):
             x = random.randrange(1, self.width)
             y = random.randrange(1, self.height)
             self.grid[y][x] = 1
-        for y in xrange(self.height):
-            self.grid[y][1] = 0
-            self.grid[y][-2] = 0
-        for x in xrange(self.width):
-            self.grid[1][x] = 0
-            self.grid[-2][x] = 0
-            self.grid[0][x] = 1
-            self.grid[-1][x] = 1
-        for y in xrange(self.height):
-            self.grid[y][0] = 1
-            self.grid[y][-1] = 1
+        for i in xrange(50, 200):
+            x = random.randrange(2, self.width - 3)
+            y = random.randrange(2, self.height - 3)
+            w = random.randrange(3, 10)
+            h = random.randrange(3, 10)
+            w = max(1, min(self.width - x - 2, w))
+            h = max(1, min(self.height - y - 2, h))
+            self.rect(x, y, w, h, 1)
+            self.rect(x-1, y-1, w+2, h+2, 0)
+            self.rect(x+1, y+1, w-2, h-2, 0)
+            if w > 2 and h > 2:
+                if random.choice((0, 1)):
+                    dx = x + random.randrange(w - 2) + 1
+                    if random.choice((0, 1)):
+                        self.grid[y][dx] = 0
+                    else:
+                        self.grid[y + h - 1][dx] = 0
+                else:
+                    dy = y + random.randrange(h - 2) + 1
+                    if random.choice((0, 1)):
+                        self.grid[dy][x] = 0
+                    else:
+                        self.grid[dy][x + w - 1] = 0
+        self.rect(0, 0, self.width, self.height, 1)
 
     def __iter__(self):
         return iter(self.grid)
 
+    def rect(self, sx, sy, w, h, v):
+        sx = max(0, min(self.width, sx))
+        sy = max(0, min(self.height, sy))
+        w = max(1, min(self.width - sx, w))
+        h = max(1, min(self.height - sy, h))
+        for y in xrange(sy, sy + h):
+            self.grid[y][sx] = v
+            self.grid[y][sx + w - 1] = v
+        for x in xrange(sx, sx + w):
+            self.grid[sy][x] = v
+            self.grid[sy + h - 1][x] = v
+        return sx, sy, w, h
+
     def render(self, surf, tiles):
         nrows = iter(self.grid)
         nrows.next()
@@ -82,7 +108,7 @@
         y = self.y + DY[d]
         if level.grid[y][x]:
             return False
-        self.rect.topleft = x * 48 - 24, y * 32 - 32
+        self.rect.move_ip(DX[d] * 48, DY[d] * 32)
         self.x, self.y = x, y
         return True
 
@@ -112,13 +138,20 @@
 
 class Game(object):
     def __init__(self):
-        self.screen = pygame.display.set_mode((480, 480))
+        self.width = 480
+        self.height = 480
+        self.margin = 64
+        self.x = 0
+        self.y = 0
+        self.screen = pygame.display.set_mode((self.width, self.height))
         self.clock = pygame.time.Clock()
         self.grass = load_atlas('grass.png', 48, 32)
         self.mobs = load_atlas('mobs.png', 48, 48)
-        self.level = Level((11, 16))
+        self.level = Level((32, 32))
         self.sprites = SortedUpdates()
-        self.background = pygame.Surface((480, 480))
+        self.background = pygame.Surface(
+            (self.level.width * 48, self.level.height * 32)
+        )
 
         self.level.render(self.background, self.grass)
         self.level.hero = Hero(self.mobs[0], (1, 1))
@@ -129,18 +162,46 @@
         for m in self.level.monsters:
             self.sprites.add(m)
 
-    def run(self):
-        self.screen.blit(self.background, (0, 0))
+    def scroll_by(self, dx, dy):
+        if self.x - dx > 0:
+            dx = self.x
+        if self.y - dy > 0:
+            dy = self.y
+        for s in self.sprites:
+            s.rect.move_ip(-dx, -dy)
+        self.x -= dx
+        self.y -= dy
+        self.screen.fill((0, 0, 0))
+        self.screen.blit(self.background, (self.x, self.y))
         self.sprites.draw(self.screen)
         pygame.display.flip()
+
+    def _sprite_clear(self, screen, rect):
+        source = rect.move(-self.x, -self.y)
+        if not self.background.get_rect().contains(source):
+            screen.fill((0, 0, 0), rect)
+        screen.blit(self.background, rect, source)
+
+
+    def run(self):
+        self.scroll_by(0, 0)
         while True:
             while not self.level.hero.act(self.level):
-                self.sprites.clear(self.screen, self.background)
+                self.sprites.clear(self.screen, self._sprite_clear)
                 dirty = self.sprites.draw(self.screen)
                 pygame.display.update(dirty)
                 self.clock.tick(12)
             for m in self.level.monsters:
                 m.act(self.level)
+            if self.level.hero.rect.right > self.width - self.margin:
+                self.scroll_by(48, 0)
+            if self.level.hero.rect.bottom > self.height - self.margin:
+                self.scroll_by(0, 32)
+            if self.level.hero.rect.left < self.margin:
+                self.scroll_by(-48, 0)
+            if self.level.hero.rect.top < self.margin:
+                self.scroll_by(0, -32)
+
 
 
 Game().run()