想改善爱情2D代码的性能

发布于 2025-01-19 10:07:49 字数 5839 浏览 4 评论 0原文

我正在制作一个平台游戏,我使用以下代码生成关卡:

function LevelMaker.generateLevel3(width, height)
    local tiles = {}
    local objects = {}
    local entities = {}

    for y = 1, height do
        table.insert(tiles, {})
        for x = 1, width do
            if y == 9 and (x > 5 and x < 12) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 12) and (x > 11 and x < 30) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 3) and (x == 13) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 17) and (x > 15 and x < 28) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 9) and (x > 31 and x < 35) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 6) and (x > 25 and x < 29) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y > 1 and y < 7) and (x == 26) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 0) and (x > 3 and x < 10) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 13) and (x > 22 and x < 30) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = 2,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y == 3)  and (x == 12) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = 4,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y == 2 or y == 4) and (x == 13) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = y == 2 and 1 or 2,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y == 8) and (x == 10 or x == 11) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = 1,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y > 1 and y < 7) and (x == 25) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                GameObject{
                    texture = 'spikes',
                    x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                    width = TILE_SIZE, height = TILE_SIZE,
                    frame = 4,
                    collidable = true,
                    solid = true,
                    deadly = true
                }
            )
            elseif (y == 8) and (x == 34) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                GameObject{
                    texture = 'spikes',
                    x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                    width = TILE_SIZE, height = TILE_SIZE,
                    frame = 1,
                    collidable = true,
                    solid = true,
                    deadly = true
                }
            )
            elseif (y == 1) and (x > 3 and x < 10) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                GameObject{
                    texture = 'spikes',
                    x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                    width = TILE_SIZE, height = TILE_SIZE,
                    frame = 2,
                    collidable = true,
                    solid = true,
                    deadly = true
                }
            )
            else
                table.insert(tiles[y], Tile(x, y, SKY))
            end
        end
    end

    table.insert(objects,
        GameObject{
            texture = 'house',
            x = (16 - 1) * TILE_SIZE, y = (13 - 1) * TILE_SIZE,
            width = 36, height = 44,
            frame = 1,
        }
    )

    table.insert(objects,
        GameObject{
            texture = 'gems',
            x = (28 - 1) * TILE_SIZE, y = (5 - 1) * TILE_SIZE,
            width = 15, height = 11,
            frame = {1,2,3,4,5},
        }
    )

    local map = TileMap(width, height)
    map.tiles = tiles

    return GameLevel(entities, objects, map)
end

有没有办法改进上述相同的代码(提高时间复杂度) 当玩家在 1-2 秒内反复死亡时,游戏就会冻结。 我想尽快加载关卡

I am making a platformer where i am generating level with below code:

