CS50 Games Track: Марио - PullRequest
       9

CS50 Games Track: Марио

1 голос
/ 29 мая 2020

Я застрял на этом около двух недель, и у меня все еще есть мотивация завершить это, потому что я так близок к концу курса!

Я смог сгенерировать пирамиды и правильно помечает и гарантирует, что грибы или кусты не растут за моей пирамидой до конца уровня.

Однако часть, которую я не могу понять, находится здесь:

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

Я поместил код, генерирующий пирамиду и флаг, после всех остальных элементов. Однако кажется, что если условие в приведенном выше блоке кода выполняется (math.random (10) == 1), последняя часть моей карты будет одним большим пробелом, а пирамида и флаг исчезнут.

Поскольку logi c 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

Спасибо за помощь!

...