Started My Very First Love2d Game

Show off your games, demos and other (playable) creations.
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Started My Very First Love2d Game

Post by kingnoob »

It is going to be a very simple Space Conquest Game. I'll be open to suggestions along the way. All I got done so far is some initialization, drawing of stars and ship numbers. And one can press 'n' for a new start. Basically one will send ships from one's own stars to another star in range. There will be gui popup wickets like Send Ships etc. Anyway here is the code so far. Press escape to exit.

Edit: Oh yeah, there are 12 players. Those are the filled in circles. The outline only stars are neutral.

colors.lua

Code: Select all

color = {{}}
color[1] = { 30/255, 144/255, 255/255 }
color[2] = { 255/255, 0, 0 }
color[3] = { 199/255, 21/255, 133/255 }
color[4] = { 255/255, 165/255, 0 }
color[5] = { 255/255, 255/255, 0 }
color[6] = { 100/255, 0, 100/255 } 
color[7] = { 0, 128/255, 0 }
color[8] = { 0, 255/255, 0 }
color[9] = { 0, 255/255, 255/255 }
color[10] = { 80/255, 80/255, 0 }
color[11] = { 210/255, 180/255, 140/255 }
color[12] = { 139/255, 69/255, 19/255 }
color[13] = { 128/255, 128/255, 128/255 }
main.lua

Code: Select all

require("colors")

function love.load()
	love.window.setFullscreen(true, "desktop")
    width, height = love.window.getDesktopDimensions(1)
    love.math.setRandomSeed(love.timer.getTime())
    star = {}
    for i = 1, 200 do
        star[i] = {}
    end
    player = {}
    InitStars()
    InitPlayers()
end

function love.update(dt)
    if love.keyboard.isDown("escape") then love.event.quit() end
    if love.keyboard.isDown("n") then 
        InitStars()
        InitPlayers()
    end
    BuildShips()
end

function love.draw()
    DrawStars()
    DrawShips()
end

function BuildShips()
    for i = 1, 200 do
        r = love.math.random(1000)
        if star[i].owner == 13 then
            if r / 4 < star[i].size then
                star[i].ships = star[i].ships + 1
            end
        else
            if r / 8 < star[i].size then
                star[i].ships = star[i].ships + 1
            end
        end   
    end
end

function InitStars() 
    for i = 1, 200 do
        ::try_again::
        star[i].x = love.math.random(20, width - 20)
        star[i].y = love.math.random(20, height - 30)
        for j = 1, i do
            if j ~= i then
                if Distance(star[i].x, star[j].x, star[i].y, star[j].y) < 60 then
                    goto try_again
                end
            end
        end
        star[i].size = love.math.random(5) + love.math.random(5) + love.math.random(5)
        star[i].owner = 13
        star[i].ships = 10
    end
end

function InitPlayers()
    ::IP2::
    for i = 1, 12 do
        ::IP1::
        player[i] = love.math.random(200)
        for j = 1, i do
            if j ~= i then
                if player[i] == player[j] then
                    goto IP1
                end
            end
        end
        for j = 1, i do
            if j ~= i then
                x1 = star[player[i]].x
                x2 = star[player[j]].x
                y1 = star[player[i]].y 
                y2 = star[player[j]].y 
                if Distance(x1,x2,y1,y2) < 320 then
                    goto IP2
                end
            end
        end
    end
    for i = 1, 12 do 
        star[player[i]].owner = i
        star[player[i]].size = 10
        star[player[i]].ships = 100
    end
end

function DrawStars()
    for i = 1, 200 do
        if star[i].owner == 13 then
            love.graphics.setColor(color[star[i].size - 2])
            love.graphics.circle("line", star[i].x, star[i].y, star[i].size + 3)
            love.graphics.circle("line", star[i].x, star[i].y, star[i].size + 2)
        else
            love.graphics.setColor(color[star[i].owner])
            love.graphics.circle("fill", star[i].x, star[i].y, star[i].size + 3)
        end
    end
end

function DrawShips()
    love.graphics.setColor(0.8, 0.8, 0.8)
    for i = 1, 200 do
        x = star[i].x - 4
        y = star[i].y + star[i].size + 4
        love.graphics.printf(star[i].ships, x - 46, y, 100, "center")
    end
end

function Distance(x1, x2, y1, y2) 
    return math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2))
