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