2013-05-01 92 views
0

我在某個地方弄錯了什麼嗎?如果我把putxy放在同一個盒子裏,它會呈現一個字符串,但由於某種原因,這段代碼不喜歡對象。這段代碼應該做的是在屏幕上呈現一組隨機定位的「t」,並且這種方式在沒有對象的情況下工作正常。但是,當我添加對象時,沒有任何呈現。我究竟做錯了什麼?非常感謝幫助。代碼如下:這段代碼爲什麼在屏幕上不顯示對象?

------------------------------- 
-- 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 2013-05-01 22:34:39

+0

請注意,您並未在'player.refresh()'中重置'player.roomy',您正在設置全局'寬敞'。 – 2013-05-02 03:22:35

+0

謝謝,我沒有聽清楚。 – William 2013-05-02 20:55:04

回答

1

您需要的參數的順序交換到table.insert - 它應該是

table.insert(objects, obj1) 

table.insert(obj1, objects) 

您還可以使用

setmetatable(objects,{__index=table}) 
objects:insert(obj1) 
-- etc. 
相關問題