Lua CS50游戏轨道:马里奥
我在这方面已经坚持了大约两个星期了,我仍然有动力完成这项工作,因为我已经接近课程的结尾了 我已经能够正确生成金字塔和旗帜,并确保没有蘑菇或灌木经过我的金字塔生成到关卡的末尾 然而,我似乎无法理解的部分是:Lua CS50游戏轨道:马里奥,lua,cs50,Lua,Cs50,我在这方面已经坚持了大约两个星期了,我仍然有动力完成这项工作,因为我已经接近课程的结尾了 我已经能够正确生成金字塔和旗帜,并确保没有蘑菇或灌木经过我的金字塔生成到关卡的末尾 然而,我似乎无法理解的部分是: -- 10% chance to not generate anything, creating a gap elseif math.random(10) ~= 1 then -- creates column of tiles going to bottom of m
-- 10% chance to not generate anything, creating a gap
elseif math.random(10) ~= 1 then
-- creates column of tiles going to bottom of map
for y = self.mapHeight / 2, self.mapHeight do
self:setTile(x, y, TILE_BRICK)
end
-- chance to create a block for Mario to hit
if math.random(15) == 1 and x < self.mapWidth - 10 then
self:setTile(x, self.mapHeight / 2 - 4, JUMP_BLOCK)
end
-- next vertical scan line
x = x + 1
else
-- increment X so we skip two scanlines, creating a 2-tile gap
x = x + 2
end
——有10%的机会不产生任何结果,造成差距
其他数学。随机(10)~=1
--创建位于地图底部的一列平铺
对于y=self.mapHeight/2,self.mapHeight do
self:setTile(x,y,TILE_-BRICK)
结束
--有机会为马里奥创造一个拦网
如果math.random(15)==1且x
我将生成金字塔和标志的代码放在所有其他实体之后。然而,如果上面代码块中的条件得到满足(math.random(10)=1),我的地图的最后一部分将只是一个大缺口,金字塔和标志将全部消失
由于math.random的逻辑在创建间隙的情况下被翻转,如何防止间隙生成超过任意点(例如,x-- begin generating the terrain using vertical scan lines
local x = 1
while x < self.mapWidth do
-- 2% chance to generate a cloud
-- make sure we're 2 tiles from edge at least
if x < self.mapWidth - 2 then
if math.random(20) == 1 then
-- choose a random vertical spot above where blocks/pipes generate
local cloudStart = math.random(self.mapHeight / 2 - 6)
self:setTile(x, cloudStart, CLOUD_LEFT)
self:setTile(x + 1, cloudStart, CLOUD_RIGHT)
end
end
-- 5% chance to generate a mushroom
if math.random(20) == 1 and x < self.mapWidth - 10 then
-- left side of pipe
self:setTile(x, self.mapHeight / 2 - 2, MUSHROOM_TOP)
self:setTile(x, self.mapHeight / 2 - 1, MUSHROOM_BOTTOM)
-- creates column of tiles going to bottom of map
for y = self.mapHeight / 2, self.mapHeight do
self:setTile(x, y, TILE_BRICK)
end
-- next vertical scan line
x = x + 1
-- 10% chance to generate bush, being sure to generate away from edge
elseif math.random(10) == 1 and x < self.mapWidth - 10 then
local bushLevel = self.mapHeight / 2 - 1
-- place bush component and then column of bricks
self:setTile(x, bushLevel, BUSH_LEFT)
for y = self.mapHeight / 2, self.mapHeight do
self:setTile(x, y, TILE_BRICK)
end
x = x + 1
self:setTile(x, bushLevel, BUSH_RIGHT)
for y = self.mapHeight / 2, self.mapHeight do
self:setTile(x, y, TILE_BRICK)
end
x = x + 1
-- 10% chance to not generate anything, creating a gap
elseif math.random(10) ~= 1 then
-- creates column of tiles going to bottom of map
for y = self.mapHeight / 2, self.mapHeight do
self:setTile(x, y, TILE_BRICK)
end
-- chance to create a block for Mario to hit
if math.random(15) == 1 and x < self.mapWidth - 10 then
self:setTile(x, self.mapHeight / 2 - 4, JUMP_BLOCK)
end
-- next vertical scan line
x = x + 1
else
-- increment X so we skip two scanlines, creating a 2-tile gap
x = x + 2
end
-- generate pyramid starting at least 7 blocks away from right edge
if x == self.mapWidth - 7 then
for j = 1, 4, 1 do
self:setTile(x, self.mapHeight / 2 - j, TILE_BRICK)
-- creates column of tiles going to bottom of map
for y = self.mapHeight / 2 - (j - 1), self.mapHeight / 2 - 1 do
self:setTile(x, y, TILE_BRICK)
end
x = x + 1
-- ensure nothing is generated between pyramid and flag
for y = 0, self.mapHeight / 2 - 1 do
self:setTile(x, y, TILE_EMPTY)
end
for x = self.mapWidth - 7, self.mapWidth do
for y = self.mapHeight / 2, self.mapHeight do
self:setTile(x, y, TILE_BRICK)
end
end
end
end
——开始使用垂直扫描线生成地形
局部x=1
而x
谢谢你的帮助 这也是我一段时间以来一直在努力解决的问题。我理解这个问题的最好方法是 仅当x变量达到
self.mapWidth-7
时才会触发级别结束程序生成,因为对于x=1,self.mapWidth do级别的程序生成不是以的形式进行的,因此每次加载贴图时都会达到x==self.mapWidth-7
为了解决这个问题,我在Map.lua文件中,把level编码序列的末尾放在前面提到的部分之后
-- first, fill map with empty tiles
for y = 1, self.mapHeight do
for x = 1, self.mapWidth do
-- support for multiple sheets per tile; storing tiles as tables
self:setTile(x, y, TILE_EMPTY)
end
end
已开始在公式中进行编码,以创建与self.mapWidth末端适当距离的棱锥体和旗杆
-- create the end of level terrain
-- make local variables for ease of editing
local PFront = self.mapWidth - 22
local PEnd = self.mapWidth - 15
local PTop = self.mapHeight / 2 - 1
local PBottom = self.mapHeight / 2 - 1
for x = PFront, PEnd, 1 do
for y = PBottom, PTop, -1 do
self:setTile(x, y, TILE_BRICK)
end
PTop = PTop - 1
end
-- Generates a uniform ground for the final section
for x = self.mapWidth - 22, self.mapWidth do
for y = self.mapHeight / 2, self.mapHeight do
self:setTile(x, y, TILE_BRICK)
end
end
-- Flag pole with declared top and bottom with iterated shaft
for y = self.mapHeight / 2 - 7, self.mapHeight / 2 - 2 do
self:setTile(self.mapWidth - 6, y, MIDDLE_POLE)
end
self:setTile(self.mapWidth - 6, self.mapHeight / 2 - 8, TOP_POLE)
self:setTile(self.mapWidth - 6, self.mapHeight / 2 - 1, BOTTOM_POLE)
然后在所有这些之后继续到这段时间。。。从类代码中执行循环公式
-- begin generating the terrain using vertical scan lines
x = 1
while x <= self.mapWidth - 23 do
...the rest of the Map.lua file
——开始使用垂直扫描线生成地形