function LevelMaker.generateLevel3(width, height)
    local tiles = {}
    local objects = {}
    local entities = {}

    for y = 1, height do
        table.insert(tiles, {})
        for x = 1, width do
            if y == 9 and (x > 5 and x < 12) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 12) and (x > 11 and x < 30) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 3) and (x == 13) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 17) and (x > 15 and x < 28) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 9) and (x > 31 and x < 35) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 6) and (x > 25 and x < 29) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y > 1 and y < 7) and (x == 26) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 0) and (x > 3 and x < 10) then
                table.insert(tiles[y], Tile(x, y, math.random(1, 3)))
            elseif (y == 13) and (x > 22 and x < 30) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = 2,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y == 3)  and (x == 12) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = 4,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y == 2 or y == 4) and (x == 13) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = y == 2 and 1 or 2,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y == 8) and (x == 10 or x == 11) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                    GameObject{
                        texture = 'spikes',
                        x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                        width = TILE_SIZE, height = TILE_SIZE,
                        frame = 1,
                        collidable = true,
                        solid = true,
                        deadly = true
                    }
                )
            elseif (y > 1 and y < 7) and (x == 25) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                GameObject{
                    texture = 'spikes',
                    x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                    width = TILE_SIZE, height = TILE_SIZE,
                    frame = 4,
                    collidable = true,
                    solid = true,
                    deadly = true
                }
            )
            elseif (y == 8) and (x == 34) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                GameObject{
                    texture = 'spikes',
                    x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                    width = TILE_SIZE, height = TILE_SIZE,
                    frame = 1,
                    collidable = true,
                    solid = true,
                    deadly = true
                }
            )
            elseif (y == 1) and (x > 3 and x < 10) then
                table.insert(tiles[y], Tile(x, y, SKY))
                table.insert(objects, 
                GameObject{
                    texture = 'spikes',
                    x = (x - 1) * TILE_SIZE, y = (y - 1) * TILE_SIZE,
                    width = TILE_SIZE, height = TILE_SIZE,
                    frame = 2,
                    collidable = true,
                    solid = true,
                    deadly = true
                }
            )
            else
                table.insert(tiles[y], Tile(x, y, SKY))
            end
        end
    end

    table.insert(objects,
        GameObject{
            texture = 'house',
            x = (16 - 1) * TILE_SIZE, y = (13 - 1) * TILE_SIZE,
            width = 36, height = 44,
            frame = 1,
        }
    )

    table.insert(objects,
        GameObject{
            texture = 'gems',
            x = (28 - 1) * TILE_SIZE, y = (5 - 1) * TILE_SIZE,
            width = 15, height = 11,
            frame = {1,2,3,4,5},
        }
    )

    local map = TileMap(width, height)
    map.tiles = tiles

    return GameLevel(entities, objects, map)
end

Is there a way to improve the same above code (improve time complexity)
When the player dies over and over within 1-2 sec the game freezes.
I want to load the level as fast as possible

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2025-01-26 10:07:49

让我印象深刻的是,您在 X 和 Y 上运行两个嵌套 for 循环只是为了绘制几条线,然后使用范围检查检查这些点是否在线上,以最终放置图块;唯一似乎随机的是图块类型,而不是图块位置。您应该存储线条列表,并使用随机图块在屏幕上绘制每条线:

for y = 1, height do -- prepare the grid
        table.insert(tiles, {})
end
local lines = {{from = 6, to = 11, y = 9}, {from = ..., to = ...}, {...}, ...}
for _, line in pairs(lines) do -- draw lines
        if line.y then
                for x = line.from, line.to do
                        table.insert(tiles[line.y], Tile(x, line.y, math.random(1, 3)))
                end
        elseif line.x then
                for y = line.from, line.to do
                        table.insert(tiles[y], Tile(line.x, y, math.random(1, 3)))
                end
        end
end

固定的天空/尖峰/房屋/宝石图块可以受益于相同的技术;您必须为每条线存储一个线图块类型并使用它而不是随机图块。不过,这些似乎是静态的 - 为什么您不能重复使用旧关卡并使用所描述的“画线”技术仅替换随机图块?

What sticks out to me is that you're running two nested for loops over X and Y just to draw a couple lines, then checking whether the points are on the line using range checks to eventually place tiles; the only thing that seems to be randomized is the tile type, but not the tile position. You should store a list of lines and draw each line on the screen with randomized tiles:

for y = 1, height do -- prepare the grid
        table.insert(tiles, {})
end
local lines = {{from = 6, to = 11, y = 9}, {from = ..., to = ...}, {...}, ...}
for _, line in pairs(lines) do -- draw lines
        if line.y then
                for x = line.from, line.to do
                        table.insert(tiles[line.y], Tile(x, line.y, math.random(1, 3)))
                end
        elseif line.x then
                for y = line.from, line.to do
                        table.insert(tiles[y], Tile(line.x, y, math.random(1, 3)))
                end
        end
end

the fixed sky/spikes/house/gems tiles can benefit from the same technique; you'll have to store a line tile type with each line and use it instead of the random tile though. These seem to be static though - why can't you just reuse the old level and replace only the randomized tiles using the described "line drawing" technique?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文