lovexp/main.lua

176 lines
5.4 KiB
Lua
Raw Normal View History

2025-11-12 21:31:35 -05:00
local love = require("love")
function love.load()
arenaWidth = 800
arenaHeight = 600
2025-11-12 21:35:42 -05:00
shipRadius = 30
2025-11-14 16:22:01 -05:00
bulletTimerLimit = 0.5
2025-11-14 16:50:50 -05:00
bulletRadius = 5
2025-11-14 16:33:24 -05:00
2025-11-14 17:10:40 -05:00
asteroidStages = {
2025-11-14 16:33:24 -05:00
{
2025-11-14 17:10:40 -05:00
speed = 120,
radius = 15,
2025-11-14 16:33:24 -05:00
},
{
2025-11-14 17:10:40 -05:00
speed = 70,
radius = 30,
2025-11-14 16:33:24 -05:00
},
{
2025-11-14 17:10:40 -05:00
speed = 50,
radius = 50,
},
{
speed = 20,
radius = 80,
},
2025-11-14 16:33:24 -05:00
}
2025-11-14 17:10:40 -05:00
function reset()
shipX = arenaWidth / 2
shipY = arenaHeight / 2
shipAngle = 0
shipSpeedX = 0
shipSpeedY = 0
bullets = {}
bulletTimer = bulletTimerLimit
asteroids = {
{
x = 100,
y = 100,
},
{
x = arenaWidth - 100,
y = 100,
},
{
x = arenaWidth / 2,
y = arenaHeight - 100,
}
}
for asteroidIndex, asteroid in ipairs(asteroids) do
asteroid.angle = love.math.random() * (2 * math.pi)
asteroid.stage = #asteroidStages
end
2025-11-14 16:33:24 -05:00
end
2025-11-14 17:10:40 -05:00
reset()
2025-11-12 21:35:42 -05:00
end
2025-11-12 21:31:35 -05:00
function love.update(dt)
local turnSpeed = 10
if love.keyboard.isDown('right') then
shipAngle = shipAngle + turnSpeed * dt
end
if love.keyboard.isDown('left') then
shipAngle = shipAngle - turnSpeed * dt
end
shipAngle = shipAngle % (2 * math.pi)
if love.keyboard.isDown('up') then
local shipSpeed = 100
shipSpeedX = shipSpeedX + math.cos(shipAngle) * shipSpeed * dt
shipSpeedY = shipSpeedY + math.sin(shipAngle) * shipSpeed * dt
end
shipX = (shipX + shipSpeedX * dt) % arenaWidth
shipY = (shipY + shipSpeedY * dt) % arenaHeight
2025-11-13 16:29:57 -05:00
2025-11-14 16:50:50 -05:00
local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius)
return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2
end
2025-11-13 16:35:51 -05:00
for bulletIndex = #bullets, 1, -1 do
local bullet = bullets[bulletIndex]
bullet.timeLeft = bullet.timeLeft - dt
if bullet.timeLeft <= 0 then
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
end
2025-11-14 16:50:50 -05:00
for asteroidIndex = #asteroids, 1, -1 do
local asteroid = asteroids[asteroidIndex]
2025-11-14 17:10:40 -05:00
if areCirclesIntersecting(bullet.x, bullet.y, bulletRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then
2025-11-14 16:50:50 -05:00
table.remove(bullets, bulletIndex)
2025-11-14 17:10:40 -05:00
if asteroid.stage > 1 then
local angle1 = love.math.random() * (2 * math.pi)
local angle2 = (angle1 - math.pi) % (2 * math.pi)
table.insert(asteroids, {
x = asteroid.x,
y = asteroid.y,
angle = angle1,
stage = asteroid.stage - 1,
})
table.insert(asteroids, {
x = asteroid.x,
y = asteroid.y,
angle = angle2,
stage = asteroid.stage - 1,
})
end
2025-11-14 16:50:50 -05:00
table.remove(asteroids, asteroidIndex)
break
end
end
2025-11-13 16:29:57 -05:00
end
2025-11-14 16:22:01 -05:00
bulletTimer = bulletTimer + dt
if love.keyboard.isDown('s') then
if bulletTimer >= bulletTimerLimit then
bulletTimer = 0
table.insert(bullets, {
x = shipX + math.cos(shipAngle) * shipRadius,
y = shipY + math.sin(shipAngle) * shipRadius,
angle = shipAngle,
timeLeft = 4,
})
end
2025-11-13 16:29:57 -05:00
end
2025-11-14 16:33:24 -05:00
for asteroidIndex, asteroid in ipairs(asteroids) do
2025-11-14 17:10:40 -05:00
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
2025-11-14 16:41:04 -05:00
2025-11-14 17:10:40 -05:00
if areCirclesIntersecting(shipX, shipY, shipRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then
reset()
2025-11-14 16:41:04 -05:00
break
end
2025-11-14 16:33:24 -05:00
end
2025-11-14 17:10:40 -05:00
if #asteroids == 0 then
reset()
end
2025-11-12 21:31:35 -05:00
end
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.setColor(0, 0, 1)
2025-11-12 21:35:42 -05:00
love.graphics.circle('fill', shipX, shipY, shipRadius)
2025-11-12 21:31:35 -05:00
local shipCircleDistance = 20
love.graphics.setColor(0, 1, 1)
love.graphics.circle(
'fill',
shipX + math.cos(shipAngle) * shipCircleDistance,
shipY + math.sin(shipAngle) * shipCircleDistance,
5
)
2025-11-12 21:35:42 -05:00
for bulletIndex, bullet in ipairs(bullets) do
love.graphics.setColor(0, 1, 0)
2025-11-14 17:10:40 -05:00
love.graphics.circle('fill', bullet.x, bullet.y, bulletRadius)
2025-11-12 21:35:42 -05:00
end
2025-11-14 16:33:24 -05:00
for asteroidIndex, asteroid in ipairs(asteroids) do
love.graphics.setColor(1, 1, 0)
2025-11-14 17:10:40 -05:00
love.graphics.circle('fill', asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius)
2025-11-14 16:33:24 -05:00
end
2025-11-12 21:31:35 -05:00
end
end
end