My Very First Love2d Program

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

My Very First Love2d Program

Post by kingnoob »

Rat In A Cage

Code: Select all

function love.load()
   love.window.setFullscreen(true, "desktop")
   width, height = love.window.getDesktopDimensions(1)
   circle = {}
   circle.x = width/2
   circle.y = height/2
   circle.dx = 0
   circle.dy = 0
   circle.radius = 20
end

function love.update(dt)
    down = love.keyboard.isDown("escape")
    if down == true then
       love.event.quit() 
    end
    circle.dx = circle.dx + love.math.random() * 3 - 1.5
    circle.dy = circle.dy + love.math.random() * 3 - 1.5
    if circle.dx > 10 then circle.dx = 10 end
    if circle.dx < -10 then circle.dx = -10 end
    if circle.dy > 10 then circle.dy = 10 end
    if circle.dy < -10 then circle.dy = -10 end
    circle.x = circle.x + circle.dx 
    circle.y = circle.y + circle.dy 
    if circle.x > width - circle.radius then 
        circle.x = width - circle.radius 
        circle.dx = 0
    end
    if circle.x < circle.radius then
        circle.x = circle.radius
        circle.dx = 0   
    end
    if circle.y > height - circle.radius then
         circle.y = height - circle.radius
         circle.dy = 0
    end
    if circle.y < circle.radius then
         circle.y = circle.radius
         circle.dy = 0
    end
end

function love.draw()
    local r = circle.x / width
    local g = circle.y / height
    local b = (circle.x * circle.y) / (height * width)
    love.graphics.setColor(r, g, b)
    love.graphics.circle("fill", circle.x, circle.y, circle.radius, 64)
end
Pseudo 3D

Code: Select all

function love.load()
   love.window.setFullscreen(true, "desktop")
   width, height = love.window.getDesktopDimensions(1)
   circle = {}
   circle.x = width/2
   circle.y = height/2
   circle.z = 500
   circle.dx = 0
   circle.dy = 0
   circle.dz = 0
   circle.radius = 20
end

function love.update(dt)
    down = love.keyboard.isDown("escape")
    if down == true then
       love.event.quit() 
    end
    circle.dx = circle.dx + love.math.random() * 3 - 1.5
    circle.dy = circle.dy + love.math.random() * 3 - 1.5
    circle.dz = circle.dz + love.math.random() * 3 - 1.5
    if circle.dx > 10 then circle.dx = 10 end
    if circle.dx < -10 then circle.dx = -10 end
    if circle.dy > 10 then circle.dy = 10 end
    if circle.dy < -10 then circle.dy = -10 end
    if circle.dz > 10 then circle.dz = 10 end
    if circle.dz < -10 then circle.dz = -10 end
    circle.x = circle.x + circle.dx 
    circle.y = circle.y + circle.dy 
    circle.z = circle.z + circle.dz
    if circle.x > width - circle.radius then 
        circle.x = width - circle.radius 
        circle.dx = 0
    end
    if circle.x < circle.radius then
        circle.x = circle.radius
        circle.dx = 0   
    end
    if circle.y > height - circle.radius then
         circle.y = height - circle.radius
         circle.dy = 0
    end
    if circle.y < circle.radius then
         circle.y = circle.radius
         circle.dy = 0
    end
    if circle.z > 1000 then
         circle.z = 1000
         circle.dz = 0
    end
    if circle.z < 0 then
         circle.z = 0
         circle.dz = 0
    end
end

function love.draw()
    local r = circle.x / width
    local g = circle.y / height
    local b = circle.z / 1000
    circle.radius = circle.z / 50 + 10
    love.graphics.setColor(r, g, b)
    love.graphics.circle("fill", circle.x, circle.y, circle.radius, 64)
end
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: My Very First Love2d Program

Post by milon »

Pretty good for a first go at it!

Quick tip - the first 4 lines in love.update(dt) can be changed to one line:

Code: Select all

if love.keyboard.isDown("escape") then love.event.quit() end
You don't necessarily need to store the result of love.keyboard.isDown("escape") in a variable, and you don't need to compare it to true (since the function already returns true/false). There are other optimizations that can be made, of course, but this one is pretty easy to do. :)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: My Very First Love2d Program

Post by kingnoob »

milon wrote: Thu Dec 21, 2023 6:38 pm Pretty good for a first go at it!

Quick tip - the first 4 lines in love.update(dt) can be changed to one line:

Code: Select all

if love.keyboard.isDown("escape") then love.event.quit() end
Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests