math.randomseed(os.time()) local cron = require "lib.cron" local lg = love.graphics local function Enemy(enemyType) self = {} self.x = lg.getWidth() + 10 if enemyType == 'top' then self.y = lg.getHeight() * 1/5 elseif enemyType == 'bottom' then self.y = lg.getHeight() * 4/5 elseif enemyType == 'random' then self.y = math.random(10, lg.getHeight() - 10) end self.w = 20 self.h = 20 self.color = {255, 0, 0} self.update = function(self, dt) self.x = self.x - 960/3 * dt end return self end local enemies = {} table.insert(enemies, Enemy('top')) table.insert(enemies, Enemy('bottom')) local newEnemies = cron.every(1, function() table.insert(enemies, Enemy('top')) table.insert(enemies, Enemy('bottom')) end) local randEnemies local function randEneesfs() for i=0,math.random(2) do table.insert(enemies, Enemy('random')) end randEnemies = cron.after(math.random(5, 22) / 2, randEneesfs) end randEnemies = cron.after(3.2, randEneesfs) function love.update(dt) newEnemies:update(dt) randEnemies:update(dt) for i=1,#enemies do if enemies[i].update then if enemies[i]:update(dt) then --enemies[i] = nil --table.remove(enemies, i) end end end end function love.draw() for i=1,#enemies do lg.setColor(enemies[i].color) lg.rectangle("fill", enemies[i].x - enemies[i].w / 2, enemies[i].y - enemies[i].h / 2, enemies[i].w, enemies[i].h) end end function love.keypressed(key, unicode) if key == 'escape' then love.event.quit() end end