将Corona SDK中的瓷砖拼接成一个突破游戏网格的单词?

我有一个游戏项目来重新实施Breakout 。 我想显示两个单词,每个单词在一行。 他们join了砖块。 里面,第一行是第一个名字,左alignment。 底线是最后一个名字,右alignment。 他们是从文本框input,并呈现如下所示:

如上所述

每经过一秒钟,屏幕会向网格添加一个可configuration数量的砖块(例如,每秒五块砖块),直到两个单词显示完成。 我显示了从matrix(0,1)创build的字母表字母。

…但我不知道如何将它们合并成一个单词。 我怎样才能join这些信件?

这是我到目前为止:

Bricks.lua

local Bricks = display.newGroup() -- static object local Events = require("Events") local Levels = require("Levels") local sound = require("Sound") local physics = require("physics") local Sprites = require("Sprites") local Func = require("Func") local brickSpriteData = { { name = "brick", frames = {Sprites.brick} }, { name = "brick2", frames = {Sprites.brick2} }, { name = "brick3", frames = {Sprites.brick3} }, } -- animation table local brickAnimations = {} Sprites:CreateAnimationTable { spriteData = brickSpriteData, animationTable = brickAnimations } -- get size from temp object for later use local tempBrick = display.newImage('red_apple_20.png',300,500) --local tempBrick = display.newImage('cheryGreen2.png',300,500) local brickSize = { width = tempBrick.width, height = tempBrick.height } --tempBrick:removeSelf( ) ---------------- -- Rubble -- needs to be moved to its own file ---------------- local rubbleSpriteData = { { name = "rubble1", frames = {Sprites.rubble1} }, { name = "rubble2", frames = {Sprites.rubble2} }, { name = "rubble3", frames = {Sprites.rubble3} }, { name = "rubble4", frames = {Sprites.rubble4} }, { name = "rubble5", frames = {Sprites.rubble5} }, } local rubbleAnimations = {} Sprites:CreateAnimationTable { spriteData = rubbleSpriteData, animationTable = rubbleAnimations } local totalBricksBroken = 0 -- used to track when level is complete local totalBricksAtStart = 0 -- contains all brick objects local bricks = {} local function CreateBrick(data) -- random brick sprite local obj = display.newImage('red_apple_20.png') local objGreen = display.newImage('cheryGreen2.png') obj.name = "brick" obj.x = data.x --or display.contentCenterX obj.y = data.y --or 1000 obj.brickType = data.brickType or 1 obj.index = data.index function obj:Break() totalBricksBroken = totalBricksBroken + 1 bricks[self.index] = nil obj:removeSelf( ) sound.play(sound.breakBrick) end function obj:Update() if(self == nil) then return end if(self.y > display.contentHeight - 20) then obj:Break() end end if(obj.brickType ==1) then physics.addBody( obj, "static", {friction=0.5, bounce=0.5 } ) elseif(obj.brickType == 2) then physics.addBody( objGreen,"static",{friction=0.2, bounce=0.5, density = 1 } ) end return obj end local currentLevel = testLevel -- create level from bricks defined in an object -- this allows for levels to be designed local function CreateBricksFromTable(level) totalBricksAtStart = 0 local activeBricksCount = 0 for yi=1, #level.bricks do for xi=1, #level.bricks[yi] do -- create brick? if(level.bricks[yi][xi] > 0) then local xPos local yPos if(level.align == "center") then --1100-((99*16)*0.5) xPos = display.contentCenterX- ((level.columns * brickSize.width) * 0.5/3) + ((xi-1) * level.xSpace)--display.contentCenterX --xPos = 300 +(xi * level.xSpace) yPos = 100 + (yi * level.ySpace)--100 else xPos = level.xStart + (xi * level.xSpace) yPos = level.yStart + (yi * level.ySpace) end local brickData = { x = xPos, y = yPos, brickType = level.bricks[yi][xi], index = activeBricksCount+1 } bricks[activeBricksCount+1] = CreateBrick(brickData) activeBricksCount = activeBricksCount + 1 end end end totalBricks = activeBricksCount totalBricksAtStart = activeBricksCount end -- create bricks for level --> set from above functions, change function to change brick build type local CreateAllBricks = CreateBricksFromTable -- called by a timer so I can pass arguments to CreateAllBricks local function CreateAllBricksTimerCall() CreateAllBricks(Levels.currentLevel) end -- remove all brick objects from memory local function ClearBricks() for i=1, #bricks do bricks[i] = nil end end -- stuff run on enterFrame event function Bricks:Update() -- update individual bricks if(totalBricksAtStart > 0) then for i=1, totalBricksAtStart do -- brick exists? if(bricks[i]) then bricks[i]:Update() end end end -- is level over? if(totalBricksBroken == totalBricks) then Events.allBricksBroken:Dispatch() end end ---------------- -- Events ---------------- function Bricks:allBricksBroken(event) -- cleanup bricks ClearBricks() local t = timer.performWithDelay( 1000, CreateAllBricksTimerCall) --CreateAllBricks() totalBricksBroken = 0 -- play happy sound for player to enjoy sound.play(sound.win) print("You Win!") end Events.allBricksBroken:AddObject(Bricks) CreateAllBricks(Levels.currentLevel) return Bricks 

