Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. import os
  2.  
  3. import libtcodpy as libtcod
  4.  
  5. curbuild = '0.035'
  6.  
  7. #---Variables
  8. screen_w = 80
  9. screen_h = 50
  10. limit_fps = 20
  11. playerx = screen_w/2
  12. playery = screen_h/2
  13. ROOM_MAX_SIZE = 10
  14. ROOM_MIN_SIZE = 6
  15. MAX_ROOMS = 30
  16.  
  17. #Size of map
  18. map_w = 80
  19. map_h = 45
  20. #colors of unlit tiles
  21. color_dark_wall = libtcod.Color(0, 0, 100)
  22. color_dark_ground = libtcod.Color(50, 50, 150)
  23.  
  24.  
  25. #---Functions
  26. def handle_keys():
  27.  
  28. #Keys to quit and make fullscreen
  29. key = libtcod.console_wait_for_keypress(True)
  30. if key.vk == libtcod.KEY_ENTER and libtcod.KEY_ALT:
  31. #Alt + Enter to toggle fullscreen
  32. libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
  33. #ESC to exit game
  34. elif key.vk == libtcod.KEY_ESCAPE:
  35. return True
  36.  
  37. #Movement keys
  38. if libtcod.console_is_key_pressed(libtcod.KEY_UP):
  39. player.move(0, -1)
  40. elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
  41. player.move(0, 1)
  42. elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
  43. player.move(-1, 0)
  44. elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
  45. player.move(1, 0)
  46.  
  47. def create_room(room):
  48. global map
  49. #go through the tiles in the rectangle and make them passable
  50. for x in range(room.x1 + 1, room.x2):
  51. for y in range(room.y1 + 1, room.y2):
  52. map[x][y].blocked = False
  53. map[x][y].block_sight = False
  54.  
  55. def make_map():
  56. global map, player
  57.  
  58. #fill map with "blocked" tiles
  59. map = [[ Tile(True)
  60. for y in range(map_h) ]
  61. for x in range(map_w) ]
  62.  
  63. rooms = []
  64. num_rooms = 0
  65.  
  66. for r in range(MAX_ROOMS):
  67. #random width and height
  68. w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
  69. h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
  70. #random position without going out of the boundaries of the map
  71. x = libtcod.random_get_int(0, 0, map_w - w - 1)
  72. y = libtcod.random_get_int(0, 0, map_h - h - 1)
  73.  
  74. #"Rect" class makes rectangles easier to work with
  75. new_room = Rect(x, y, w, h)
  76.  
  77. #run through the other rooms and see if they intersect with this one
  78. failed = False
  79. for other_room in rooms:
  80. if new_room.intersect(other_room):
  81. failed = True
  82. break
  83.  
  84. if not failed:
  85. #this means there are no intersections, so this room is valid
  86.  
  87. #"paint" it to the map's tiles
  88. create_room(new_room)
  89.  
  90. #center coordinates of new room, will be useful later
  91. (new_x, new_y) = new_room.center()
  92.  
  93. if num_rooms == 0:
  94. #this is the first room, where the player starts at
  95. player.x = new_x
  96. player.y = new_y
  97. else:
  98. #all rooms after the first:
  99. #connect it to the previous room with a tunnel
  100.  
  101. #center coordinates of previous room
  102. (prev_x, prev_y) = rooms[num_rooms-1].center()
  103.  
  104. #draw a coin (random number that is either 0 or 1)
  105. if libtcod.random_get_int(0, 0, 1) == 1:
  106. #first move horizontally, then vertically
  107. create_h_tunnel(prev_x, new_x, prev_y)
  108. create_v_tunnel(prev_y, new_y, new_x)
  109. else:
  110. #first move vertically, then horizontally
  111. create_v_tunnel(prev_y, new_y, prev_x)
  112. create_h_tunnel(prev_x, new_x, new_y)
  113.  
  114. #finally, append the new room to the list
  115. rooms.append(new_room)
  116. num_rooms += 1
  117.  
  118. def render_all():
  119. global color_light_wall
  120. global color_light_ground
  121. #Draw all objects in the list
  122. for y in range(map_h):
  123. for x in range(map_w):
  124. wall = map[x][y].block_sight
  125. if wall:
  126. libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET )
  127. else:
  128. libtcod.console_set_back(con, x, y, color_dark_ground, libtcod.BKGND_SET )
  129. for object in objects:
  130. object.draw()
  131. libtcod.console_blit(con, 0, 0, screen_w, screen_h, 0, 0, 0)
  132.  
  133. def create_h_tunnel(x1, x2, y):
  134. global map
  135. for x in range(min(x1, x2), max(x1, x2) + 1):
  136. map[x][y].blocked = False
  137. map[x][y].block_sight = False
  138. def create_v_tunnel(y1, y2, x):
  139. global map
  140. #vertical tunnel
  141. for y in range(min(y1, y2), max(y1, y2) + 1):
  142. map[x][y].blocked = False
  143. map[x][y].block_sight = False
  144. #---Main Class
  145. class Object:
  146. #This is a generic object: the player, a monster, an item, the stairs...
  147. #it's always represented by a character on screen.
  148. def __init__(self, x, y, char, color):
  149. self.x = x
  150. self.y = y
  151. self.char = char
  152. self.color = color
  153.  
  154. def move(self, dx, dy):
  155. #Move by the given amount, if the destination is not blocked
  156. if not map[self.x + dx][self.y + dy].blocked:
  157. self.x += dx
  158. self.y += dy
  159.  
  160. def draw(self):
  161. #Set the color and then draw the character that represents this object at its position
  162. libtcod.console_set_foreground_color(con, self.color)
  163. libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
  164.  
  165. def clear(self):
  166. #Erase the character that represents this object
  167. libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
  168.  
  169. #---Tile Class
  170. class Tile:
  171. #A tile of the map and its properties
  172. def __init__(self, blocked, block_sight = None):
  173. self.blocked = blocked
  174.  
  175. #By default, if a tile is blocked, it also blocks sight
  176. if block_sight is None: block_sight = blocked
  177. self.block_sight = block_sight
  178.  
  179. class Rect:
  180. #A rectangle on the map. Used to characterize a room.
  181. def __init__(self, x, y, w, h):
  182. self.x1 = x
  183. self.y1 = y
  184. self.x2 = x + w
  185. self.y2 = y + h
  186. def center(self):
  187. center_x = (self.x1 + self.x2) / 2
  188. center_y = (self.y1 + self.y2) / 2
  189. return (center_x, center_y)
  190.  
  191. def intersect(self, other):
  192. #returns true if this rectangle intersects with another one
  193. return (self.x1 <= other.x2 and self.x2 >= other.x1 and self.y1 <= other.y2 and self.y2 >= other.y1)
  194.  
  195. #This sets up the fonts
  196. libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
  197.  
  198. #This sets up the window
  199. libtcod.console_init_root(screen_w, screen_h, 'Sida the Adventurer', False)
  200.  
  201. #This is a second console used for buffering certain things
  202. con = libtcod.console_new(screen_w, screen_h)
  203.  
  204. player = Object(screen_w/2, screen_h/2, '@', libtcod.white)
  205.  
  206. npc = Object(screen_w/2, screen_h/2, '@', libtcod.yellow)
  207.  
  208. objects = [npc, player]
  209.  
  210. make_map()
  211.  
  212. #---Main Loop
  213. while not libtcod.console_is_window_closed():
  214.  
  215. #Render the screen
  216. render_all()
  217.  
  218. #This prints the @ symbol
  219. libtcod.console_print_left(0, 1, 45, libtcod.BKGND_NONE, 'Welcome to Sida the Adventurer!')
  220. libtcod.console_print_left(0, 1, 46, libtcod.BKGND_NONE, 'Use arrow keys to move.')
  221. libtcod.console_print_left(0, 1, 47, libtcod.BKGND_NONE, 'Current build is: %s' % (curbuild))
  222. libtcod.console_print_left(0, 38, 45, libtcod.BKGND_NONE, 'The NPC is just there for show,')
  223. libtcod.console_print_left(0, 45, 46, libtcod.BKGND_NONE, 'it exists as a tile')
  224. libtcod.console_print_left(0, 42, 49, libtcod.BKGND_NONE, 'Every dungeon is random!')
  225.  
  226. libtcod.console_flush()
  227.  
  228. #Erase all objects at their old locations before they move
  229. for object in objects:
  230. object.clear()
  231.  
  232. #Handle keys and exit game if needed
  233. exit = handle_keys()
  234. if exit:
  235. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement