forked from sd16spring/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented direcitonal continuity, comments/organization #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Halliax
wants to merge
1
commit into
release
Choose a base branch
from
master
base: release
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,27 +207,60 @@ 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. This is a really cool added feature. |
||
| #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] | ||
|
|
||
| self.model.char.image = self.model.char.walking_frames_d[1] | ||
| 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) | ||
| self.view.build_surface(self.model.room_map) | ||
| self.view.background = 'currentroom.png' | ||
| self.model.char.walls = walls | ||
|
|
||
|
|
||
| class Wall(object): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice reorganization here - well-thought out. |
||
| """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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job here both reorganizing and adding some good documentation. 👍 👠 🐍