-
-
Save 1328/947a4cc7f9dda2172648 to your computer and use it in GitHub Desktop.
adv
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
| # comments added, but not tested, so there are probably typos and other things | |
| # that need to be worked out | |
| import sqlite3 | |
| # consider using json for this sort of thing. sqlite3 is terrific for random | |
| # selects, but if you are going to be loading it all up anyway, no reason not to | |
| # have the easier editing of json | |
| quit_game = False | |
| char_id = 1 | |
| class Player: | |
| # consider passing char_id to init | |
| def __init__(self): | |
| self.get_char_info(char_id) | |
| # remember that you can create custom instantion methods by using | |
| # @classmethd | |
| # def char_from_db(cls, rec_id): ... | |
| def get_char_info(self, rec_id): | |
| """ | |
| Grab the character's info from the database. | |
| """ | |
| # check out the dictionary row factor discussion here: http://stackoverflow.com/questions/3300464/how-can-i-get-dict-from-sqlite-query | |
| # combine that with a custom instantiation method and having __init__ | |
| # take the attributes and your control flow might be better | |
| db_conn = sqlite3.connect("db/survivalist.sqlite") | |
| db_cursor = db_conn.cursor() | |
| db_query = ("SELECT name, loc_x, loc_y, loc_z, health " | |
| "FROM characters " | |
| "WHERE id = %s" % rec_id) | |
| db_cursor.execute(db_query) | |
| for db_row in db_cursor: | |
| self.name = db_row[0] | |
| # easier to have this as a list | |
| self.loc = [db_row[1], db_row[2], db_row[3]] | |
| self.health = db_row[4] | |
| db_conn.close() | |
| def get_char_loc(self, move_axis, move_value): | |
| """ | |
| Determine the character's new X, Y, Z location. | |
| """ | |
| # consider consolidating with move | |
| if move_axis == "x": | |
| self.loc_x += move_value | |
| elif move_axis == "y": | |
| self.loc_y += move_value | |
| elif move_axis == "z": | |
| self.loc_z += move_value | |
| def move(self, direction): | |
| ''' | |
| consider consolidating move from below with get_loc and using a | |
| dictionary to make this a bit less redundant | |
| ''' | |
| directions = { | |
| 'u':(0,0,-1), | |
| 'd':(0,0,1), | |
| 'n':(1,0,0), | |
| 's':(-1,0,0), | |
| 'e':(0,1,0), | |
| 'w':(0,-1,0), | |
| } | |
| if direction not in directions: | |
| raise Exception('wrong way') | |
| self.loc = [a+b for a,b in zip(self.loc, directions[direction]] | |
| @property | |
| def location(self): | |
| return '{},{},{}'.format(self.loc_x, | |
| self.loc_y, | |
| self.loc_z) | |
| class Room: | |
| # same comments on objection contrution as above | |
| def __init__(self): | |
| print self.get_room_info() | |
| def get_room_info(self): | |
| """ | |
| Queries the database for the name and description of the | |
| current room. | |
| """ | |
| db_conn = sqlite3.connect("db/survivalist.sqlite") | |
| db_cursor = db_conn.cursor() | |
| db_query = ("SELECT rw.id AS room_id, biomes.name AS biome_name, " | |
| " biomes.description AS biome_desc " | |
| " FROM rooms_wilderness rw " | |
| " INNER JOIN biomes ON rw.biome_id = biomes.id " | |
| " WHERE rw.loc_x = %s AND rw.loc_y = %s AND rw.loc_z = %s" | |
| ) % (my_char.loc_x, my_char.loc_y, my_char.loc_z) | |
| db_cursor.execute(db_query) | |
| for db_row in db_cursor: | |
| self.id = db_row[0] | |
| self.name = db_row[1] | |
| self.desc = db_row[2] | |
| return "[%s]\n%s" % (self.name, self.desc) | |
| db_conn.close() | |
| def get_room_survey(self): | |
| """ | |
| Queries the database to determine which resources are | |
| available in the current room. | |
| """ | |
| db_conn = sqlite3.connect("db/survivalist.sqlite") | |
| db_cursor = db_conn.cursor() | |
| db_query = ("SELECT GROUP_CONCAT(br.name, \", \") AS resource_name " | |
| " FROM biome_resources br " | |
| " INNER JOIN biomes ON br.biome_id = biomes.id " | |
| " WHERE biomes.id = %s" | |
| ) % this_room.id | |
| db_cursor.execute(db_query) | |
| for db_row in db_cursor: | |
| return "Resources found here: %s" % (db_row[0]) | |
| db_conn.close() | |
| def move_char(move_axis, move_value): | |
| """ | |
| Updates the character's coordinates and requests the | |
| room information for the new location. | |
| """ | |
| my_char.get_char_loc(move_axis, move_value) | |
| print this_room.get_room_info() | |
| my_char = Player() | |
| this_room = Room() | |
| while not quit_game: | |
| # consider moving char_loc to a property of Player | |
| char_loc = "{0},{1},{2}".format(str(my_char.loc_x), str(my_char.loc_y), str(my_char.loc_z)) | |
| prompt_string = "[{0}] > ".format(my_char.location) | |
| key_pressed = raw_input(prompt_string).lower() | |
| # consider a dictionary of aliases that make processing a bit easier: | |
| alias = {'north':'n', | |
| 'south':'s', | |
| } | |
| # swap aliased for words | |
| key_pressed = alias.get(key_pressed, key_pressed) | |
| # consider consolidating and moving move_char to the player class: | |
| if key_pressed in ('n', 's', 'e', 'w', 'u', 'd'): | |
| my_char.move(key_pressed) | |
| if key_pressed in ("n", "north"): | |
| move_char("x", 1) | |
| elif key_pressed in ("s", "south"): | |
| move_char("x", -1) | |
| elif key_pressed in ("e", "east"): | |
| move_char("y", 1) | |
| elif key_pressed in ("w", "west"): | |
| move_char("y", -1) | |
| elif key_pressed in ("u", "up"): | |
| move_char("z", 1) | |
| elif key_pressed in ("d", "down"): | |
| move_char("z", -1) | |
| elif key_pressed in ("sur", "survey"): | |
| print this_room.get_room_survey() | |
| elif key_pressed in ("q", "quit"): | |
| break | |
| else: | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment