如何在 love2d 中随机绘制一些东西
How to randomly draw something once in love2d
我目前正在尝试使用 love2d 创建打砖机,但问题是每当使用 math.random() 生成爱情应用程序保留的随机砖块时 运行 多次,砖块不断移动。
编辑:所以我想在特定列生成砖块,但行应该随机 selected。我的基本想法是执行 math.random(2) == 1 然后使用 for 循环绘制砖块,但问题是它每秒得到 updated/drawn 而砖块一直在 flickering/moving。我只是想在执行代码时随机(仅随机select y坐标我的x坐标是固定的)绘制一次但它一直在闪烁
我面临的问题 - https://youtu.be/AJB5vH7yfHc
我的代码
for y = 0, VIRTUAL_HEIGHT- 4, 10 do
if math.random(2) == 1 then
love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, y, 5, 10)
end
end
在love.load()
中生成块位置
--VIRTUAL_WIDTH, VIRTUAL_HEIGHT = love .graphics .getDimensions( )
block_pos = {} -- table to store block positions
rows, columns = 5, 8 -- you decide how many
chance_of_block = 75 -- % chance of placing a block
block_width = math .floor( VIRTUAL_WIDTH /columns )
block_height = math .floor( VIRTUAL_HEIGHT /rows )
for row = 0, rows -1 do
for col = 0, columns -1 do
if love .math .random() *100 <= chance_of_block then
local xpos = col *block_width
local ypos = row *block_height
local red = love .math .random()
local green = love .math .random()
local blue = love .math .random()
block_pos[ #block_pos +1 ] = { x = xpos, y = ypos, r = red, g = green, b = blue }
end -- rand
end -- #columns
end -- #rows
(已编辑:实现的行和列应从 0 索引开始,因此它们在屏幕上排列)
然后用love.draw()
画出来
for b = 1, #block_pos do
local block = block_pos[b]
love .graphics .setColor( block.r, block.g, block.b )
love .graphics .rectangle( 'fill', block.x, block.y, block_width, block_height )
end -- #block_pos
好的,在你编辑问题后,这听起来更像你在说什么。只需取出 #column 循环,并将其设置为在最后一列上创建单元格。
love.load()
--VIRTUAL_WIDTH, VIRTUAL_HEIGHT = love .graphics .getDimensions()
block_pos = {} -- table to store block positions
rows, columns = 30, 20 -- you decide how many
chance_of_block = 33 -- % chance of placing a block
block_width = math .floor( VIRTUAL_WIDTH /columns )
block_height = math .floor( VIRTUAL_HEIGHT /rows )
col = columns -1 -- don't loop through columns, just use final column
for row = 0, rows -1 do
if love .math .random() *100 <= chance_of_block then
local xpos = col *block_width
local ypos = row *block_height
block_pos[ #block_pos +1 ] = { x = xpos, y = ypos }
end -- rand
end -- #columns
r, g, b = 0.5, 0.5, 0.0
love .graphics .setColor( r, g, b )
love.draw()
for b = 1, #block_pos do
local block = block_pos[b]
love .graphics .rectangle( 'line', block.x, block.y, block_width, block_height )
end -- #block_pos
我目前正在尝试使用 love2d 创建打砖机,但问题是每当使用 math.random() 生成爱情应用程序保留的随机砖块时 运行 多次,砖块不断移动。
编辑:所以我想在特定列生成砖块,但行应该随机 selected。我的基本想法是执行 math.random(2) == 1 然后使用 for 循环绘制砖块,但问题是它每秒得到 updated/drawn 而砖块一直在 flickering/moving。我只是想在执行代码时随机(仅随机select y坐标我的x坐标是固定的)绘制一次但它一直在闪烁
我面临的问题 - https://youtu.be/AJB5vH7yfHc
我的代码
for y = 0, VIRTUAL_HEIGHT- 4, 10 do
if math.random(2) == 1 then
love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, y, 5, 10)
end
end
在love.load()
中生成块位置--VIRTUAL_WIDTH, VIRTUAL_HEIGHT = love .graphics .getDimensions( )
block_pos = {} -- table to store block positions
rows, columns = 5, 8 -- you decide how many
chance_of_block = 75 -- % chance of placing a block
block_width = math .floor( VIRTUAL_WIDTH /columns )
block_height = math .floor( VIRTUAL_HEIGHT /rows )
for row = 0, rows -1 do
for col = 0, columns -1 do
if love .math .random() *100 <= chance_of_block then
local xpos = col *block_width
local ypos = row *block_height
local red = love .math .random()
local green = love .math .random()
local blue = love .math .random()
block_pos[ #block_pos +1 ] = { x = xpos, y = ypos, r = red, g = green, b = blue }
end -- rand
end -- #columns
end -- #rows
(已编辑:实现的行和列应从 0 索引开始,因此它们在屏幕上排列)
然后用love.draw()
画出来for b = 1, #block_pos do
local block = block_pos[b]
love .graphics .setColor( block.r, block.g, block.b )
love .graphics .rectangle( 'fill', block.x, block.y, block_width, block_height )
end -- #block_pos
好的,在你编辑问题后,这听起来更像你在说什么。只需取出 #column 循环,并将其设置为在最后一列上创建单元格。
love.load()
--VIRTUAL_WIDTH, VIRTUAL_HEIGHT = love .graphics .getDimensions()
block_pos = {} -- table to store block positions
rows, columns = 30, 20 -- you decide how many
chance_of_block = 33 -- % chance of placing a block
block_width = math .floor( VIRTUAL_WIDTH /columns )
block_height = math .floor( VIRTUAL_HEIGHT /rows )
col = columns -1 -- don't loop through columns, just use final column
for row = 0, rows -1 do
if love .math .random() *100 <= chance_of_block then
local xpos = col *block_width
local ypos = row *block_height
block_pos[ #block_pos +1 ] = { x = xpos, y = ypos }
end -- rand
end -- #columns
r, g, b = 0.5, 0.5, 0.0
love .graphics .setColor( r, g, b )
love.draw()
for b = 1, #block_pos do
local block = block_pos[b]
love .graphics .rectangle( 'line', block.x, block.y, block_width, block_height )
end -- #block_pos