想改善爱情2D代码的性能
我正在制作一个平台游戏,我使用以下代码生成关卡:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我印象深刻的是,您在 X 和 Y 上运行两个嵌套 for 循环只是为了绘制几条线,然后使用范围检查检查这些点是否在线上,以最终放置图块;唯一似乎随机的是图块类型,而不是图块位置。您应该存储线条列表,并使用随机图块在屏幕上绘制每条线:
固定的天空/尖峰/房屋/宝石图块可以受益于相同的技术;您必须为每条线存储一个线图块类型并使用它而不是随机图块。不过,这些似乎是静态的 - 为什么您不能重复使用旧关卡并使用所描述的“画线”技术仅替换随机图块?
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:
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?