Page 1 of 1

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

Posted: Mon Sep 28, 2015 3:33 pm
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)

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

Posted: Mon Sep 28, 2015 5:17 pm
by CanadianGamer
Do you need the frog to jump in an arc or can it be a linear movement?

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

Posted: Mon Sep 28, 2015 5:24 pm
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.

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

Posted: Mon Sep 28, 2015 11:19 pm
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.

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

Posted: Mon Sep 28, 2015 11:21 pm
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?

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

Posted: Tue Sep 29, 2015 3:47 pm
by CanadianGamer
It might help if you posted all the code so that we can get a little more context.

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

Posted: Tue Sep 29, 2015 3:57 pm
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

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

Posted: Tue Sep 29, 2015 4:17 pm
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.

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

Posted: Tue Sep 29, 2015 8:10 pm
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.