Code: Select all
function love.load()
-- credit to tentus
player = {
x = 400,
y = 465,
w = 20,
h = 70,
v = false -- not just 0 thrust, but NO thrust at all
}
ground= {
x = 0,
y = 535,
w = 800,
h = 100
}
initial = -200 -- start with an upward for of 200 px per second
falloff = -50 -- we lose 50 px of upward thrust per second
Tileset = love.graphics.newImage('images/foresttiles01.png')
TileW, TileH = 60,60
local tilesetW, tilesetH = Tileset:getWidth(),Tileset:getHeight()
love.graphics.setBackgroundColor( 54, 54, 54)
local quadInfo = {
{ 'a', 0, 300 }, -- air
{ '#', 60, 0 }, -- grass
{ '*', 60, 120 }, -- flowers
{ '^', 120, 120 }, -- other flowers
{ 'g', 0, 0 } --other grass
}
Quads={}
for _,info in ipairs(quadInfo) do
-- info[1] = character, info[2]= x, info[3] = y
Quads[info[1]] = love.graphics.newQuad(info[2], info[3], TileW, TileH, tilesetW, tilesetH)
end
--Map Tile Layout
local tileString = [[
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
*^^***^^**^^**
#g#g#g#g#g#g#g
]]
-- Translate the characters into the right tile parts of the Tileset
TileTable = {}
local width = #(tileString:match("[^\n]+"))
for x = 1,width,1 do TileTable[x] = {} end
local rowIndex,columnIndex = 1,1
for row in tileString:gmatch("[^\n]+") do
assert(#row == width, 'Map is not aligned: width of row ' .. tostring(rowIndex) .. ' should be ' .. tostring(width) .. ', but it is ' .. tostring(#row))
columnIndex = 1
for character in row:gmatch(".") do
TileTable[columnIndex][rowIndex] = character
columnIndex = columnIndex + 1
end
rowIndex=rowIndex+1
end
end
function love.update(dt)
xc = love.mouse.getX( )
yc = love.mouse.getY( )
if player.v then --movement per update
player.v = player.v + (falloff * dt)
player.y = player.y + (player.v * dt)
end
if player.y + player.h >= ground.y then -- stop the box when touching ground
player.y = ground.y - player.h
player.v = false
end
end
function love.keypressed (key)
if key == "w" then
if not player.v then -- can't jump if we're already jumping
player.v = initial
end
end
end
function love.draw()
lg = love.graphics
--draw the actors
lg.rectangle("fill", player.x, player.y, player.w, player.h)
lg.rectangle("fill", ground.x, ground.y, ground.w, ground.h)
--print me some debug info
lg.print("FPS: "..love.timer.getFPS(), 8,10)
lg.print("Mouse X: " ..xc, 8, 20)
lg.print("Mouse Y: " ..yc, 8, 30)
lg.print("Player Y: " ..player.x, 8, 40)
lg.print("Player X: " ..player.y, 8, 50)
-- draw the Tilemap
for columnIndex,column in ipairs(TileTable) do
for rowIndex,char in ipairs(column) do
local x,y = (columnIndex-1)*TileW, (rowIndex-1)*TileH
love.graphics.drawq(Tileset, Quads[char], x, y)
end
end
end
Okay, sorry for that messy code, I followed Burrito's advise and cleaned the code and decided to fully remove HC. But somehow the player now spawns and escapes into the offscreen via -y. Can you please take a look?. I don't see any mistakes...