2013-02-21 40 views
0

嘿,我正在用python使用Libtcod創建一個roguelike遊戲。我有代碼在遊戲製作隨機房間的地方。它應該使多個房間,直到一個十字架,然後停止製作房間。問題是它總是隻創建一個房間。這裏是孔代碼:Python代碼永遠不會到達它應該的某個部分

import libtcodpy as libtcod; 

SCREEN_WIDTH = 80; 
SCREEN_HEIGHT = 50; 

MAP_WIDTH = 80 
MAP_HEIGHT = 45 

ROOM_MAX_SIZE = 10 
ROOM_MIN_SIZE = 6 
MAX_ROOMS = 30 

LIMIT_FPS = 20; 

color_dark_wall = libtcod.Color(0, 0, 100) 
color_dark_ground = libtcod.Color(50, 50, 150) 

class Tile: 
    def __init__(self, blocked, block_sight = None): 
     self.blocked = blocked 

     if block_sight is None: block_sight = blocked 
     self.block_sight = block_sight 

class Rect: 
    def __init__(self, x, y , w, h): 
     self.x1 = x 
     self.y1 = y 
     self.x2 = x + w 
     self.y2 = y + h 

    def center(self): 
     center_x = (self.x1 + self.x2)/2 
     center_y = (self.y1 + self.y2)/2 
     return (center_x, center_y) 

    def intersect(self, other): 
     return (self.x1 <= other.x2 and self.x2 >= other.x1 and 
       self.y1 <= other.y2 and self.y2 >= other.y1) 

class Object: 
    def __init__(self, x, y, char, color): 
     self.x = x 
     self.y = y 
     self.char = char 
     self.color = color 

    def move(self, dx, dy): 
     if not map[self.x + dx][self.y + dy].blocked: 
      self.x += dx 
      self.y += dy 

    def draw(self): 
     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): 
     libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE) 

def create_room(room): 
    global map 

    for x in range(room.x1 + 1, room.x2): 
     for y in range(room.y1 + 1, room.y2): 
      map[x][y].blocked = False 
      map[x][y].block_sight = False 

def create_h_tunnel(x1, x2, y): 
    global map 

    for x in range(min(x1, x2), max(x1, x2) + 1): 
     map[x][y].blocked = False 
     map[x][y].block_sight = False 

def create_v_tunnel(y1, y2, x): 
    global map 

    print("Hello") 

    for y in range(min(y1, y2), max(y1, y2) + 1): 
     map[x][y].blocked = False 
     map[x][y].block_sight = False 


def make_map(): 
    global map 

    map = [[ Tile(True) 
     for y in range(MAP_HEIGHT) ] 
      for x in range(MAP_WIDTH) ] 

    rooms = [] 
    num_rooms = 0 

    for r in range(MAX_ROOMS): 
     w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) 
     h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) 
     x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) 
     y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h -1) 

    new_room = Rect(x, y, w, h) 

    failed = False 

    for other_room in rooms: 
     if new_room.intersect(other_room): 
      failed = True 
      break 

    if not failed: 
     create_room(new_room) 

     (new_x, new_y) = new_room.center() 

     if num_rooms == 0: 
      player.x = new_x 
      player.y = new_y 

     else: 
      (prev_x, prev_y) = rooms[num_rooms-1].center() 

      if libtcod.random_get_int(0, 0, 1) == 1: 
       create_h_tunnel(prev_x, new_x, prev_y) 
       create_v_tunnel(prev_y, new_y, new_x) 
      else: 
       create_v_tunnel(prev_y, new_y, prev_x) 
       create_h_tunnel(prev_x, new_x, new_y) 

     rooms.append(new_room) 
     num_rooms += 1 

def render_all(): 
    global color_dark_wall, color_light_wall 
    global color_dark_ground, color_light_ground 

    for y in range(MAP_HEIGHT): 
     for x in range (MAP_WIDTH): 
      wall = map[x][y].block_sight 
      if wall: 
       libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) 
      else: 
       libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) 

    for object in objects: 
     object.draw() 

    libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) 

def handle_keys(): 
    key = libtcod.console_check_for_keypress() 
    if key.vk == libtcod.KEY_ENTER and key.lalt: 
     libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) 

    elif key.vk == libtcod.KEY_ESCAPE: 
     return True #exit game 

    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) 

libtcod.console_set_custom_font('terminal10x10_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD); 
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'Lets Crawl', False); 
libtcod.sys_set_fps(LIMIT_FPS); 
con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) 

player = Object(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, '@', libtcod.green) 
npc = Object(SCREEN_WIDTH/2 - 5, SCREEN_HEIGHT/2, '@', libtcod.yellow) 
objects = [npc, player] 

#makes the map 
make_map() 

while not libtcod.console_is_window_closed(): 

    render_all() 


    #libtcod.console_check_for_keypress() 

    libtcod.console_flush(); 

    for object in objects: 
     object.clear() 

    exit = handle_keys() 
    if exit: 
     break 

這裏是我懷疑問題是make_map()函數:

def make_map(): 
    global map 

    map = [[ Tile(True) 
     for y in range(MAP_HEIGHT) ] 
      for x in range(MAP_WIDTH) ] 

    rooms = [] 
    num_rooms = 0 

    for r in range(MAX_ROOMS): 
     w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) 
     h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) 
     x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) 
     y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h -1) 

    new_room = Rect(x, y, w, h) 

    failed = False 

    for other_room in rooms: 
     if new_room.intersect(other_room): 
      failed = True 
      break 

    if not failed: 
     create_room(new_room) 

     (new_x, new_y) = new_room.center() 

     if num_rooms == 0: 
      player.x = new_x 
      player.y = new_y 

     else: 
      (prev_x, prev_y) = rooms[num_rooms-1].center() 

      if libtcod.random_get_int(0, 0, 1) == 1: 
       create_h_tunnel(prev_x, new_x, prev_y) 
       create_v_tunnel(prev_y, new_y, new_x) 
      else: 
       create_v_tunnel(prev_y, new_y, prev_x) 
       create_h_tunnel(prev_x, new_x, new_y) 

     rooms.append(new_room) 
     num_rooms += 1 

後,程序會創建房間,它增加了房間列表中,但它不會繼續製造房間嗎?還是每個創建的房間都是相同的大小(即使即時通過隨機數),它創建了第一個房間,然後停下來?我不明白...

回答

1

我懷疑你有一個縮進錯誤。在w,h,x和y的定義應該是同一個for循環的一部分之後,看起來像所有東西,所以應該縮進到相同的級別。

+0

哇。謝啦。不管我看我的代碼多長時間,我都從未發現這些錯誤。我想有第二雙眼睛看它會有很大的幫助。謝啦 – 2013-02-21 20:51:33

相關問題