so i figured i got to to be doing somthing wrong
this example code is from another thread: viewtopic.php?f=4&t=8831&hilit=jumping
Disclaimer: dont worry, wont use the code for real, just learning.
Code: Select all
function love.load()
player = love.graphics.newImage("assets/player.png")
playerx = 270
ySpeed = 0
playery = 400
gravSecond = 1.5
bg = love.graphics.newImage("assets/background.png")
-- music
music = love.audio.newSource( "assets/music.ogg" , "steam")
music:setLooping(true)
love.audio.play(music)
Cloudx = 0
yCloud = 900
cloudimage = love.graphics.newImage("assets/cloud.gif")
bullet = love.graphics.newImage("assets/bullet.gif")
right = love.graphics.newImage("assets/player.png")
left = love.graphics.newImage("assets/playerback.png")
end
function love.keypressed(key)
if key == " " and player.y < 400 then -- Are we on the ground?
player.ySpeed = -20 -- Make us add a negative, to move up
end
end
function love.update(dt)
frame = dt * 30 -- I do this just because I think better this way
player.ySpeed = player.ySpeed + player.gravSecond * frame -- We add gravity to the ySpeed
player.y = player.y + player.ySpeed * frame -- We add ySpeed to the player's y position
if player.y > 400 then -- Are we on the ground?
player.y = player.y - player.ySpeed * frame -- If we are, We undo that moving down
player.ySpeed = 0 -- The ySpeed is reset
end
Cloudx = Cloudx + 20*dt
if Cloudx >= (800 + 578) then
Cloudx = -100
end
if love.keyboard.isDown("d") then
playerx = playerx + 275*dt
player = right
end
if love.keyboard.isDown("s") then
playery = playery + 675*dt
end
if love.keyboard.isDown("a") then
player = left
playerx = playerx - 275*dt
end
if playery > 420 then
playery = 420
end
end
function love.draw()
love.graphics.setBackgroundColor(40,255,40)
love.graphics.setColor(111,183,255)
love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),450)
love.graphics.setColor(255,255,255)
love.graphics.draw(cloudimage, Cloudx - 578, -200)
love.graphics.draw(player, playerx, playery)
end
Thanks