Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import libtcodpy as libtcod
- import random
- #actual size of the window
- SCREEN_WIDTH = 160
- SCREEN_HEIGHT = 80
- #fake map for now
- currentMap = []
- #booleans
- gameExit = False
- #some variables
- WORLD_WIDTH = 80
- WORLD_HEIGHT = 40
- ROOM_WIDTHMIN = 1
- ROOM_HEIGHTMIN = 1
- ROOM_WIDTH = 15
- ROOM_HEIGHT = 5
- LIMIT_FPS = 20 #20 frames-per-second maximum
- ###########################
- # CLASSES
- #######################
- class Object:
- #this is a generic object: the player, a monster, an item, the stairs...
- #it's always represented by a character on screen.
- def __init__(self, x, y, char, color):
- self.x = x
- self.y = y
- self.char = char
- self.color = color
- def move(self, dx, dy):
- self.x += dx
- self.y += dy
- def draw(self):
- #set the color and then draw the character that represents this object at its position
- libtcod.console_set_default_foreground(con, self.color)
- libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
- def clear(self):
- #erase the character that represents this object
- libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
- ########################
- # FUNCTIONS
- #########################
- def createWorld():
- theTempNum = 0
- for i in range(WORLD_WIDTH):
- tempList = []
- for r in range(WORLD_HEIGHT):
- tempVal = 0
- #lets pad so the rooms aren't on the edge
- if(r>ROOM_WIDTH and r<(WORLD_WIDTH-(ROOM_WIDTH+2))and i>ROOM_HEIGHT and i<(WORLD_HEIGHT-(ROOM_HEIGHT+2))):
- #how likely is it that this tile will be a room if there is no
- #other room on this row? 90% as of now.
- if(random.randrange(1,100)>60):
- #inc counter
- theTempNum = theTempNum + 1
- #this is the room
- tempVal = theTempNum
- tempList.append(tempVal)
- currentMap.append(tempList)
- text_file = open("world.wld", "w")
- #now fill out the rooms
- counter = 0
- for i in range(WORLD_WIDTH):
- for r in range(WORLD_HEIGHT):
- if currentMap[i][r]>0:
- if currentMap[i][r]!=counter:
- counter = currentMap[i][r]
- #how big will the room be?
- w_roomRange = random.randrange(ROOM_WIDTHMIN,ROOM_WIDTH)
- h_roomRange = random.randrange(ROOM_HEIGHTMIN,ROOM_HEIGHT)
- #modify the map
- for x in range(-h_roomRange,h_roomRange):
- for y in range(-w_roomRange,w_roomRange):
- if(i+x>1 and i+x<WORLD_WIDTH-1 and r+y>1 and r+y<WORLD_HEIGHT-1):
- string = "x=" + str(x) + ", y=" + str(y) + "\n"
- string += "i=" + str(i) + ", r=" + str(r) + "\n"
- string += "i+x=" + str(i+x) + ", r+y=" + str(r+y) + "\n"
- string += "--------------------\n"
- text_file.write(string)
- currentMap[i+x][r+y]=counter
- for item in currentMap:
- for zebra in item:
- if(zebra==0):
- text_file.write(" ")
- else:
- text_file.write(".")
- text_file.write("\n")
- #def checkDir(a):
- def handle_keys():
- global playerx, playery
- #key = libtcod.console_check_for_keypress() #real-time
- key = libtcod.console_wait_for_keypress(True) #turn-based
- if key.vk == libtcod.KEY_ENTER and key.lalt:
- #Alt+Enter: toggle fullscreen
- libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
- elif key.vk == libtcod.KEY_ESCAPE:
- return 1 #exit game
- elif key.vk == libtcod.KEY_CHAR:
- if key.c == ord('i'):
- return 2 #display inventory
- #movement keys
- if libtcod.console_is_key_pressed(libtcod.KEY_UP):
- player.move(0, -1)
- elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
- player.move(0, 1)
- elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
- player.move(-1, 0)
- elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
- player.move(1, 0)
- # Warm up functions for handling key strokes
- def bringUpInventory():
- #libtcod.console_put_char(con, 70, 15, "This is inventory", libtcod.BKGND_NONE)
- libtcod.console_put_char(con, 70, 15, 'd', libtcod.BKGND_NONE)
- def render_all():
- tempy = 0
- for item in currentMap:
- tempx = 0
- for zebra in item:
- if(zebra==0):
- if(tempx!=player.x or tempy!=player.y):
- libtcod.console_put_char(con, tempx, tempy, '#', libtcod.BKGND_SET)
- else:
- if(tempx!=player.x or tempy!=player.y):
- libtcod.console_put_char(con, tempx, tempy, chr(zebra), libtcod.BKGND_SET)
- tempx = tempx + 1
- tempy = tempy + 1
- for objectz in objects:
- objectz.draw()
- #final "blit" or render of the "con" screen
- libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
- def startGame():
- while not libtcod.console_is_window_closed():
- render_all()
- #flush console
- libtcod.console_flush()
- #remove all objects
- for objectz in objects:
- objectz.clear()
- #handle keys
- returnCode = handle_keys()
- #exit
- if returnCode == 1:
- break
- #inventory
- elif returnCode == 2:
- bringUpInventory()
- #############################################
- # Initialization & Main Loop
- #############################################
- libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
- libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
- libtcod.sys_set_fps(LIMIT_FPS)
- con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
- #game variables
- player = Object(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, '@', libtcod.white)
- npc = Object(SCREEN_WIDTH/2-5, SCREEN_HEIGHT/2, '@', libtcod.yellow)
- objects = [npc, player]
- #create the world
- createWorld()
- #start the game
- startGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement