diff --git a/asteroids/main.lua b/asteroids/main.lua index 29fd96f..81d8882 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -1,57 +1,55 @@ local love = require("love") -function love.load() - arenaWidth = 800 - arenaHeight = 600 - shipRadius = 30 - - bulletTimerLimit = 0.5 - bulletRadius = 5 - - asteroidStages = { - { - speed = 120, - radius = 15, - }, - { - speed = 70, +local function initGame() + return { + width = 800, + height = 600, + ship = { radius = 30, + bullet = { + timerLimit = 0.5, + radius = 5, + } }, - { - speed = 50, - radius = 50, - }, - { - speed = 20, - radius = 80, + asteroids = { + stages = { + { speed = 120, radius = 15 }, + { speed = 70, radius = 30 }, + { speed = 50, radius = 50 }, + { speed = 20, radius = 80 }, + }, }, } +end +local game = initGame() + +function love.load() function reset() - shipX = arenaWidth / 2 - shipY = arenaHeight / 2 + shipX = game.width / 2 + shipY = game.height / 2 shipAngle = 0 shipSpeedX = 0 shipSpeedY = 0 bullets = {} - bulletTimer = bulletTimerLimit + bulletTimer = game.ship.bullet.timerLimit asteroids = { { x = 100, y = 100, }, { - x = arenaWidth - 100, + x = game.width - 100, y = 100, }, { - x = arenaWidth / 2, - y = arenaHeight - 100, + x = game.width / 2, + y = game.height - 100, } } - for asteroidIndex, asteroid in ipairs(asteroids) do + for _, asteroid in ipairs(asteroids) do asteroid.angle = love.math.random() * (2 * math.pi) - asteroid.stage = #asteroidStages + asteroid.stage = #game.asteroids.stages end end @@ -74,8 +72,8 @@ function love.update(dt) shipSpeedY = shipSpeedY + math.sin(shipAngle) * shipSpeed * dt end - shipX = (shipX + shipSpeedX * dt) % arenaWidth - shipY = (shipY + shipSpeedY * dt) % arenaHeight + shipX = (shipX + shipSpeedX * dt) % game.width + shipY = (shipY + shipSpeedY * dt) % game.height local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius) return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2 @@ -88,12 +86,12 @@ function love.update(dt) table.remove(bullets, bulletIndex) else local bulletSpeed = 500 - bullet.x = (bullet.x + math.cos(bullet.angle) * bulletSpeed * dt) % arenaWidth - bullet.y = (bullet.y + math.sin(bullet.angle) * bulletSpeed * dt) % arenaHeight + bullet.x = (bullet.x + math.cos(bullet.angle) * bulletSpeed * dt) % game.width + bullet.y = (bullet.y + math.sin(bullet.angle) * bulletSpeed * dt) % game.height end for asteroidIndex = #asteroids, 1, -1 do local asteroid = asteroids[asteroidIndex] - if areCirclesIntersecting(bullet.x, bullet.y, bulletRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then + if areCirclesIntersecting(bullet.x, bullet.y, game.ship.bullet.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then table.remove(bullets, bulletIndex) if asteroid.stage > 1 then local angle1 = love.math.random() * (2 * math.pi) @@ -119,11 +117,11 @@ function love.update(dt) bulletTimer = bulletTimer + dt if love.keyboard.isDown('s') then - if bulletTimer >= bulletTimerLimit then + if bulletTimer >= game.ship.bullet.timerLimit then bulletTimer = 0 table.insert(bullets, { - x = shipX + math.cos(shipAngle) * shipRadius, - y = shipY + math.sin(shipAngle) * shipRadius, + x = shipX + math.cos(shipAngle) * game.ship.radius, + y = shipY + math.sin(shipAngle) * game.ship.radius, angle = shipAngle, timeLeft = 4, }) @@ -131,10 +129,12 @@ function love.update(dt) end for asteroidIndex, asteroid in ipairs(asteroids) do - asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidStages[asteroid.stage].speed * dt) % arenaWidth - asteroid.y = (asteroid.y + math.sin(asteroid.angle) * asteroidStages[asteroid.stage].speed * dt) % arenaHeight + asteroid.x = (asteroid.x + math.cos(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) % + game.width + asteroid.y = (asteroid.y + math.sin(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) % + game.height - if areCirclesIntersecting(shipX, shipY, shipRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then + if areCirclesIntersecting(shipX, shipY, game.ship.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then reset() break end @@ -149,9 +149,9 @@ function love.draw() for y = -1, 1 do for x = -1, 1 do love.graphics.origin() - love.graphics.translate(x * arenaWidth, y * arenaHeight) + love.graphics.translate(x * game.width, y * game.height) love.graphics.setColor(0, 0, 1) - love.graphics.circle('fill', shipX, shipY, shipRadius) + love.graphics.circle('fill', shipX, shipY, game.ship.radius) local shipCircleDistance = 20 love.graphics.setColor(0, 1, 1) love.graphics.circle( @@ -161,14 +161,14 @@ function love.draw() 5 ) - for bulletIndex, bullet in ipairs(bullets) do + for _, bullet in ipairs(bullets) do love.graphics.setColor(0, 1, 0) - love.graphics.circle('fill', bullet.x, bullet.y, bulletRadius) + love.graphics.circle('fill', bullet.x, bullet.y, game.ship.bullet.radius) end - for asteroidIndex, asteroid in ipairs(asteroids) do + for _, asteroid in ipairs(asteroids) do love.graphics.setColor(1, 1, 0) - love.graphics.circle('fill', asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) + love.graphics.circle('fill', asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) end end end