Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 11th, 2013  |  syntax: Python  |  size: 7.34 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import libtcodpy as libtcod
  2. import random
  3.  
  4. #actual size of the window
  5. SCREEN_WIDTH = 160
  6. SCREEN_HEIGHT = 80
  7.  
  8. #fake map for now
  9. currentMap = []
  10.  
  11. #booleans
  12. gameExit = False
  13.  
  14. #some variables
  15. WORLD_WIDTH = 80
  16. WORLD_HEIGHT = 40
  17. ROOM_WIDTHMIN = 1
  18. ROOM_HEIGHTMIN = 1
  19. ROOM_WIDTH = 15
  20. ROOM_HEIGHT = 5
  21.  
  22. LIMIT_FPS = 20  #20 frames-per-second maximum
  23.  
  24.  
  25. ###########################
  26. # CLASSES
  27. #######################
  28. class Object:
  29.     #this is a generic object: the player, a monster, an item, the stairs...
  30.     #it's always represented by a character on screen.
  31.     def __init__(self, x, y, char, color):
  32.         self.x = x
  33.         self.y = y
  34.         self.char = char
  35.         self.color = color
  36.  
  37.     def move(self, dx, dy):
  38.             self.x += dx
  39.             self.y += dy
  40.  
  41.     def draw(self):
  42.         #set the color and then draw the character that represents this object at its position
  43.         libtcod.console_set_default_foreground(con, self.color)
  44.         libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
  45.  
  46.     def clear(self):
  47.         #erase the character that represents this object
  48.         libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
  49.  
  50.  
  51. ########################
  52. # FUNCTIONS
  53. #########################
  54. def createWorld():
  55.         theTempNum = 0
  56.         for i in range(WORLD_WIDTH):
  57.                 tempList = []
  58.                 for r in range(WORLD_HEIGHT):
  59.                         tempVal = 0
  60.                         #lets pad so the rooms aren't on the edge
  61.                         if(r>ROOM_WIDTH and r<(WORLD_WIDTH-(ROOM_WIDTH+2))and i>ROOM_HEIGHT and i<(WORLD_HEIGHT-(ROOM_HEIGHT+2))):
  62.                                 #how likely is it that this tile will be a room if there is no
  63.                                 #other room on this row?  90% as of now.
  64.                                 if(random.randrange(1,100)>60):
  65.                                         #inc counter
  66.                                         theTempNum = theTempNum + 1
  67.                                         #this is the room
  68.                                         tempVal = theTempNum
  69.                         tempList.append(tempVal)
  70.                 currentMap.append(tempList)
  71.  
  72.         text_file = open("world.wld", "w")
  73.         #now fill out the rooms
  74.         counter = 0
  75.         for i in range(WORLD_WIDTH):
  76.                 for r in range(WORLD_HEIGHT):
  77.                         if currentMap[i][r]>0:
  78.                                 if currentMap[i][r]!=counter:
  79.                                         counter = currentMap[i][r]
  80.                                         #how big will the room be?
  81.                                         w_roomRange = random.randrange(ROOM_WIDTHMIN,ROOM_WIDTH)
  82.                                         h_roomRange = random.randrange(ROOM_HEIGHTMIN,ROOM_HEIGHT)
  83.                                         #modify the map
  84.                                         for x in range(-h_roomRange,h_roomRange):
  85.                                                 for y in range(-w_roomRange,w_roomRange):
  86.                                                         if(i+x>1 and i+x<WORLD_WIDTH-1 and r+y>1 and r+y<WORLD_HEIGHT-1):
  87.                                                                 string =  "x=" + str(x) + ", y=" + str(y) + "\n"
  88.                                                                 string += "i=" + str(i) + ", r=" + str(r) + "\n"
  89.                                                                 string += "i+x=" + str(i+x) + ", r+y=" + str(r+y) + "\n"
  90.                                                                 string += "--------------------\n"
  91.                                                                 text_file.write(string)
  92.                                                                 currentMap[i+x][r+y]=counter
  93.        
  94.        
  95.         for item in currentMap:
  96.                 for zebra in item:
  97.                         if(zebra==0):
  98.                                 text_file.write(" ")
  99.                         else:
  100.                                 text_file.write(".")
  101.                 text_file.write("\n")
  102.        
  103.            
  104. #def checkDir(a):
  105. def handle_keys():
  106.  
  107.     global playerx, playery
  108.     #key = libtcod.console_check_for_keypress()  #real-time
  109.     key = libtcod.console_wait_for_keypress(True)  #turn-based
  110.  
  111.     if key.vk == libtcod.KEY_ENTER and key.lalt:
  112.         #Alt+Enter: toggle fullscreen
  113.         libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
  114.  
  115.     elif key.vk == libtcod.KEY_ESCAPE:
  116.         return 1  #exit game
  117.  
  118.     elif key.vk == libtcod.KEY_CHAR:
  119.         if key.c == ord('i'):
  120.             return 2  #display inventory
  121.  
  122.     #movement keys
  123.     if libtcod.console_is_key_pressed(libtcod.KEY_UP):
  124.         player.move(0, -1)
  125.  
  126.     elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
  127.         player.move(0, 1)
  128.  
  129.     elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
  130.         player.move(-1, 0)
  131.  
  132.     elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
  133.         player.move(1, 0)
  134.  
  135. # Warm up functions for handling key strokes
  136.        
  137. def bringUpInventory():
  138.         #libtcod.console_put_char(con, 70, 15, "This is inventory", libtcod.BKGND_NONE)
  139.         libtcod.console_put_char(con, 70, 15, 'd', libtcod.BKGND_NONE)
  140.        
  141. def render_all():
  142.         tempy = 0
  143.         for item in currentMap:
  144.                 tempx = 0
  145.                 for zebra in item:
  146.                         if(zebra==0):
  147.                                 if(tempx!=player.x or tempy!=player.y):
  148.                                         libtcod.console_put_char(con, tempx, tempy, '#', libtcod.BKGND_SET)
  149.                         else:
  150.                                 if(tempx!=player.x or tempy!=player.y):
  151.                                         libtcod.console_put_char(con, tempx, tempy, chr(zebra), libtcod.BKGND_SET)
  152.                         tempx = tempx + 1
  153.                 tempy = tempy + 1
  154.  
  155.         for objectz in objects:
  156.                 objectz.draw()
  157.  
  158.         #final "blit" or render of the "con" screen
  159.         libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
  160.        
  161. def startGame():
  162.         while not libtcod.console_is_window_closed():
  163.            
  164.             render_all()
  165.            
  166.             #flush console
  167.             libtcod.console_flush()
  168.             #remove all objects
  169.             for objectz in objects:
  170.                     objectz.clear()
  171.                    
  172.             #handle keys
  173.             returnCode = handle_keys()
  174.             #exit
  175.             if returnCode == 1:
  176.                 break
  177.             #inventory
  178.             elif returnCode == 2:
  179.                 bringUpInventory()
  180.  
  181.  
  182. #############################################
  183. # Initialization & Main Loop
  184. #############################################
  185.  
  186. libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
  187. libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
  188. libtcod.sys_set_fps(LIMIT_FPS)              
  189. con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
  190.  
  191. #game variables
  192. player = Object(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, '@', libtcod.white)
  193. npc = Object(SCREEN_WIDTH/2-5, SCREEN_HEIGHT/2, '@', libtcod.yellow)
  194.  
  195. objects = [npc, player]
  196.  
  197.  
  198. #create the world
  199. createWorld()
  200. #start the game
  201. startGame()