Levels.lua

 local Events = require("Events") local Levels = {} local function MakeLevel(data) local level = {} level.xStart = data.xStart or 100 level.yStart = data.yStart or 100 level.xSpace = data.xSpace or 23 level.ySpace = data.ySpace or 23 level.align = data.align or "center" level.columns = data.columns or #data.bricks[1] level.bricks = data.bricks --> required return level end Levels.test4 = MakeLevel { bricks = { {0,2,0,0,2,0,0,2,0}, {0,0,2,0,2,0,2,0,0}, {0,0,0,0,2,0,0,0,0}, {1,1,2,1,1,1,2,1,1}, {0,0,0,0,1,0,0,0,0}, {0,0,0,0,1,0,0,0,0}, {0,0,0,0,1,0,0,0,0}, } } Levels.test5 = MakeLevel { bricks = { {0,0,0,1,0,0,0,0}, {0,0,1,0,1,0,0,0}, {0,0,1,0,1,0,0,0}, {0,1,0,0,0,1,0,0}, {0,1,1,1,1,1,0,0}, {1,0,0,0,0,0,1,0}, {1,0,0,0,0,0,1,0}, {1,0,0,0,0,0,1,0}, {1,0,0,0,0,0,1,0} } } -- Levels.test6 = MakeLevel2 -- { -- bricks = -- { ----A "a" = {{0,0,0,1,0,0,0,0}, -- {0,0,1,0,1,0,0,0}, -- {0,0,1,0,1,0,0,0}, -- {0,1,0,0,0,1,0,0}, -- {0,1,1,1,1,1,0,0}, -- {1,0,0,0,0,0,1,0}, -- {1,0,0,0,0,0,1,0}, -- {1,0,0,0,0,0,1,0}, -- {1,0,0,0,0,0,1,0}}, ----B -- "b" = {{1,1,1,1,0,0,0}, -- {1,0,0,0,1,0,0}, -- {1,0,0,0,1,0,0}, -- {1,0,0,0,1,0,0}, -- {1,1,1,1,0,0,0}, -- {1,0,0,0,1,0,0}, -- {1,0,0,0,0,1,0}, -- {1,0,0,0,0,1,0}, -- {1,1,1,1,1,0,0}}, --........... --....... --... -- --Z -- "z"= {{1,1,1,1,1,1,1,0}, -- {0,0,0,0,0,1,0,0}, -- {0,0,0,0,1,0,0,0}, -- {0,0,0,0,1,0,0,0}, -- {0,0,0,1,0,0,0,0}, -- {0,0,1,0,0,0,0,0}, -- {0,0,1,0,0,0,0,0}, -- {0,1,0,0,0,0,0,0}, -- {1,1,1,1,1,1,1,0}} -- } -- } -- stores all levels in ordered table so that one can be selected randomly by index Levels.levels = { --Levels.test4, Levels.test5 -- Levels.test6, } function Levels:GetRandomLevel() return self.levels[math.random(#Levels.levels)] end Levels.notPlayedYet = {} Levels.currentLevel = Levels:GetRandomLevel() -- Events function Levels:allBricksBroken(event) self.currentLevel = Levels:GetRandomLevel() end Events.allBricksBroken:AddObject(Levels) return Levels 

到目前为止我所做的工作(与上面相同)作为外部下载: http : //www.mediafire.com/download/1t89ftkbznkn184/Breakout2.rar

为了实际回答这个问题:

我不能100%确定你的意思是“我怎样才能join这些信件”,但是通过密码我有一个猜测,所以请澄清是否是准确的,或者如果我错了你想要的。

情况1

您还没有成功地获得截图中所示的图像 – 您可以绘制一个字母,但不能绘制多个字母。