end
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: Started My Very First Love2d Game

Post by kingnoob »

Took a moment to update the file structure.
This way I need only post the files that change.
main

Code: Select all

require("colors")
require("initiate")
require("draw")
require("update")
require("utils")

function love.load()
	love.window.setFullscreen(true, "desktop")
    width, height = love.window.getDesktopDimensions(1)
    love.math.setRandomSeed(love.timer.getTime())
    star = {}
    for i = 1, 200 do
        star[i] = {}
    end
    player = {}
    InitStars()
    InitPlayers()
end

function love.update(dt)
    if love.keyboard.isDown("escape") then love.event.quit() end
    if love.keyboard.isDown("n") then 
        InitStars()
        InitPlayers()
    end
    BuildShips()
end

function love.draw()
    DrawStars()
    DrawShips()
end
colors

Code: Select all

color = {{}}
color[1] = { 30/255, 144/255, 255/255 }
color[2] = { 255/255, 0, 0 }
color[3] = { 199/255, 21/255, 133/255 }
color[4] = { 255/255, 165/255, 0 }
color[5] = { 255/255, 255/255, 0 }
color[6] = { 100/255, 0, 100/255 } 
color[7] = { 0, 128/255, 0 }
color[8] = { 0, 255/255, 0 }
color[9] = { 0, 255/255, 255/255 }
color[10] = { 80/255, 80/255, 0 }
color[11] = { 210/255, 180/255, 140/255 }
color[12] = { 139/255, 69/255, 19/255 }
color[13] = { 128/255, 128/255, 128/255 }
draw

Code: Select all

function DrawStars()
    for i = 1, 200 do
        if star[i].owner == 13 then
            love.graphics.setColor(color[star[i].size - 2])
            love.graphics.circle("line", star[i].x, star[i].y, star[i].size + 3)
            love.graphics.circle("line", star[i].x, star[i].y, star[i].size + 2)
        else
            love.graphics.setColor(color[star[i].owner])
            love.graphics.circle("fill", star[i].x, star[i].y, star[i].size + 3)
        end
    end
end

function DrawShips()
    love.graphics.setColor(0.8, 0.8, 0.8)
    for i = 1, 200 do
        x = star[i].x - 4
        y = star[i].y + star[i].size + 4
        love.graphics.printf(star[i].ships, x - 46, y, 100, "center")
    end
end
initiate

Code: Select all

function InitStars() 
    for i = 1, 200 do
        ::try_again::
        star[i].x = love.math.random(20, width - 20)
        star[i].y = love.math.random(20, height - 30)
        for j = 1, i do
            if j ~= i then
                if Distance(star[i].x, star[j].x, star[i].y, star[j].y) < 60 then
                    goto try_again
                end
            end
        end
        star[i].size = love.math.random(5) + love.math.random(5) + love.math.random(5)
        star[i].owner = 13
        star[i].ships = 10
    end
end

function InitPlayers()
    ::IP2::
    for i = 1, 12 do
        ::IP1::
        player[i] = love.math.random(200)
        for j = 1, i do
            if j ~= i then
                if player[i] == player[j] then
                    goto IP1
                end
            end
        end
        for j = 1, i do
            if j ~= i then
                x1 = star[player[i]].x
                x2 = star[player[j]].x
                y1 = star[player[i]].y 
                y2 = star[player[j]].y 
                if Distance(x1,x2,y1,y2) < 320 then
                    goto IP2
                end
            end
        end
    end
    for i = 1, 12 do 
        star[player[i]].owner = i
        star[player[i]].size = 10
        star[player[i]].ships = 100
    end
end
update

Code: Select all

function BuildShips()
    for i = 1, 200 do
        r = love.math.random(1000)
        if star[i].owner == 13 then
            if r / 4 < star[i].size then
                star[i].ships = star[i].ships + 1
            end
        else
            if r / 8 < star[i].size then
                star[i].ships = star[i].ships + 1
            end
        end   
    end
end
utils

Code: Select all

function Distance(x1, x2, y1, y2) 
    return math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2))
end
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: Started My Very First Love2d Game

Post by kingnoob »

I'm definitely up past my bedtime but I couldn't go to sleep until I cleaned up the code.

main.lua

Code: Select all

require("colors")
require("globals")
require("initiate")
require("draw")
require("update")
require("utils")

