Advertisement
Hadlock

firstrl_1.py

Nov 9th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.69 KB | None | 0 0
  1. ########################################
  2. # Test thing of things for Typographer #
  3. # 12:18pm 11/6/2011 Hadlock            #
  4. ########################################
  5.  
  6. #import libtcodpy & libtcod library thing for python
  7. import libtcodpy as libtcod
  8.  
  9. #important, unchanging values
  10. SCREEN_WIDTH = 100
  11. SCREEN_HEIGHT = 55
  12.  
  13. #size of the map
  14. MAP_WIDTH = 100
  15. MAP_HEIGHT = 55
  16.  
  17. #parameters for dungeon generator
  18. ROOM_MAX_SIZE = 10
  19. ROOM_MIN_SIZE = 6
  20. MAX_ROOMS = 30
  21.  
  22. LIMIT_FPS = 20  #MORE SPEED
  23.  
  24.  
  25. color_dark_wall = libtcod.Color(0, 0, 100)
  26. color_dark_ground = libtcod.Color(50, 50, 150)
  27.  
  28. #tile class determines characteristics, such as see through, passable, etc
  29. class Tile:
  30.     #a tile of the map and its properties
  31.     def __init__(self, blocked, block_sight = None):
  32.         self.blocked = blocked
  33.  
  34.         #by default, if a tile is blocked, it also blocks sight
  35.         if block_sight is None: block_sight = blocked
  36.         self.block_sight = block_sight
  37.  
  38.  
  39. #let's build a room!       
  40. class Rect:
  41.     #a rectangle on the map. used to characterize a room.
  42.     def __init__(self, x, y, w, h):
  43.         self.x1 = x
  44.         self.y1 = y
  45.         self.x2 = x + w
  46.         self.y2 = y + h
  47.  
  48.     def center(self):
  49.         center_x = (self.x1 + self.x2) / 2
  50.         center_y = (self.y1 + self.y2) / 2
  51.         return (center_x, center_y)
  52.  
  53.     def intersect(self, other):
  54.         #returns true if this rectangle intersects with another one
  55.         return (self.x1 <= other.x2 and self.x2 >= other.x1 and
  56.                 self.y1 <= other.y2 and self.y2 >= other.y1)   
  57.        
  58. #object - players, npcs etc are defined by this    
  59. class Object:
  60.     #this is a generic object: the player, a monster, an item, the stairs...
  61.     #it's always represented by a character on screen.
  62.     def __init__(self, x, y, char, color):
  63.         self.x = x
  64.         self.y = y
  65.         self.char = char
  66.         self.color = color
  67.  
  68.     def move(self, dx, dy):
  69.         #move by the given amount, if the destination is not blocked
  70.         if not map[self.x + dx][self.y + dy].blocked:
  71.             self.x += dx
  72.             self.y += dy
  73.  
  74.     def draw(self):
  75.         #set the color and then draw the character that represents this object at its position
  76.         libtcod.console_set_foreground_color(con, self.color)
  77.         libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
  78.  
  79.     def clear(self):
  80.         #erase the character that represents this object
  81.         libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
  82.  
  83.  
  84. #room carving 101 (converts a series of tiles from blocking to passable))
  85. def create_room(room):
  86.     global map
  87.     #go through the tiles in the rectangle and make them passable
  88.     for x in range(room.x1 + 1, room.x2):
  89.         for y in range(room.y1 + 1, room.y2):
  90.             map[x][y].blocked = False
  91.             map[x][y].block_sight = False
  92.  
  93. #door carving 101 (cuts a HORIZONTAL hole in walls to make a door between two rooms)           
  94. def create_h_tunnel(x1, x2, y):
  95.     global map
  96.     #horizontal tunnel. min() and max() are used in case x1>x2
  97.     for x in range(min(x1, x2), max(x1, x2) + 1):
  98.         map[x][y].blocked = False
  99.         map[x][y].block_sight = False
  100.            
  101. #door carving 102 (cuts a VERTICAL hole in walls to make a door between two rooms)
  102. def create_v_tunnel(y1, y2, x):
  103.     global map
  104.     #vertical tunnel
  105.     for y in range(min(y1, y2), max(y1, y2) + 1):
  106.         map[x][y].blocked = False
  107.         map[x][y].block_sight = False
  108.  
  109.  
  110. #some fuckin map generation goin' on
  111. def make_map():
  112.     global map, player
  113.  
  114.     #fill map with "blocked" tiles
  115.     map = [[ Tile(True)
  116.         for y in range(MAP_HEIGHT) ]
  117.             for x in range(MAP_WIDTH) ]
  118.  
  119.     rooms = []
  120.     num_rooms = 0
  121.  
  122.     for r in range(MAX_ROOMS):
  123.         #random width and height
  124.         w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
  125.         h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)
  126.         #random position without going out of the boundaries of the map
  127.         x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1)
  128.         y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1)
  129.  
  130.         #"Rect" class makes rectangles easier to work with
  131.         new_room = Rect(x, y, w, h)
  132.  
  133.         #run through the other rooms and see if they intersect with this one
  134.         failed = False
  135.         for other_room in rooms:
  136.             if new_room.intersect(other_room):
  137.                 failed = True
  138.                 break
  139.  
  140.         if not failed:
  141.             #this means there are no intersections, so this room is valid
  142.  
  143.             #"paint" it to the map's tiles
  144.             create_room(new_room)
  145.  
  146.             #center coordinates of new room, will be useful later
  147.             (new_x, new_y) = new_room.center()
  148.  
  149.             if num_rooms == 0:
  150.                 #this is the first room, where the player starts at
  151.                 player.x = new_x
  152.                 player.y = new_y
  153.             else:
  154.                 #all rooms after the first:
  155.                 #connect it to the previous room with a tunnel
  156.  
  157.                 #center coordinates of previous room
  158.                 (prev_x, prev_y) = rooms[num_rooms-1].center()
  159.  
  160.                 #draw a coin (random number that is either 0 or 1)
  161.                 if libtcod.random_get_int(0, 0, 1) == 1:
  162.                     #first move horizontally, then vertically
  163.                     create_h_tunnel(prev_x, new_x, prev_y)
  164.                     create_v_tunnel(prev_y, new_y, new_x)
  165.                 else:
  166.                     #first move vertically, then horizontally
  167.                     create_v_tunnel(prev_y, new_y, prev_x)
  168.                     create_h_tunnel(prev_x, new_x, new_y)
  169.  
  170.             #finally, append the new room to the list
  171.             rooms.append(new_room)
  172.             num_rooms += 1
  173.  
  174.  
  175. #let's render some map stuff (i think?)
  176. def render_all():
  177.     global color_light_wall
  178.     global color_light_ground
  179.  
  180.     #go through all tiles, and set their background color
  181.     for y in range(MAP_HEIGHT):
  182.         for x in range(MAP_WIDTH):
  183.             wall = map[x][y].block_sight
  184.             if wall:
  185.                 libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET )
  186.             else:
  187.                 libtcod.console_set_back(con, x, y, color_dark_ground, libtcod.BKGND_SET )
  188.  
  189.     #draw all objects in the list
  190.     for object in objects:
  191.         object.draw()
  192.  
  193.     #blit the contents of "con" to the root console
  194.     libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
  195.  
  196.  
  197.  
  198. #keyboard input - also toggles realtime on/off
  199. def handle_keys():
  200.     key = libtcod.console_check_for_keypress()  #real-time ON
  201.     #key = libtcod.console_wait_for_keypress(True)  #real-time OFF; turn-based - aka DEBUG MODE
  202.  
  203.     if key.vk == libtcod.KEY_ENTER and key.lalt:
  204.         #Alt+Enter: toggle fullscreen
  205.         libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
  206.  
  207.     elif key.vk == libtcod.KEY_ESCAPE:
  208.         return True  #exit game
  209.  
  210.     #movement keys
  211.     if libtcod.console_is_key_pressed(libtcod.KEY_UP):
  212.         player.move(0, -1)
  213.  
  214.     elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
  215.         player.move(0, 1)
  216.  
  217.     elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
  218.         player.move(-1, 0)
  219.  
  220.     elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
  221.         player.move(1, 0)
  222.  
  223.  
  224. ##########################################################
  225. #the main loop - if window is open, game continues to run#
  226. ##########################################################
  227.  
  228. #pick a font. any font.
  229. libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
  230. #the most important call initialize window:
  231. libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
  232. #let's limit the FPS because this will be a real time roguelike
  233. libtcod.sys_set_fps(LIMIT_FPS)
  234. #initialize offscreen console
  235. con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
  236.  
  237. #create object representing the player
  238. player = Object(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, '@', libtcod.white)
  239.  
  240. #create an NPC
  241. npc = Object(SCREEN_WIDTH/2 - 5, SCREEN_HEIGHT/2, '@', libtcod.yellow)
  242.  
  243. #the list of objects with those two
  244. objects = [npc, player]
  245.  
  246. #generate map (at this point it's not drawn to the screen)
  247. make_map()
  248.  
  249.  
  250. while not libtcod.console_is_window_closed():
  251.  
  252.     #render the screen
  253.     render_all()
  254.     #delete it so we can start all over again
  255.     libtcod.console_flush()
  256.  
  257.     #erase all objects at their old locations, before they move
  258.     for object in objects:
  259.         object.clear()
  260.  
  261.     #handle keys and exit game if needed
  262.     exit = handle_keys()
  263.     if exit:
  264.         break
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement