changeset 9:e90e639d45f9

doors generated by the generator
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 21 Aug 2011 19:35:17 +0200
parents e1209ce6e267
children 56d161aebf0b
files generator.py hero.py level.py thing.py
diffstat 4 files changed, 29 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/generator.py	Sun Aug 21 19:13:26 2011 +0200
+++ b/generator.py	Sun Aug 21 19:35:17 2011 +0200
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 
 import random
+import thing
 
 
 class SimpleLevelGenerator(object):
@@ -37,6 +38,7 @@
 class LevelGenerator(object):
     def __init__(self):
         self.map = [['x'] * 30 for i in range(30)]
+        self.things = []
         self.rooms = [[self.make_room(x, y) for x in range(3)]
                       for y in range(3)]
         for line in self.rooms:
@@ -47,6 +49,10 @@
             for i, c in enumerate(line):
                 if c == 'X':
                     line[i] = 'x'
+        self.make_stairs()
+
+    def make_stairs(self):
+        pass
 
     def make_connections(self):
         connected = [(random.randint(0, 2), random.randint(0, 2))]
@@ -67,8 +73,8 @@
             connected.append((x2, y2))
 
     def make_room(self, x1, y1):
-        w = random.randint(4, 8)
-        h = random.randint(4, 8)
+        w = random.randint(3, 8)
+        h = random.randint(3, 8)
         x = 10 * x1 + random.randint(0, 8 - w)
         y = 10 * y1 + random.randint(0, 8 - h)
         return (x, y, x + w, y + h)
@@ -76,6 +82,7 @@
     def connect_rooms(self, r1, r2):
         if abs(r1[0] - r2[0]) < abs(r1[1] - r2[1]):
             # vertical
+            v = True
             if r1[1] > r2[1]:
                 r1, r2 = r2, r1
             x1 = random.randint(r1[0] + 1, r1[2] - 1)
@@ -95,6 +102,7 @@
                 self.map[y][x2] = ','
         else:
             # horizontal
+            v = False
             if r1[0] > r2[0]:
                 r1, r2 = r2, r1
             x1 = r1[2]
@@ -116,6 +124,10 @@
                 self.map[y2][x] = ','
         self.map[y1][x1] = '+'
         self.map[y2][x2] = '+'
+        if random.randint(0, 10) > 7:
+            self.things.append(thing.Door(x1, y1, not v))
+        if random.randint(0, 10) > 7:
+            self.things.append(thing.Door(x2, y2, not v))
 
     def rect(self, x1, y1, x2, y2, c, fill=None):
         if fill:
--- a/hero.py	Sun Aug 21 19:13:26 2011 +0200
+++ b/hero.py	Sun Aug 21 19:35:17 2011 +0200
@@ -56,6 +56,9 @@
         self.sprite.animation = self.sprite.anim_drop()
         self.sprite.animation.next()
 
+    def render(self, to_display):
+        to_display.sprites.add(self.sprite)
+
     def walk(self, level, dx, dy):
         if level.is_blocked(self.x + dx, self.y + dy):
             self.sprite.animation = self.sprite.anim_blocked(dx, dy)
--- a/level.py	Sun Aug 21 19:13:26 2011 +0200
+++ b/level.py	Sun Aug 21 19:35:17 2011 +0200
@@ -3,7 +3,6 @@
 
 import display
 import hero
-import thing
 import generator
 import pygame
 
@@ -30,12 +29,6 @@
 class DoorTile(Tile):
     tx, ty = 3, 3
 
-    def render(self, to_display):
-        super(DoorTile, self).render(to_display)
-        v = self.y-1 > 0 and self.map[self.y-1][self.x] == 'x'
-        door = thing.Door(self.x, self.y, vertical=v)
-        to_display.sprites.add(door.sprite)
-
 
 class WallTile(Tile):
     filename = 'wall.png'
@@ -81,6 +74,9 @@
     def __init__(self):
         gen = generator.LevelGenerator()
         self.map = gen.map
+        self.things = {}
+        for t in gen.things:
+            self.things[(t.x, t.y)] = t
         self.hero = None
 
     def is_blocked(self, x, y):
@@ -108,4 +104,6 @@
                 self.flags[-1].append(0)
                 if tile_class is None or tile.blocks:
                     self.flags[-1][-1] |= self.BLOCKED
-        display.sprites.add(self.hero.sprite)
+        self.hero.render(display)
+        for thing in self.things.itervalues():
+            thing.render(display)
--- a/thing.py	Sun Aug 21 19:13:26 2011 +0200
+++ b/thing.py	Sun Aug 21 19:35:17 2011 +0200
@@ -7,8 +7,12 @@
     def __init__(self, x, y, vertical):
         self.x = x
         self.y = y
-        if vertical:
+        self.vertical = vertical
+
+    def render(self, to_display):
+        if self.vertical:
             tx = 1
         else:
             tx = 0
-        self.sprite = display.Sprite(x, y, 'objects.png', tx, 0)
+        self.sprite = display.Sprite(self.x, self.y, 'objects.png', tx, 0)
+        to_display.sprites.add(self.sprite)