How can I make the player move a certain ammount of pixels?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

How can I make the player move a certain ammount of pixels?

Post by MichaelShort »

So what i'm trying to do is make a frogger game and make the frog jump 1/11 of the screen. So below I have an example but the jump length is not measured in pixels. If anyone could help me that'd be great me and a friend are making this for a school project, thanks.

player = {x = 1, y = 1, jumpLength = 40000, playerImg = love.graphics.newImage('frog.png')}
player.y = player.y - (player.jumpLength * dt)
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: How can I make the player move a certain ammount of pixe

Post by CanadianGamer »

Do you need the frog to jump in an arc or can it be a linear movement?
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: How can I make the player move a certain ammount of pixe

Post by Ulydev »

What you've got there
MichaelShort wrote:player.y = player.y - (player.jumpLength * dt)
Will move the player by jumpLength pixels per second.
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

Re: How can I make the player move a certain ammount of pixe

Post by MichaelShort »

CanadianGamer wrote:Do you need the frog to jump in an arc or can it be a linear movement?
Id like it to be linear movement.
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

Re: How can I make the player move a certain ammount of pixe

Post by MichaelShort »

Ulydev wrote:What you've got there
MichaelShort wrote:player.y = player.y - (player.jumpLength * dt)
Will move the player by jumpLength pixels per second.
So it will move it 40,000 pixels? When me and my friend do it on are screen it doesn't even go are full height of the screen (1080). Are we missing something here?
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: How can I make the player move a certain ammount of pixe

Post by CanadianGamer »

It might help if you posted all the code so that we can get a little more context.
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

Re: How can I make the player move a certain ammount of pixe

Post by MichaelShort »

CanadianGamer wrote:It might help if you posted all the code so that we can get a little more context.
Frog is 64x64 pixels.

Code: Select all

player = {x = 1, y = 1, jumpLength = 40000, playerImg = love.graphics.newImage('frog.png')}
jump = true
jumpMax = 0.4
jumpTimer = jumpMax
play = false
sound = love.audio.newSource("Frogger - Main.mp3")
--[[
frog width: 64
frog height: 64
screen height: 1031
screen width: 1920
--]]
function love.load()

end

function love.update(dt)
	sound:setVolume(0.2)
	sound:play()
	jumpTimer = jumpTimer - (1 * dt)
	if jumpTimer < 0 then
		jump = true
	end

	if love.keyboard.isDown('escape') then
		love.event.push('quit')
	end

	if love.keyboard.isDown('return') then
		play = true
	end

	if love.keyboard.isDown('w') and jump and play then
		player.y = player.y - (player.jumpLength * dt)
		jump = false
		jumpTimer = jumpMax
	end

	if love.keyboard.isDown('s') and jump and play then
		player.y = player.y + (player.jumpLength * dt)
		jump = false
		jumpTimer = jumpMax
	end

	if love.keyboard.isDown('a') and jump and play then
		player.x = player.x - (player.jumpLength * dt)
		jump = false
		jumpTimer = jumpMax
	end

	if love.keyboard.isDown('d') and jump and play then
		player.x = player.x + (player.jumpLength * dt)
		jump = false
		jumpTimer = jumpMax
	end
end

function love.draw()
	if play == true then
		love.graphics.setColor(0, 255, 0)
    	love.graphics.draw(player.playerImg, player.x, player.y)
    else
    	if play == false then
    		love.graphics.setFont(love.graphics.newFont(20))
    		love.graphics.setColor(0, 191, 255)
    		love.graphics.print("Press enter/return to start!", love.graphics:getWidth()/2-100, love.graphics:getHeight()/2)
    	end
    end
end
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: How can I make the player move a certain ammount of pixe

Post by MadByte »

Okay, it looks like you want to create a Frogger clone?
If so you don't have to use love.update to update the movement (if you dont want smooth movement). Instead try to use the keypressed callback. And then to move the frog just update its x / y position by adding a static value (the jumpDistance).

Here is a small example:

Code: Select all

love.window.setTitle("Frogger")
love.window.setMode(512, 512, {vsync = false})
WIDTH, HEIGHT = love.graphics.getDimensions()


-- Create the Frog --
local frog = {}
frog.w = 64
frog.h = 64
frog.x = 0
frog.y = 192
frog.jumpDistance = 64

function frog.move(dx, dy)
  frog.x = frog.x + dx
  frog.y = frog.y + dy
end

function frog.draw()
  love.graphics.setColor(0, 255, 0)
  love.graphics.rectangle("fill", frog.x, frog.y, frog.w, frog.h)
  love.graphics.setColor(255, 255, 255)
end

function frog.keypressed(key)
  if key == "w" and frog.y > 0 then frog.move(0, -frog.jumpDistance)
  elseif key == "s" and frog.y < HEIGHT - frog.h then frog.move(0, frog.jumpDistance) end
  if key == "a" and frog.x > 0 then frog.move(-frog.jumpDistance, 0)
  elseif key == "d" and frog.x < WIDTH - frog.w then frog.move(frog.jumpDistance, 0) end
end


-- Main callbacks --
function love.draw()
  frog.draw()
  love.graphics.print("ESC to Quit")
end


function love.keypressed(key)
  frog.keypressed(key)
  if key == "escape" then love.event.quit() end
end
Another way would be to create a tile based level or a grid to move the frog from cell to cell.
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

Re: How can I make the player move a certain ammount of pixe

Post by MichaelShort »

MadByte wrote:Okay, it looks like you want to create a Frogger clone?
If so you don't have to use love.update to update the movement (if you dont want smooth movement). Instead try to use the keypressed callback. And then to move the frog just update its x / y position by adding a static value (the jumpDistance).

Here is a small example:

Code: Select all

love.window.setTitle("Frogger")
love.window.setMode(512, 512, {vsync = false})
WIDTH, HEIGHT = love.graphics.getDimensions()


-- Create the Frog --
local frog = {}
frog.w = 64
frog.h = 64
frog.x = 0
frog.y = 192
frog.jumpDistance = 64

function frog.move(dx, dy)
  frog.x = frog.x + dx
  frog.y = frog.y + dy
end

function frog.draw()
  love.graphics.setColor(0, 255, 0)
  love.graphics.rectangle("fill", frog.x, frog.y, frog.w, frog.h)
  love.graphics.setColor(255, 255, 255)
end

function frog.keypressed(key)
  if key == "w" and frog.y > 0 then frog.move(0, -frog.jumpDistance)
  elseif key == "s" and frog.y < HEIGHT - frog.h then frog.move(0, frog.jumpDistance) end
  if key == "a" and frog.x > 0 then frog.move(-frog.jumpDistance, 0)
  elseif key == "d" and frog.x < WIDTH - frog.w then frog.move(frog.jumpDistance, 0) end
end


-- Main callbacks --
function love.draw()
  frog.draw()
  love.graphics.print("ESC to Quit")
end


function love.keypressed(key)
  frog.keypressed(key)
  if key == "escape" then love.event.quit() end
end
Another way would be to create a tile based level or a grid to move the frog from cell to cell.
Alright thank you. So I think it was not working because we had * dt.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests