2013-05-01 4 views
0

어딘가에서 뭔가 엉망이 되었습니까? putsxy를 같은 상자에 넣으면 문자열이 잘 표현되지만, 어떤 이유로이 코드는 객체를 싫어합니다. 이 코드는 무작위로 배치 된 집합을 화면에 렌더링하는 것으로 객체없이 잘 동작합니다. 그러나 객체를 추가하면 아무 것도 렌더링되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 도움을 많이 주시면 감사하겠습니다. 다음과 같이 코드는 다음과 같습니다이 코드가 화면에 아무 것도 렌더링하지 않는 이유는 무엇입니까?

------------------------------- 
-- Asterix v0.0.3 -- 
-- Copyright 2013 WilliamTDR. -- 
------------------------------- 

-- prerequestities -- 
require "actions" -- button input 
require "math" -- random numbers 

-- Global Variables -- 
local player = { 
    health = 20, -- player health 
    foodparts = 12, -- portions of food until the food level depletes by one 
    food = 40, -- player food 
    x = 50, -- player x position 
    y = 50, -- player y position 
    roomx = 0, -- x value of room player is in 
    roomy = 0 -- y value of room player is in 
} 

local object = { 
    x = 30, 
    y = 30, 
    roomx = 0, 
    roomy = 0, 
    name = "Unknown Object", 
    touchingplayer = false, 
    symbol = "u" 
} 

local objects = {} 

function object:new (o) 
    o = o or {} -- create object if user does not provide one 
    setmetatable(o, self) 
    self.__index = self 
    return o 
end 

function player.refresh() 
    -- reset player values 
    player.food = 40 
    player.health = 20 
    player.foodparts = 12 
    player.x = 50 
    player.y = 50 
    player.roomx = 0 
    roomy = 0 
end 

function player.die() 
    rb.lcd_clear_display() -- clear the display 
    rb.lcd_putsxy(1, 7, "You have died!") 
    rb.lcd_putsxy(1, 45, "Respawning in 3...") 
    rb.lcd_update() 
    rb.sleep(rb.HZ) -- sleep for one second 
    rb.lcd_putsxy(1, 45, "Respawning in 2...") 
    rb.lcd_update() 
    rb.sleep(rb.HZ) 
    rb.lcd_putsxy(1, 45, "Respawning in 1...") 
    rb.lcd_update() 
    rb.sleep(rb.HZ) 
    player.refresh() 
    render() 
end 

function renderobjects() 
    for _,object in ipairs(objects) do 
     if player.roomx == object.roomx and player.roomy == object.roomy then 
      rb.putsxy(object.x, object.y, object.symbol) 
     end 
    end 
    rb.lcd_update() 
end 

function createnewobjects() 
    obj1 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj2 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj3 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj4 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj5 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj6 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj7 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj8 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj9 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj10 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj11 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj12 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj13 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}    
    obj14 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj15 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj16 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj17 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj18 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj19 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    obj20 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"} 
    table.insert(obj1, objects) 
    table.insert(obj2, objects) 
    table.insert(obj3, objects) 
    table.insert(obj4, objects) 
    table.insert(obj5, objects) 
    table.insert(obj6, objects) 
    table.insert(obj7, objects) 
    table.insert(obj8, objects) 
    table.insert(obj9, objects) 
    table.insert(obj10, objects) 
    table.insert(obj11, objects) 
    table.insert(obj12, objects) 
    table.insert(obj13, objects) 
    table.insert(obj14, objects) 
    table.insert(obj15, objects) 
    table.insert(obj16, objects) 
    table.insert(obj17, objects) 
    table.insert(obj18, objects) 
    table.insert(obj19, objects) 
    table.insert(obj20, objects) 
end 

function checks() 
    if player.foodparts < 1 then 
     player.foodparts = 12 
     player.food = player.food - 1 
    end 

    if player.food < 1 then 
     player.health = player.health - 1 
    end 

    if player.health < 1 then 
     player.die() 
    end 

    if player.x < 1 then 
     player.roomx = player.roomx + 1 
     createnewobjects() 
     player.x = 92 
    end 

    if player.x > 93 then 
     player.roomx = player.roomx - 1 
     createnewobjects() 
     player.x = 4 
    end 

    if player.y < 1 then 
     player.roomy = player.roomy + 1 
     createnewobjects() 
     player.y = 92 
    end 

    if player.y > 93 then 
     player.roomy = player.roomy - 1 
     renderobjects() 
     player.y = 4 
    end 

    end 

function render() 
    checks() 
    rb.lcd_set_background(0, 255, 0) -- background is black 
    rb.lcd_set_foreground(-1, 0, 0) -- foreground is white 
    rb.lcd_clear_display() 
    rb.lcd_fillrect(5, 3, (player.health * 2), 2) 
    rb.lcd_set_foreground(88, 2, 2) 
    rb.lcd_fillrect(45, 3, player.food, 2) 
    rb.lcd_set_foreground(125, 125, 0) 
    rb.lcd_putsxy(player.x, player.y, "*") 
    renderobjects() 
    rb.lcd_update() 
end 

function LoadGame() 
render() 
    while true do 
     button = rb.get_plugin_action(0) 
     if button == 182 then 
      player.x = player.x - 1 
      player.foodparts = player.foodparts - 1 
      render() 
     elseif button == 183 then 
      player.x = player.x + 1 
      player.foodparts = player.foodparts - 1 
      render() 
     elseif button == 180 then 
      player.y = player.y - 1 
      player.foodparts = player.foodparts - 1 
      render() 
     elseif button == 181 then 
      player.y = player.y + 1 
      player.foodparts = player.foodparts - 1 
      render() 
     elseif button == 186 then 
      player.x = player.x - 4 
      player.foodparts = player.foodparts - 4 
      LoadGame() 
     elseif button == 187 then 
      player.x = player.x + 4 
      player.foodparts = player.foodparts - 4 
      LoadGame() 
     elseif button == 184 then 
      player.y = player.y - 4 
      player.foodparts = player.foodparts - 4 
      LoadGame() 
     elseif button == 185 then 
      player.y = player.y + 4 
      player.foodparts = player.foodparts - 4 
      LoadGame() 
     elseif button == 191 then 
      ShowMainMenu() -- start the program by displaying the main menu of the script. 
     end 
    end 
end 


function ShowMainMenu() 
    mainmenu = {"New Game", "Load Game", "About MCRE", "Exit"} 

    while true do 
     s = rb.do_menu("Asterix v0.0.1", mainmenu, nil, false) 

     if  s == 0 then LoadGame() 
     elseif s == 1 then rb.splash(3 * rb.HZ, "No maps found! Create one first.") 
     elseif s == 2 then rb.splash(5 * rb.HZ, "Game ©2013 WilliamTDR. Version 0.0.1, build 4. Minecraft is a registered trademark of Notch Development AB.") 
     elseif s == 3 then os.exit() 
     elseif s == -2 then os.exit() 
     else rb.splash(2 * rb.HZ, "Whoops: Unexpected Button press!" .. s) 
     end 
    end 
end 

ShowMainMenu() -- start the program by displaying the main menu of the script. 
+0

또한 문자열 렌더링을 코드에서 장소를 부추 겨서 작동 했으므로 렌더링 시스템에서 뭔가가 발생했습니다. 또한 구문 오류가 없습니다. – William

+0

'player.roomy'를'player.refresh()'에 리셋하지 않는다면, 전역 변수'roomy'를 설정하는 것입니다. –

+0

고마워, 나는 그것을 잡지 못했다. – William

답변

1

당신은 table.insert에 인수의 순서를 바꿀 필요 - 또한

을 사용할 수는

table.insert(objects, obj1) 

하지

table.insert(obj1, objects) 

해야한다

setmetatable(objects,{__index=table}) 
objects:insert(obj1) 
-- etc. 
관련 문제