将Corona SDK中的图块加入一个单词来代表Breakout游戏网格?


77

我有一个游戏项目可以重新实现Breakout。我想显示两个单词,每个单词一行。它们由砖块连接。在内部,第一行是名字,左对齐。底行是姓氏,右对齐。它们是从文本框输入的,并显示如下:

如上所述

每过一秒钟,屏幕将在网格中添加可配置数量的积木(例如,每秒五块),直到两个单词看起来完整为止。我显示了从矩阵(0,1)创建的字母。

...但是我不知道如何将它们组合成一个词。我如何加入这些信件?

到目前为止,这是我得到的:

砖块

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



11
Google告诉我此页面上有“播放快乐的声音供玩家欣赏”,所以我来看看-20000个空格在哪里?
techolic


8
如果您来自Google,那么您可能位于meta.meta.stackoverflow.com/questions/328376/…
Grant Holliday

4
请告诉我您如何在StackOverflow帖子中获得20000个空格!
rubik '16

Answers:


4

为了真正回答这个问题:

我不确定100%表示“如何加入这些字母”是什么意思,但是通过仔细阅读代码,我有一个猜测,因此请澄清它是否准确,或者我对您想要的内容是否有错。

场景1

您尚未成功实现屏幕截图中显示的图像-能够绘制一个字母,但不能绘制多个字母。

在这种情况下,您需要更好地了解您的代码在做什么。该CreateBricksFromTable函数接收一个Level对象,该对象由该MakeLevel函数从具有bricks属性的表中创建,该表是具有表的表,该表表示其中具有列的行,显示在每个位置应使用哪种类型的砖。在注释掉的级别上,您创建了一个表,其中该bricks字段包含每个字母的字段,但是该MakeLevel函数仍希望该bricks字段直接包含块网格。您将不得不-看起来像您尝试过的-创建一个MakeWordLevel函数(或类似函数),该函数接受此字母列表,并为每行输入一个单词,并通过将适当的字母复制到其中来构造更大的网格。

StackOverflow不是您的编程辅导员,因此SO问题也不适合让人们为您编写代码或逐步了解如何执行此操作的正确论坛,但我将为您提供基本的概述。您的函数如下所示:

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

然后必须:

  • 填写所有的属性都相同MakeLevel
  • 计算level.columns所有字母的水平宽度()
  • 以与bricks属性相同的格式创建一个表,但其大小足以容纳所有字母
  • 遍历输入字符串(line1line2),从现在的test6数组中找到正确的字母数据,然后将该数据复制到大表中
  • 将该表分配为 level.bricks

这个问题已经超出了StackOverflow的预期范围,因为它询问如何实现功能而不是完成小的特定编程任务,因此任何进一步的跟进都应该在聊天室中进行-也许Hello World会议室可以会有所帮助。

方案2:

这是我最初的猜测,但是在考虑并阅读了以往的编辑后,我怀疑这是否在回答正确的问题

您可能需要一个坚实的“背景”,例如红色的块,将字母围起来,并使该字段成为一个坚实的“墙”,并使用不同的颜色命名。您可能希望这些积木一次慢慢出现。

在这种情况下,您需要做的主要事情就是跟踪名称砖“占用”了哪些空间。有很多方法可以做到这一点,但我将从一个矩阵开始进行跟踪,该矩阵与最终的比赛场地一样大,全为0。然后,在为名称添加积木时,根据该块的坐标在该矩阵的x,y位置设置1。

当您要填充背景时,每次您要在坐标处添加一个块时,请在尝试添加一个块之前检查“已摄取”矩阵-如果已使用(1),则跳过它并移至下一个坐标。

如果您要顺序填充背景块(例如,从左到右,从上到下),或者要随机添加它们,则此方法有效。使用random时,您还希望继续更新“ taken”矩阵,因此您不会尝试两次添加块。

但是,随机填充存​​在其自身的问题-它将继续花费更长的时间来填充,因为它将发现越来越多的“已拾取”块,并且必须选择一个新的块。当然,有解决方案,但是当我不知道那是否是您想要的东西时,我不会走太远。


2

我不太了解(或阅读)您的代码,但是从我看来,将它们组合成完整的单词很容易。您有两种可能性。

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

-- 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

您可以将字母保存在查找表中,然后将它们逐段复制到级别数据中。在这里,我用点和数字符号替换了数字,以使其在命令行上更漂亮。

另外,您也可以将单词“渲染”到准备好的缓冲区中,然后使用相同的逻辑将其插入到级别数据中。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.