Coin = {}
function Coin:load()
self.width = 20
self.height = 20
self.speed = 200
self.x = love.math.random(0, love.graphics.getWidth() - self.width)
self.y = love.math.random(0, love.graphics.getHeight() - self.height)
end
function Coin:update(dt)
end
function Coin:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end
So here is my current code, and if u run this code, u can just see a white square generating randomly on random pos on the screen. My goal here is to try and make multiple generating objects at random spot and then disappearing.
If u can, please help me out, I'm quite new to Lua and coding.
How can I make randomly generated multiple objects spawn on the screen?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 1
- Joined: Sat Oct 09, 2021 9:05 am
Re: How can I make randomly generated multiple objects spawn on the screen?
Have fun with it,
The thing I'd like to point out is,
I didn't go the coin.update(), coin.draw route
Instead I just put some vanilla objects in a table
The thing I'd like to point out is,
I didn't go the coin.update(), coin.draw route
Instead I just put some vanilla objects in a table
Code: Select all
local rnd = love.math.random
function love.keypressed(key)
if key=='escape' then love.event.quit() end
end
function love.load()
coins = {}
local num_coins = 40
for i = 1, num_coins do
table.insert(
coins,
{
x = rnd()*800,
y = rnd()*600,
size = 10 + rnd() * 20,
rotate_timer = rnd(),
rotate_speed = 2 + rnd()*8,
alpha_timer = rnd()*2,
color = {rnd(), rnd(), rnd()}
}
)
end
end
function love.update(dt)
for i = 1, #coins do
coins[i].rotate_timer = coins[i].rotate_timer + dt * coins[i].rotate_speed
coins[i].alpha_timer = coins[i].alpha_timer + dt
coins[i].alpha = math.abs(math.sin(coins[i].alpha_timer))
if (math.abs(coins[i].alpha) < 0.01 ) then
coins[i].x = rnd()*800
coins[i].y = rnd()*600
end
end
end
function love.draw()
for i = 1, #coins do
local c = coins[i]
love.graphics.setColor(c.color[1],c.color[2], c.color[3], c.alpha)
love.graphics.ellipse(
'fill',
c.x, c.y,
math.abs(math.sin(c.rotate_timer)) * c.size, c.size)
end
end
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests