I'm making an Agar.io clone and struggling with something

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
KokoSeal
Prole
Posts: 1
Joined: Sat Sep 03, 2022 6:25 pm

I'm making an Agar.io clone and struggling with something

Post by KokoSeal »

Hey people,
How can I make the player go diagonally?
When I'm holding 2 keys at once only the first one pressed is functioning.
Ignore my bad coding skills I'm still learning and doing my best, if you have any tips for feel free to share them :awesome:
this is my code:

Code: Select all

function love.load()
    player = {}
    baseCircleRadius = 20
    score = 0
    player.radius = baseCircleRadius + (score/3)
    player.x = love.graphics.getWidth()/2
    player.y = love.graphics.getHeight()/2
    player.speed = 200 - player.radius + 100
end

function love.update(dt)
    if love.keyboard.isDown('d') then player.x = player.x + (player.speed * dt)
    elseif love.keyboard.isDown('a') then player.x = player.x - (player.speed * dt)
    elseif love.keyboard.isDown('w') then player.y = player.y - (player.speed * dt)
    elseif love.keyboard.isDown('s') then player.y = player.y + (player.speed * dt)
    end
end

function love.draw()
    love.graphics.setColor(0, 1, 1)
    love.graphics.circle("fill", player.x, player.y, player.radius)
end
How can I make the player go diagonally?
When I'm holding 2 keys at once only the first one pressed is functioning.
Ignore my bad coding skills I'm still learning and doing my best, if you have any tips for feel free to share them :awesome:
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: I'm making an Agar.io clone and struggling with something

Post by BrotSagtMist »

In quick: Instead of an elseif you simply use an if.
In detail, handling keys properly isnt something you can do in 4 lines, this is fine for the beginning, once your game is finished, come back at it.
obey
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: I'm making an Agar.io clone and struggling with something

Post by darkfrei »

Code: Select all

local dx,dy = 0, 0
if d and not a then dx = 1 end
if a and not d then dx = -1 end
if s and not w then dy = 1 end
if w and not s then dy = -1 end 
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
RNavega
Party member
Posts: 235
Joined: Sun Aug 16, 2020 1:28 pm

Re: I'm making an Agar.io clone and struggling with something

Post by RNavega »

The reasoning behind what darkfrei showed is that you need to think more like physics work: every frame, the player moves by their speed.
Wherever that speed points to, and whatever strength that speed has, it doesn't matter. Every frame the player moves by it.

You have player.speed, which is the maximum speed that the player can reach.
You also need two new variables: horizontal speed and vertical speed.

If the right key is pressed, set the horizontal speed to its positive value.
If the left key is pressed, set it to the negative value.
Else (when no horizontal keys are pressed), set that horizontal speed to zero.

Same for the vertical keys and the vertical speed.

On love.update(), move the player by whatever are the contents of the horizontal and vertical speeds. Each can either be positive, negative or zero.

PS if both the horizontal and vertical speeds are different than zero then you need to 'normalize' them, else you have that subtle but annoying problem where the player moves faster diagonally than only horizontally or only vertically.
The player moves faster in this case because the length of the 2D vector formed by the horizontal and vertical speeds is bigger than player.speed.
The horizontal and vertical speeds are 2D vectors too, it's just that they're aligned to the screen axes. When moving both horizontally and vertically, you're actually moving by the combination of these two vectors, and the result of this is that diagonal vector whose length is bigger than the max speed.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: I'm making an Agar.io clone and struggling with something

Post by darkfrei »

Code: Select all

 -- love.update 
local dx,dy = 0, 0
local d = love.keyboard.isScancodeDown("d")
local a = love.keyboard.isScancodeDown("a")
local s = love.keyboard.isScancodeDown("s")
local w = love.keyboard.isScancodeDown("w")
if d and not a then dx = 1 elseif a and not d then dx = -1 end
if s and not w then dy = 1 elseif w and not s then dy = -1 end 

local velocity = 100
local k = (math.abs(dx) +math.abs(dy)) == 2 and 0.7071 or 1
local vx = dx*k*velocity
local vy = dy*k*velocity
x = x + dt*vx
y = y + dt*vy
Or you can change the acceleration, same as velocity in this system.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests