2013-05-01 63 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. 
相关问题