- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我和一个 friend 尝试使用 Löve 框架在 Lua 中编写扫雷程序。到目前为止,代码需要查看一个框(一个单元格)是否被选中,然后绘制它。我们是 Lua 的新手,该程序现在有一个缺陷,它只能在右下角的框上运行。
更新:现在看,我发现初始化的 GameBoard 的值都具有相同的值(即,GameBoard[1]
到 GameBoard[150]
都是相同的单元格)。
代码如下:
conf.lua
定义了一些全局变量:
function love.conf(t)
-- Global variables.
CELL_SIZE = 40
NUM_ROWS = 15
NUM_COLS = 10
STATS_HEIGHT = 100
AMOUNT_OF_CELLS = NUM_ROWS * NUM_COLS
GRID_WIDTH = 400
GRID_HEIGHT = 700
end
这是 main.lua
中的相关失败代码(在 GameBoard
填充 Cell
的加载方法中出错。
-- The Cell table is used for every individual square on
-- the gameboard
Cell = {}
-- The Gameboard (150 Cell objects)
GameBoard = {}
-- The function new belongs to Cell and spawns a new object (a table)
-- with the same attributes as Cell.
function Cell:new(i, j)
-- each cell knows:
-- its x- and y-coordinates.
self.x_min = (i-1) * CELL_SIZE
self.x_max = (CELL_SIZE-1) + (i-1) * CELL_SIZE
self.y_min = STATS_HEIGHT + (j-1) * CELL_SIZE
self.y_max = STATS_HEIGHT + (CELL_SIZE-1) + (j-1) * CELL_SIZE
-- if it is a mine (determined with random number generator)
isMine = (math.random(1, 8) % 8 == 0) -- Roughly 0.15 (1/7) times true (is a mine)
self.isMine = isMine
-- do not check the mine initially
self.checked = false
-- return the cell object
return self;
end
-- love.load is a love-function that is called once when the game
-- starts.
function love.load()
-- The index of the cell on the GameBoard (ranging from 1 to 150)
local index = 1
-- Build a two dimensional table of Cell-objects
for i = 1, NUM_COLS, 1 do
for j = 1, NUM_ROWS, 1 do
GameBoard[ index ] = Cell:new( i, j )
index = index + 1
end
end
end
结果是所有框都具有索引为 150 的下框的值(自 NUM_ROWS * NUM_COLS
= 150 以来的最新值)。表 ( Cells
) 的所有元素 ( Gameboard
) 都具有在 Cell:new
中设置的相同 x 和 y 值方法。
如果有人能告诉我们如何正确初始化和访问表的表,我们将不胜感激。
最佳答案
在函数 Cell:new
中,self
是表 Cell
本身,所以你每次都返回同一个表。
一个简单的解决方法是创建一个新表:
function Cell:new(i, j)
local t = {}
t.x_min = (i-1) * CELL_SIZE
--omit the rest
return t;
end
为了将来的改进,您可能对另一种实现原型(prototype)的方式感兴趣:
function Cell:new(i, j)
local o = {}
setmetatable(o, self)
self.__index = self
self.x_min = (i-1) * CELL_SIZE
--omits rest
return o;
end
阅读PiL: Object-Oriented Programming了解更多信息。
关于lua - 如何在 Lua 中用表初始化表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685409/
有人可以向我解释该声明在C++中的含义吗?我从未见过这样的声明,只是对它的含义和作用感到困惑: int ix((dx > 0) - (dx > 1)); 最佳答案 您可以在括号或花括号中使用初始化程序
我有一个带有单词的mysql数据库。我用 while 语句打印所有单词。所以我觉得: 马铃薯番茄生菜 一切正常,但我想按长度对单词进行排序。我试过: if(strlen($go['words']) =
我忠实的路径遍历方法不再有效——它将空格视为分隔符。好久没做批处理编程了。 使用 FOR 循环时,唯一允许使用分隔符的是 FOR/F 选项。 我不想创建一个包含路径的临时文件,希望做如下的事情: C:
新建一个表: ? 1
我有一些带有多行块的文本文件,例如 2011/01/01 13:13:13,, Some Certain Text,=, [ certain text [
我想在 Vim 中文件的不同部分之间进行一些很好的分离: 我想用#'s 填充一行,然后在中间写上我的标题: ############################# 居中标题############
我该如何逃生 "*"至 "\*"在clojure?似乎无法让它工作: (s/replace "A*B" #"*" "*")生产 "A*B" (当然) (s/replace "A*B" #"*" "\*
这周我一直在努力更熟悉 C。我一直在阅读C Primer Plus (5th Edition) 但是我仍然在使用变量和指针时遇到了一些麻烦。 这是我用来测试的脚本: int main (int arg
在 Dart 中,初始化 List 有什么区别?使用 new 运算符并使用文字对其进行初始化? 情况1: List args = new List(2); args[0] = 1; args[1] =
我有一个字符向量,如下所示: "Internet" "Internet" "-1" "-5" "Internet" "Internet" 我想替换所有负数值的值(-1、-5 等
我有一个名为 gen 的数据框,如下所示 A B C D E 1 NA 4.35 35.3 3.36 4.8
我有一个字符向量,如下所示: "Internet" "Internet" "-1" "-5" "Internet" "Internet" 我想替换所有负数值的值(-1、-5 等
我想知道为什么 CMake 中的变量经常用美元符号和大括号括起来。例如,我看到这个电话in a CMake tutorial . include_directories(${PROJECT_BINAR
我正在尝试做这样的事情 $this->db->count_all("grant_money")->where('id',5); 这可能吗? 如果有任何其他方法可以做到这一点,请告诉我。谢谢 我想像上面
为什么这是有效的: int a = 5; int *aPtr = &a; printf("%i", *aPtr); 但这不是: int a = 5; int aPtr = &a; printf("%i
假设我有一个格式为“11.23.13”的日期字符串,我想用“/”替换每个点,使其看起来像“11/23/13”。 这是我的代码,但它无法正常工作,因为正则表达式看到“.”并将其解释为匹配每个字符而不是新
如何在键盘输入的字符处打印*? 例子: 如果我在控制台中输入:mouli,那么它应该将 m 替换为 *,然后是 o用 * 等等。 最佳答案 使用标准 API 无法解决此问题。如果这确实是一个明确的要求
我最近开始学习 Javascript,同时对卡在这段代码中的代码进行了一些实验: var k = { ab: "hi", func: function() { cons
我需要用“.”替换第一列中的重复项 例如: name1 name1 name1 name2 name2 name3 name3 我需要输出: name1 . . name2 . name3 . 我有这
我有以下两个表 education 和 jobs,每个表都有时间戳字段。在续集语句中,我想选择并确定两个表中保存的两个时间戳中哪个是最新的。 我已经尝试了以下但并不愉快; SELECT e.Sta
我是一名优秀的程序员,十分优秀!