function love.load()
    Initiate()
end

function love.update(dt)
    Update()
end

function love.draw()
    Draw()
end
colors.lua

Code: Select all

color = {{}}
color[1] = { 30/255, 144/255, 255/255 }
color[2] = { 255/255, 0, 0 }
color[3] = { 199/255, 21/255, 133/255 }
color[4] = { 255/255, 165/255, 0 }
color[5] = { 255/255, 255/255, 0 }
color[6] = { 100/255, 0, 100/255 } 
color[7] = { 0, 128/255, 0 }
color[8] = { 0, 255/255, 0 }
color[9] = { 0, 255/255, 255/255 }
color[10] = { 80/255, 80/255, 0 }
color[11] = { 210/255, 180/255, 140/255 }
color[12] = { 139/255, 69/255, 19/255 }
color[13] = { 128/255, 128/255, 128/255 }
draw.lua

Code: Select all

function Draw()
    DrawStars()
    DrawShips()
end

function DrawStars()
    for i = 1, 200 do
        if star[i].owner == 13 then
            love.graphics.setColor(color[star[i].size - 2])
            love.graphics.circle("line", star[i].x, star[i].y, star[i].size + 3)
            love.graphics.circle("line", star[i].x, star[i].y, star[i].size + 2)
        else
            love.graphics.setColor(color[star[i].owner])
            love.graphics.circle("fill", star[i].x, star[i].y, star[i].size + 3)
        end
    end
end

function DrawShips()
    love.graphics.setColor(0.8, 0.8, 0.8)
    for i = 1, 200 do
        x = star[i].x - 4
        y = star[i].y + star[i].size + 4
        love.graphics.printf(star[i].ships, x - 46, y, 100, "center")
    end
end
globals.lua

Code: Select all

width, height = love.window.getDesktopDimensions(1)
star = {}
for i = 1, 200 do
    star[i] = {}
end
player = {}
initiate.lua

Code: Select all

function Initiate()
    love.window.setFullscreen(true, "desktop")
    love.math.setRandomSeed(love.timer.getTime())
    InitStars()
    InitPlayers()
end

function InitStars() 
    for i = 1, 200 do
        ::try_again::
        star[i].x = love.math.random(20, width - 20)
        star[i].y = love.math.random(20, height - 30)
        for j = 1, i do
            if j ~= i then
                if Distance(i, j) < 60 then
                    goto try_again
                end
            end
        end
        star[i].size = love.math.random(5) + love.math.random(5) + love.math.random(5)
        star[i].owner = 13
        star[i].ships = 10
    end
end

function InitPlayers()
    ::IP2::
    for i = 1, 12 do
        ::IP1::
        player[i] = love.math.random(200)
        for j = 1, i do
            if j ~= i then
                if player[i] == player[j] then
                    goto IP1
                end
            end
        end
        for j = 1, i do
            if j ~= i then
                if Distance(player[i], player[j]) < 320 then
                    goto IP2
                end
            end
        end
    end
    for i = 1, 12 do 
        star[player[i]].owner = i
        star[player[i]].size = 10
        star[player[i]].ships = 100
    end
end
update.lua

Code: Select all

function Update()
    if love.keyboard.isDown("escape") then love.event.quit() end
    if love.keyboard.isDown("n") then 
        Initiate()
    end
    BuildShips()
end

function BuildShips()
    for i = 1, 200 do
        r = love.math.random(1000)
        if star[i].owner == 13 then
            if r / 4 < star[i].size then
                star[i].ships = star[i].ships + 1
            end
        else
            if r / 8 < star[i].size then
                star[i].ships = star[i].ships + 1
            end
        end   
    end
end
utils.lua

Code: Select all

function Distance(i, j)
    return math.sqrt((star[i].x - star[j].x) ^ 2 + (star[i].y - star[j].y) ^ 2)
end
gn
User avatar
Bobble68
Party member
Posts: 160
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: Started My Very First Love2d Game

Post by Bobble68 »

Nicely done! Two bits of feedback for it though:

1) It's generally best to share your projects on the forums in a .love file so others don't need to reconstruct your game themselves (It's just a .zip file with the extension changed).

2) You might want to consider using a HSL to choose the colours of the stars - it's a little more complicated than the setup you have currently, but it will give you a spectrum of colours to work with rather than a table of 13

Here's an example you can use if you want:

