Okay, I've started work on the map generation code. I had some time to think it over and talk through the logic a bit more, and I made some changes to the way it's going to work. Here's the code I have so far:
import random, pygame
class RoomObj:
def update(self,anchorpoint):
self.rect.left = self.xoffset + anchorpoint[0]
self.rect.top = self.yoffset + anchorpoint[1]
class Room(RoomObj):
def __init__(self, x,y, imageset):
#RoomObj.__init__(self)
self.direction = random.randint(1,4) #up, down, left, right
self.size = (random.randint(400,600),random.randint(500,700))
self.image = pygame.Surface(self.size)
pygame.transform.scale(random.choice(imageset), self.size, self.image)
self.rect = self.image.get_rect()
self.rect.topleft = [x,y]
self.xoffset = x
self.yoffset = y
class Hall(RoomObj):
def __init__(self, direction, room_rect):
#RoomObj.__init__(self)
if direction==1:
self.size = (80,random.randint(100,300))
self.rect.bottom = room_rect.top
self.rect.left = random.randint(room_rect.left, room_rect.right-80)
def genMap(size,roomimages,hallimages):
if size=='small':
numofrooms = random.randint(3,5)
if size=='medium':
numofrooms = random.randint(5,8)
rooms = []
map = []
startpoint = [0,0]
for i in range(numofrooms):
if i < len(numberofrooms)-1:
tmproom = Room(startpoint, roomimages)
#up
if tmproom.direction == 1:
map.append(tmproom)
map.append(Hall(tmproom.direction, tmproom.rect))
return map
Basically I have a parent class called RoomObj that has the update functions. You may be wondering why a stationary object needs an update function, but for this large of a map, i'm going to have it scroll as the player moves. If you're interested in how I've done the scrolling, check out this article. I'm applying forces to the player object, which move the player sprite around. When the player moves to the edges of an invisible radius around the center of the screen, these forces are transferred to other objects making them scroll. I do this by giving each object an anchor point as an argument. This anchor point is simply a coordinate that I transfer the forces to when the player is moving outside of their center radius. I'm sure I could explain that better, but like I said, if you really care, just check out the blog post. I have the source code up that should explain it better than I could.
Then I have two other classes that inherit from the RoomObj. I created a Room object and a Hall object. I still need to flesh them out a bit more in order to accurately place everything and check for collisions and some other general stuff. I guess I could've created a single class with overloaded init functions, but I've never really been a fan of overloaded classes. They just get kind of confusing after a while. Using inheritance, I can expand the base class or add other children classes fairly easily.
The genMap function is where I made the real changes to how everything is created. If you check out my last blog post, I was going to have it create all the rooms, then loop through the objects to place connecting hall objects. After a great deal of thought, I changed it to create a room, then create a hall, then create a room, and so on. I believe this will allow for much more complex maps. In the future I'd like to add a bit to allow for multiple Hallways to branch off, but for now, I just want to get this working.
Like I said, it's still a work in progress. I know I need to rework the room and hall objects a bit, and I still need to figure out how I want to handle the placement for them. As of right now, I'm thinking I'll use the room object to choose a random direction and then have the hallway get it's x/y coordinates from that. I'm thinking that I'll also need to add wall rectangles to each room object for player collisions later on.
I'm also thinking that the images for the floors will have to be different from those used for the walls. If I'm creating wall objects for each room, those will be scaled to fit the pseudo-random size. I'd like to get some sort of tile for the floors though. I think it'll just look nicer if the floors are all uniform for each map. In any case, I think this should do the trick if I ever finish up the Room and Hall classes. I just don't have time lately to really work on anything. I can dream though...
-Newt
No comments:
Post a Comment