From 415d664632c3426f1fbe1113bc856ede9b6faf53 Mon Sep 17 00:00:00 2001 From: Halliax Date: Tue, 19 Apr 2016 15:13:59 -0400 Subject: [PATCH] Implemented direcitonal continuity, added a bunch of comments and organized some stuff better. Collision bugs maybe sort of fixed? --- interactive.py | 126 +++++++++++++++++++++++++++++++------------------ level.py | 22 +++++---- player.py | 2 +- 3 files changed, 95 insertions(+), 55 deletions(-) diff --git a/interactive.py b/interactive.py index 5310fc9..980d109 100644 --- a/interactive.py +++ b/interactive.py @@ -7,7 +7,14 @@ from player import CharacterSprite from level import Level, WallSprite, DoorSprite, FloorSprite -"""globals: walls, exit_blocks""" +""" +This is the main function for the game; run to initialize and play. +Try to escape the dream. +Arrow keys to move, 9 to wake up. + +classes: GameView, GameModel, GameController, Wall, ExitBlock, Connection +globals: walls, exit_blocks +""" class GameView(object): @@ -17,60 +24,75 @@ class GameView(object): def __init__(self, model, size): self.model = model self.screen = pygame.display.set_mode(size) - pygame.mixer.music.load('sounds/TheDarkLake.mp3') # this music plays + + # this music plays + pygame.mixer.music.load('sounds/TheDarkLake.mp3') pygame.mixer.music.play(-1) + + # starting room image self.background = 'images/room2.png' def draw(self, filename): """ Draw the game to the pygame window""" + # draws the background background = pygame.image.load(filename).convert() - # print filename scaled_bg = pygame.transform.scale(background, size) self.screen.blit(scaled_bg, (0,0)) + + # draws the character model char = self.model.char.image self.screen.blit(char, (self.model.char.x,self.model.char.y)) + # update pygame.display.flip() def build_surface(self, room_map): - """Builds the random rooms image for our procdurally generated rooms""" + """Builds the random rooms image for our procedurally generated rooms""" + + # initialize room surface and sprites to use for the specific room surf = pygame.Surface(size) - x = 0 - y = 0 wallsprite = WallSprite() wall = wallsprite.image doorsprite = DoorSprite() door = doorsprite.image floorsprite = FloorSprite() floor = floorsprite.image + + # build room image based on character map + x = 0 + y = 0 for row in room_map: for col in row: if col == "W": surf.blit(wall,(x, y)) - if col == "C": + if col in ["T","B","L","R"]: surf.blit(door,(x, y)) if col == ".": surf.blit(floor,(x,y)) x += 32 y += 32 x = 0 + + # save to file pygame.image.save(surf, 'currentroom.png') class GameModel(object): - """This is the model for the game + """This is the model for the game. It contains most of the functional bits + of the level. attributes: height, width, char, start_map, room_map""" def __init__(self, width, height): self.height = height self.width = width + # character map of the starting room self.start_map = ["....................", "....................", "....................", "....................", "....................", - "......WCWW..........", + "......WTWW..........", "....WWW..WWWWW......", "....W........W......", "....W........WWW....", @@ -86,22 +108,28 @@ def __init__(self, width, height): self.char = CharacterSprite(384,224,walls) - # Parse the level string above. W = wall, X = exit, C = connection + def generate_room(self, room): - """feed it a list of strings and it populates the globals with rects""" + """Parses the level string above and populates the globals with rects + W = wall, X = exit, C = connection""" + + # clears the global rect lists del walls[:] # List to hold the walls del exit_blocks[:] # List to hold the exits del connections[:] # List to hold all connections self.room_map = room - x = y = 0 + + # build global rect lists based on character map + x = 0 + y = 0 for row in self.room_map: for col in row: if col == "W": Wall((x, y)) if col == "X": ExitBlock((x, y)) - if col == "C": - Connection((x,y)) + if col in ["T","B","L","R"]: + Connection((x, y), col) x += 32 y += 32 x = 0 @@ -112,39 +140,12 @@ def update(self, control=0): self.char.update(control) -class Wall(object): - """it walls - - attribute: rect""" - def __init__(self, pos): - walls.append(self) - self.rect = pygame.Rect(pos[0], pos[1], 32, 32) - - -class ExitBlock(object): - """It exits - - attribute: rect""" - def __init__(self, pos): - exit_blocks.append(self) - self.rect = pygame.Rect(pos[0], pos[1], 32, 32) - -class Connection(object): - """It exits - - attribute: rect""" - - def __init__(self, pos): - connections.append(self) - self.rect = pygame.Rect(pos[0], pos[1], 32, 32) - self.rect.inflate_ip(2,2) - - class GameController(object): """This is the controller for the game. It contains a counter for each direction that controls animation state. attributes: model, li, di, ri, ui, room_counter""" + def __init__(self, model, view): """ initializes the model and counters""" self.model = model @@ -157,6 +158,7 @@ def __init__(self, model, view): self.room_counter = 0 def reset_state(self): + """clean slate for initial room generation, all subsequent resets""" global timer self.waking += 1 if self.waking * random() > 2: @@ -183,6 +185,7 @@ def update(self): #This is the wake up function self.reset_state() + # move in directions if pressed[pygame.K_LEFT]: self.li = (self.li + 1) % 4 self.model.char.move_left(self.li) @@ -204,13 +207,15 @@ def update(self): if self.model.char.rect.colliderect(exit_block): print "You won't go out there." self.effect.play() - time.sleep(0.10) + time.sleep(0.50) self.reset_state() for connection in connections: if self.model.char.rect.colliderect(connection): - new_spaces = [(40, 200), (560,200), (240,10),(240, 380)] - new_space = choice(new_spaces) + #maps directions from which you exit the old room to positions in + #the new room, so directional continuity + new_spaces = {"R":(40, 200), "L":(560,200), "B":(240,10), "T":(240, 380)} + new_space = new_spaces[connection.side] self.model.char.x = new_space[0] self.model.char.y = new_space[1] @@ -218,6 +223,7 @@ def update(self): self.model.char.rect = self.model.char.image.get_rect().inflate(-4,-32) self.model.char.rect.move_ip(self.model.char.x,self.model.char.y + 16) + # increments counter to gradually decrease likelihood of doors generating self.room_counter += 1 self.model.room_map = level.random_gen(self.room_counter) self.model.generate_room(self.model.room_map) @@ -225,6 +231,36 @@ def update(self): self.view.background = 'currentroom.png' self.model.char.walls = walls + +class Wall(object): + """it walls + + attribute: rect""" + def __init__(self, pos): + walls.append(self) + self.rect = pygame.Rect(pos[0], pos[1], 32, 32) + + +class ExitBlock(object): + """It exits + + attribute: rect""" + def __init__(self, pos): + exit_blocks.append(self) + self.rect = pygame.Rect(pos[0], pos[1], 32, 32) + +class Connection(object): + """Links to a new room. Tracks which side of the room it is on. + + attribute: rect, side""" + + def __init__(self, pos, side): + connections.append(self) + self.side = side + self.rect = pygame.Rect(pos[0], pos[1], 32, 32) + self.rect.inflate_ip(2,2) + + if __name__ == '__main__': pygame.init() size = (640, 480) # useful diff --git a/level.py b/level.py index 834fc4a..61a140b 100644 --- a/level.py +++ b/level.py @@ -10,11 +10,11 @@ class Level(object): def __init__(self, x=0): self.x = x - def choose(self): + def choose(self,side): res = '' r = random.randint(0,100) if r < self.threshold: - res += 'C' + res += side else: res += 'W' return res @@ -30,31 +30,32 @@ def random_gen(self,count): if x < 1 or x > 18: twall+= 'W' else: - twall += self.choose() + twall += self.choose('T') + #generate bottom wall bwall = '' for x in range(20): if x < 1 or x > 18: bwall += 'W' else: - bwall += self.choose() + bwall += self.choose('B') + #generate middle walls middlewalls = [] for x in range(13): wall = '' if x < 2 or x > 10: - wall += self.choose() + wall += self.choose('L') wall += '..................' - wall += self.choose() + wall += self.choose('R') else: - wall += self.choose() + wall += self.choose('L') wall += '..' for y in range(14): wall += random.choice(['.','.','.','.','W']) wall += '..' - wall += self.choose() - # wall += random.choice(['W','W','W','W','W','W','C']) + wall += self.choose('R') middlewalls.append(wall) walls = [] @@ -77,6 +78,7 @@ def __init__(self): vert= [0, 32, 64, 96] self.image = sprite_sheet.get_image(random.choice(horiz),random.choice(vert),32,32) + class DoorSprite(pygame.sprite.Sprite): """This is the class for all of the door sprites @@ -90,6 +92,8 @@ def __init__(self): vert= [0] self.image = sprite_sheet.get_image(random.choice(horiz),random.choice(vert),32,32) + + class FloorSprite(pygame.sprite.Sprite): """This is the class for all of the floor sprites diff --git a/player.py b/player.py index 9d5426e..4c3e902 100644 --- a/player.py +++ b/player.py @@ -5,7 +5,7 @@ class CharacterSprite(pygame.sprite.Sprite): """Sprite representation of the character. attributes: x, y, walking_frames_l, walking_frames_r, walking_frames_u, - walking_frames_d, step_size, direction, image, rect""" + walking_frames_d, walls, step_size, direction, image, rect""" def __init__(self, x_coor, y_coor, walls): super(CharacterSprite, self).__init__()