So, I have this chunk of code, a part of a tutorial on making a dungeoncrawl roguelike that I've modified to have an "open" environment.
This bit makes square rooms, hollow on the inside, and I'm trying to isolate "room.x1" "room.x2" "room.y1" and "room.y2" in order to create a "door" opening in each of the four cardinal directions.
def create_room(room):
global map
#go through the tiles in the rectangle and make them impassable
for x in range(room.x1 , room.x2 + 1):
for y in range(room.y1 , room.y2 + 1):
map[x][y].blocked = True
map[x][y].block_sight = True
#Now make the interior passable
for x in range(room.x1 + 1, room.x2):
for y in range(room.y1+1, room.y2):
map[x][y].blocked = False
map[x][y].block_sight = False
Here's the Rectangle class used to build the rooms.
class Rect:
#a rectangle on the map. used to characterize a room.
def __init__(self, x, y, w, h):
self.x1 = x
self.y1 = y
self.x2 = x + w
self.y2 = y + h
def center(self):
center_x = (self.x1 + self.x2) / 2
center_y = (self.y1 + self.y2) / 2
return (center_x, center_y)
def wall(self):
left_wall = self.x1
right_wall = self.x2
top_wall = self.y1
bottom_wall = self.y2
return(left_wall, right_wall, top_wall, bottom_wall)
def intersect(self, other):
#returns true if this rectangle intersects with another one
return (self.x1 <= other.x2 and self.x2 >= other.x1 and
self.y1 <= other.y2 and self.y2 >= other.y1)
As you can see, I've tried defining each wall seperately, but I can't figure out if it's right or how to pull those variables out.
EDIT: Ahah, got it. I put a self.xc and self.yc to give me the centerpoint as a variable in the class, and I took the self.y1 as a co-ordinate to place the door in the exact center of the room!
Now that I've got it figured out, I can make sure there's always an exit north of my spawn room, and that every other room has at least one exit. debugging for blocked walls will come later.