在这种情况下,你需要更好地理解你的代码在做什么。 CreateBricksFromTable函数接受一个Level对象,该对象由MakeLevel函数从一个具有bricks属性的表中创build,该表是一个表格,其中包含表格中包含列的行,并显示每个位置应该有哪些types的砖块。 在注释级别中,您创build了一个表格,其中bricks字段包含每个字母的字段,但MakeLevel函数仍然期望bricks字段直接包含块的网格。 您将不得不 – 像您尝试的 – 创build一个MakeWordLevel函数(或类似的东西)来获取这个字母列表,并为每一行添加一个单词,并通过将适当的字母复制到一个更大的网格来构build一个更大的网格。

StackOverflow不是你的编程导师,而SO的问题不是让人们为你编写代码或逐步了解如何做的细节的正确论坛,但是我会给你留下一个基本的概要。 你的函数看起来像这样:

 local function MakeWordLevel(data, line1, line2) local level = {} ... return level end 

然后将不得不:

  • 填充所有与MakeLevel相同的属性
  • 计算该级别应该与所有字母的宽度( level.columns
  • 以与bricks属性相同的格式创build一个表格,但大到足以容纳所有字母
  • 通过inputstring(第1行和第line2 ),从现在的test6数组中find正确的字母数据,并将该数据复制到大表中
  • 将该表分配为level.bricks

这个问题已经超出了StackOverflow的意图 ,因为它提出了如何实现一个function,而不是实现一个小的,特定的编程任务,所以任何进一步的后续行动都应该在聊天室中进行 – 也许Hello World房间会有帮助。

情景2:

这是我原来的猜测,但经过考虑和阅读过去的编辑,我怀疑这是回答正确的问题

你可能需要一个固定的“背景”,例如红色的块,围绕你的字母,并把字段变成一个坚实的“墙”,名字用不同的颜色。 而且你可能会希望这些砖块一次慢慢地显示出来。

在这种情况下,你需要做的主要事情是跟踪哪些空间是由砖块“采取”的。 有很多方法可以做到这一点,但我会从一个matrix开始,跟踪最后的比赛场地 – 满了0。 然后,在为名称添加砖时,根据该块的坐标,在该matrix的x,y位置设置1。

当你想填充背景时,每次你在一个坐标上添加一个块时,在尝试添加一个块之前检查“已拍摄”matrix – 如果已经拍摄(1),那么只需跳过它并移动到下一个坐标。

如果您按顺序填充背景块(例如,从左到右,从上到下),或者想要随机添加背景块,则此function可用。 随机的,你也想不断更新“采取”matrix,所以你不要尝试添加一个块两次。

然而,随机填充会出现它自己的问题 – 它将继续花费更长的时间来填充,因为它会发现越来越多的“被占用”块,必须select一个新块。 当然有解决办法,但是当我不知道这是否是你想要的,我不会走得太远。

我不太了解你的代码(或者读这个代码),但是从我看到的将他们整理成完整的代码很容易。 你有两种可能性。

您可以将它们直接“渲染”到您的关卡/显示数据中,只需将它们复制到适当的位置,如下所示:

 -- The level data. local level = {} -- Create the level data. for row = 1, 25, 1 do local rowData = {} for column = 1, 80, 1 do rowData[column] = "." end level[row] = rowData end -- Now let us setup the letters. local letters = { A = { {".",".",".","#",".",".",".","."}, {".",".","#",".","#",".",".","."}, {".",".","#",".","#",".",".","."}, {".","#",".",".",".","#",".","."}, {".","#","#","#","#","#",".","."}, {"#",".",".",".",".",".","#","."}, {"#",".",".",".",".",".","#","."}, {"#",".",".",".",".",".","#","."}, {"#",".",".",".",".",".","#","."} }, B = { {"#","#","#","#",".",".","."}, {"#",".",".",".","#",".","."}, {"#",".",".",".","#",".","."}, {"#",".",".",".","#",".","."}, {"#","#","#","#",".",".","."}, {"#",".",".",".","#",".","."}, {"#",".",".",".",".","#","."}, {"#",".",".",".",".","#","."}, {"#","#","#","#","#",".","."} } } -- The string to print. local text = "ABBA" -- Let us insert the data into the level data. for index = 1, #text, 1 do local char = string.sub(text, index, index) local charData = letters[char] local offset = index * 7 for row = 1, 9, 1 do local rowData = charData[row] for column = 1, 7, 1 do level[row][offset + column] = rowData[column] end end end -- Print everything for row = 1, 25, 1 do local rowData = level[row] for column = 1, 80, 1 do io.write(rowData[column]) end print() end 

您可以将您的信件保存在查找表中,然后逐个将其复制到关卡数据中。 在这里,我用点和数字符号replace了数字,使它在命令行上更漂亮。

或者,您也可以将单词“渲染”到预备的缓冲区中,然后使用相同的逻辑将其插入到关卡数据中。