Code: Select all

function getColor(hue, saturation, value)
  hue = hue % 360
  saturation = math.min(saturation, 1)
  value = math.min(value, 1)
  
  local c = value * saturation
  local x = c * (1 - math.abs((hue/60)%2 - 1))
  local m = value - c
  
  
  local ra, ga, ba
  
  if hue < 60 then
    ra, ga, ba = c, x, 0
  elseif hue < 120 then
    ra, ga, ba = x, c, 0
  elseif hue < 180 then
    ra, ga, ba = 0, c, x
  elseif hue < 240 then
    ra, ga, ba = 0, x, c
  elseif hue < 300 then
    ra, ga, ba = x, 0, c
  else 
    ra, ga, ba = c, 0, x
  end
  
  return ra + m, ga + m, ba + m
  
end

local hue = 0

function love.update(dt)
  
  hue = hue + dt*60
  
end

function love.draw()
  
  love.graphics.setBackgroundColor(getColor(hue, 0.5, 2))
  
end
Dragon
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: Started My Very First Love2d Game

Post by kingnoob »

Every star will have a name. That is what I am working on now. So if anyone wants a star named after them they need to supply working code. :cool:

Thanks Bobble68, That is a bit over my head for now. Looks a bit complicated for my Very Simple Space Game.

But one question, please. Does tying it to dt vary the brightness of a star? Maybe when/if there are better graphics for the stars that would be cool to do for a random star every so often?
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: Started My Very First Love2d Game

Post by kingnoob »

Okay, so here is attached the .love file. The code is slightly further along. A mouseover of a star will pop up a panel with some information. The blue star will be named Sol (working on that now). Sol is the human's home star. Mouse over the closest stars to the blue star to see if they are in range or not. It is not much for now, just a simple demo of the gui style.
Attachments
VerySimpleSpaceGame.love
Change .love to .zip
(3.18 KiB) Downloaded 65 times
User avatar
Bobble68
Party member
Posts: 160
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: Started My Very First Love2d Game

Post by Bobble68 »

kingnoob wrote: Sun Dec 24, 2023 5:36 pm Every star will have a name. That is what I am working on now. So if anyone wants a star named after them they need to supply working code. :cool:

Thanks Bobble68, That is a bit over my head for now. Looks a bit complicated for my Very Simple Space Game.

But one question, please. Does tying it to dt vary the brightness of a star? Maybe when/if there are better graphics for the stars that would be cool to do for a random star every so often?
Don't worry I barely understand the function myself, but I know what it does.
The way it works is that is that hue is what colour it is, saturation is how much of the colour is used, and value is how bright it is. Since colour can be made into a circle, hue is often done with degrees of a circle, so for instance, 0°, 0, 0 is pure black, while 60°, 1, 1 is pure yellow. Varying the value will change its brightness, but not its saturation or colour.
Dragon
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: Started My Very First Love2d Game

Post by kingnoob »

This was a long day. But happily I was able to solve a programming dilemma with the help of ChatGPT. How to move an object across the screen from (x1, y1) to (x2, y2) at a consistent speed. And I added temporary demo code to see if it works.

Code: Select all

--added to globals.lua
fleet = {}
for i = 1, 1000 do
  fleet[i] = {}
end
fleetTop = 1

Code: Select all

--added to update.lua
function Update()
    Keyboard()
    Mouse()
    BuildShips()
    CreateRandomFleet() --added
    UpdateFleets()           --added
end

function CreateRandomFleet()
    if love.math.random(100) == 1 then
        i = fleetTop
        r = love.math.random(12)
        org = player[r]
        dst = player[love.math.random(12)]
        if org ~= dst then
            x1 = star[org].x
            y1 = star[org].y
            x2 = star[dst].x
            y2 = star[dst].y
            dist = math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
            fleet[i].dx = (x2 - x1) / dist
            fleet[i].dy = (y2 - y1) / dist
            fleet[i].owner = r
            fleet[i].x1 = x1
            fleet[i].y1 = y1
            fleet[i].x2 = x2
            fleet[i].y2 = y2
            fleetTop = fleetTop + 1
        end
    end
end

function UpdateFleets() 
    local i = 1
    while i < fleetTop do
        x1 = fleet[i].x1
        y1 = fleet[i].y1
        x2 = fleet[i].x2
        y2 = fleet[i].y2
        dx = fleet[i].dx
        dy = fleet[i].dy
        d = math.sqrt(dx ^ 2 + dy ^ 2)
        x1 = x1 + dx
        y1 = y1 + dy
        if math.abs(x2 - x1) < 1 and math.abs(y2 - y1) < 1 then
            fleetTop = fleetTop - 1
            fleet[i] = fleet[fleetTop]
            fleet[fleetTop] = {}
        else
            fleet[i].x1 = x1
            fleet[i].y1 = y1
        end
        i = i + 1    
    end
end

Code: Select all

--added to draw.lua
function DrawFleets()
    if fleetTop > 1 then
        for i = 1, fleetTop - 1 do
            love.graphics.setColor(color[fleet[i].owner])
            love.graphics.circle("line", fleet[i].x1, fleet[i].y1, 3)
            love.graphics.circle("line", fleet[i].x1, fleet[i].y1, 4)
        end
    end
end
:)
Attachments
VerySimpleSpaceGame.zip
simple fleet movement demo
(3.57 KiB) Downloaded 51 times
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: Started My Very First Love2d Game

Post by kingnoob »

This is a very simple explanation of the gui system. Maybe this is common. Or maybe it is new. Idk. It is a craft by hand system but it would be nice to have a visual designer for it that will write the functions for us.

My Update function is very simple

Code: Select all

function Update()
    Keyboard()
    Mouse()
    UpdateLine() --a line from the origin to the destination
    BuildShips()
    CreateRandomFleet() --just for demo
    UpdateFleets()
    UpdateSend() --this is the actuator code for the send ships panel


Very Simple mouse update code

Code: Select all

function Mouse()
    mx, my = love.mouse.getPosition()
    function love.mousepressed(x, y, b) 
        mbpx = x
        mbpy = y 
        mbpb = b
    end
    function love.mousereleased(x, y, b)
        mbrx = x
        mbry = y 
        mbrb = b
    end
end
Very simple draw function

Code: Select all

function Draw()
    DrawStars()
    DrawFleets()
    DrawShips()
    DrawGui()
end

function DrawGui()
    DrawLine()
    DrawInfo()
    DrawSend() --Draws the send ships panel
end

function DrawSend()
    if sendFleet then
        if lox < width / 2 then gx = lox + 24 else gx = lox - 504 end
        if loy > 540 then gy = loy - 400 else gy = loy end 
        love.graphics.setColor(SteelBlue)
        love.graphics.rectangle("fill", gx, gy, 480, 400, 12, 12)
        love.graphics.setColor(DarkSteelBlue)
        love.graphics.rectangle("fill", gx + 416, gy + 4, 60, 60, 12, 12)
        love.graphics.setColor(SteelBlue)
        love.graphics.setLineWidth(4)
        love.graphics.line(gx + 428, gy + 16, gx + 464, gy + 52)
        love.graphics.line(gx + 464, gy + 16, gx + 428, gy + 52)
        love.graphics.setLineWidth(2)
    end
end
The send ships panel for demonstration purposes has only one actuator right now and that is exit panel.

Code: Select all

function UpdateSend()
  if not sendFleet then return end
  if mbrb then
      if mbrx >= gx + 416 and mbrx <= gx + 476 and mbry >= gy + 4 and mbry <= gy + 64 then
          sendFleet = false
          mouseOver = true
      end 
  end
end
If one downloads the attachment to see how it works then click on the medium blue star and then on another star.
The send ships panel will come up and the exit actuator is the only gui element active while the send panel is active.
Attachments
VerySimpleSpaceGame.love
change .love to .zip
(4.19 KiB) Downloaded 50 times
User avatar
Bobble68
Party member
Posts: 160
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: Started My Very First Love2d Game

Post by Bobble68 »

Something seems to be up with the way you've packaged your game - normally if you have Love2D installed, you should be able to run the .love file directly. I could only do that once I unzipped it and repackaged it myself.

What method are you using for this?

Other than that, seems to be coming along nicely! My other piece of feedback is that you seem to be using a good deal of global variables, rather than local ones. I'm not one of those people who thinks globals should be illegal, but I will say they should be used sparingly - locals are a little faster, and are less likely to cause you issues down the line. If you have an issue that affects one of your globals, it will be much more difficult to track it down, especially if it's the result of a typo.
Dragon
Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests