## NooB Problem

General discussion about LÖVE, Lua, game development, puns, and unicorns.
KevG
Prole
Posts: 2
Joined: Sun Aug 06, 2017 9:55 am

### NooB Problem

Hi guys / gals

Although i have had ' some ' programming experience I am a complete noob when it comes to game programming.

I have followed this tutorial https://www.love2d.org/wiki/Tutorial:Gridlocked_Player.

Everything works fine.

The problem I have is that i want to allow the player to move whilst holding down a key rather than having to keep pressing a key, so i looked at this tutorial https://www.love2d.org/wiki/Tutorial:Hamster_Ball

and decided to try and adapt the Keypress routine.

The code works within reason apart from

Player is moving to fast now
when player gets near the map edge it seems to be too slow
Also if you hold down both up and left or up and right ( and the same for the down key ) the player tends too ' shoot ' through the inner map walls.

I know i haven't made the map any larger but I don't understand why when holding the 2 keys together the player ' shoots ' through the inner walls
I know the speed thing will be to do with it being in the update but i cant figure out how to slow it down enough.

Hope you can point me in the right direction.

Here is the full code with my adaption

Code: Select all

function love.load()

player = {
grid_x = 256,
grid_y = 256,
act_x = 200,
act_y = 200,
speed = 5
}

map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}
end

function love.update(dt) -- dt stands for delta time.

player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)

if love.keyboard.isDown("up") then
if testMap(0, -1) then
player.grid_y = player.grid_y - 32
end
elseif love.keyboard.isDown("down") then
if testMap(0, 1) then
player.grid_y = player.grid_y + 32
end
elseif love.keyboard.isDown("left") then
if testMap(-1, 0) then
player.grid_x = player.grid_x - 32
end
elseif love.keyboard.isDown("right") then
if testMap(1, 0) then
player.grid_x = player.grid_x + 32
end
end

end

function love.draw()

love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)

-- draw map

for y=1, #map do
for x=1, #map[y] do
if map[y][x] == 1 then
love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
end
end
end
end

--[[function love.keypressed(key)
if key == "up" then
if testMap(0, -1) then
player.grid_y = player.grid_y - 32
end
elseif key == "down" then
if testMap(0, 1) then
player.grid_y = player.grid_y + 32
end
elseif key == "left" then
if testMap(-1, 0) then
player.grid_x = player.grid_x - 32
end
elseif key == "right" then
if testMap(1, 0) then
player.grid_x = player.grid_x + 32
end
end
end ]]

function testMap(x, y)
if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
return false
end
return true
end

Мэтю
Prole
Posts: 31
Joined: Mon Jan 06, 2014 1:24 pm
Location: Espírito Santo, Brazil
Contact:

### Re: NooB Problem

It's too fast because the speed love.update is called. It's very fast, here in my computer the time between each call is about to 0.017 ("rounded"), so that's much more than 1 time per second, so your player is going to walk 32 units everytime update is called, then it will be very fast. To get rid of this, you can use a timer, so after an amout of time passed, the player is allowed to move.

Code: Select all

local timer = 0 -- It goes somewhere outside before love.update
local wait_time = 0.3 -- The time to wait between each "player walk"

... -- Stuff

function love.update(dt)
timer = timer + dt -- Increasing the counter
...  -- Stuff
if timer >= wait_time then
timer = 0 -- Reset the timer, so it needs to count again to the next check
... -- Check if keyboard was pressed
end
end

... -- Stuff
Edit your code, so you can have a better ideia of it C:

Hope it helps
Attachments
main.lua
World needs love.

KevG
Prole
Posts: 2
Joined: Sun Aug 06, 2017 9:55 am

### Re: NooB Problem

Thank you Мэтю for taking the time to reply.

sorry i am late getting back but normal life crap gets in the way

Once again thankyou

### Who is online

Users browsing this forum: No registered users and 22 guests