Help needed for platformer

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
CraftedPixel
Prole
Posts: 7
Joined: Tue Dec 29, 2015 5:26 pm

Help needed for platformer

Post by CraftedPixel »

I have this platformer game that I need help with.
And how to make the terrain not cycle through one block.
how do I make more than one block?
Here is main.lua:

Code: Select all

-- Made By: CraftedPixel  CC - BY 4.0 See here: http://creativecommons.org/licenses/by/4.0/ --
function love.load(dt)
	blocksize = 25 -- You can modify this without breaking the game
	godown = go -- tells script to go to next line
	b = {'block', 255, 150, 0} -- this is a block. I am giving them RGB values and block nicknames
	s = {'sky', 0, 150, 255} -- {block name, Red, Green, Blue}
	g = {'ground', 100, 255, 100}
	terrain = { -- This should look like a smiley face when code is run
	b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,godown,
	s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,godown,
	s,s,b,b,s,s,s,s,s,s,s,s,b,b,s,s,godown,
	s,s,b,g,s,s,s,s,s,s,s,s,g,b,s,s,godown,
	s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,godown,
	s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,godown,
	s,b,b,b,s,s,s,s,s,s,s,s,b,b,b,s,godown,
	s,s,b,b,b,s,s,s,s,s,s,b,b,b,s,s,godown,
	s,s,s,b,b,b,b,b,b,b,b,b,b,s,s,s,godown,
	s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,godown,
	s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,godown,
	g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,godown,
	stopdraw} -- You can make your own level if you want :D
	px = 10 -- I was planning to make a character but the terrain is my greatest priority
	py = 10
	drawx = 0 -- some draw numbers for the positon of the blocks
	drawy = 0
	drawnumber = 0 -- Terrain scanning number
end
function love.update(dt)
	drawx = drawx + 1
	drawnumber = drawnumber + 1
	if terrain[drawnumber] == go then -- Cycles through the level  given
		drawx = 0
		drawy = drawy + 1
		drawnumber = drawnumber + 1
	end
	if terrain[drawnumber] == stopdraw then -- this resets the drawing when stop signal is reached
		drawx = 0
		drawy = 0
		drawnumber =1
	end
end
function love.draw(dt)
	love.graphics.setColor(255,255,255)
	love.graphics.print(terrain[drawnumber], 10, 10)
	love.graphics.print(drawnumber, 10, 25)
	love.graphics.setColor(terrain[drawnumber][2],terrain[drawnumber][3],terrain[drawnumber][4]) -- Gets color of current terrain block
	love.graphics.rectangle("fill",drawx * blocksize, drawy * blocksize, blocksize, blocksize) -- Draws a block on terrain block
end
conf.lua

Code: Select all

function love.conf(t)
	t.window.height = 600
	t.window.width = 800
	t.title = "Map testing!"
end
Test the code out. Show me some code and how it works.
Thanks for helping! See the comments I made they may help you!
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help needed for platformer

Post by zorg »

I'm feeling generous tonight, though it's untested;

Code: Select all

function love.load() -- no dt
  blocksize = 25
  width = 9
  height = 9

  blocks = {}
  blocks.wall = {255,150,0}
  blocks.sky = {0,150,255}
  blocks.ground = {100,255,100}

  terrain = [[
sssssssss
swwwwwwws
swgggggws
swgwwwgws
swgwswgws
swgwswgws
swgwwwgws
swwwwwwws
sssssssss]]
end

function love.draw()
  local i = 0
  for c in str:gmatch"." do
    if c == 's' then
      love.graphics.setColor(blocks.sky)
    elseif c == 'g' then
      love.graphics.setColor(blocks.ground)
    elseif c == 'w' then
      love.graphics.setColor(blocks.wall)
    end
    if c == 's' or c == 'g' or c == 'w' then -- don't do this with more than a few types though
      love.graphics.rectangle((i%width)*blocksize, math.floor(i/width)*blocksize, blocksize, blocksize)
      i = i + 1
    end
  end
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
CraftedPixel
Prole
Posts: 7
Joined: Tue Dec 29, 2015 5:26 pm

Re: Help needed for platformer

Post by CraftedPixel »

zorg wrote:I'm feeling generous tonight, though it's untested;

Code: Select all

function love.load() -- no dt
  blocksize = 25
  width = 9
  height = 9

  blocks = {}
  blocks.wall = {255,150,0}
  blocks.sky = {0,150,255}
  blocks.ground = {100,255,100}

  terrain = [[
sssssssss
swwwwwwws
swgggggws
swgwwwgws
swgwswgws
swgwswgws
swgwwwgws
swwwwwwws
sssssssss]]
end

function love.draw()
  local i = 0
  for c in str:gmatch"." do
    if c == 's' then
      love.graphics.setColor(blocks.sky)
    elseif c == 'g' then
      love.graphics.setColor(blocks.ground)
    elseif c == 'w' then
      love.graphics.setColor(blocks.wall)
    end
    if c == 's' or c == 'g' or c == 'w' then -- don't do this with more than a few types though
      love.graphics.rectangle((i%width)*blocksize, math.floor(i/width)*blocksize, blocksize, blocksize)
      i = i + 1
    end
  end
end
Hmm do you think you can include the whole main.lua file (Because I am having trouble with debugging) ? I am trying to also make it so that I can put in blocks as I please. Like the original code. Because who knows. I might need a cloud block or something later on, and I don't want to change the whole design of the code. I love the effort you did to help me. Thanks alot!
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Help needed for platformer

Post by Beelz »

Here, try this...

Code: Select all

function love.load()
    blocksize = 25

     local b = {255, 150, 0}  -- localize these and translate into terrain table
     local s = {0, 150, 255}
     local g = {100, 255, 100} 
    

    terrain = {}    -- table containing all maps

    terrain[#terrain+1] = {    -- new terrain
        {b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b},      -- also put each row into a table
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},      
        {s,s,b,b,s,s,s,s,s,s,s,s,b,b,s,s},      -- it will help when drawing
        {s,s,b,g,s,s,s,s,s,s,s,s,g,b,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {s,b,b,b,s,s,s,s,s,s,s,s,b,b,b,s},
        {s,s,b,b,b,s,s,s,s,s,s,b,b,b,s,s},
        {s,s,s,b,b,b,b,b,b,b,b,b,b,s,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g}
    }
end

function love.update(dt)

end

function love.draw()
     for i, row in ipairs(terrain[1]) do
          for j, column in ipairs(terrain[1][i]) do
               love.graphics.setColor(terrain[1][i][j])
               love.graphics.rectangle("fill", j * blocksize, i * blocksize, blocksize, blocksize)
          end
     end
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Help needed for platformer

Post by ivan »

Great example by zorg.
The only thing I would change is

Code: Select all

function loadMap(s)
  local map = {}
  if string.sub(s, -1) ~= "\n" then
    s = s .. "\n"
  end
  -- for each line
  for l in string.gmatch(s, "(.-)\n") do
    -- for each character
    for c in string.gmatch(l, ".") do
      map[#map + 1] = c
    end
  end
  return map
end
This way you can run the code once when loading.
Then you iterate the table during rendering.
CraftedPixel
Prole
Posts: 7
Joined: Tue Dec 29, 2015 5:26 pm

Re: Help needed for platformer

Post by CraftedPixel »

Beelz wrote:Here, try this...

Code: Select all

function love.load()
    blocksize = 25

     local b = {255, 150, 0}  -- localize these and translate into terrain table
     local s = {0, 150, 255}
     local g = {100, 255, 100} 
    

    terrain = {}    -- table containing all maps

    terrain[#terrain+1] = {    -- new terrain
        {b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b},      -- also put each row into a table
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},      
        {s,s,b,b,s,s,s,s,s,s,s,s,b,b,s,s},      -- it will help when drawing
        {s,s,b,g,s,s,s,s,s,s,s,s,g,b,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {s,b,b,b,s,s,s,s,s,s,s,s,b,b,b,s},
        {s,s,b,b,b,s,s,s,s,s,s,b,b,b,s,s},
        {s,s,s,b,b,b,b,b,b,b,b,b,b,s,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g}
    }
end

function love.update(dt)

end

function love.draw()
     for i, row in ipairs(terrain[1]) do
          for j, column in ipairs(terrain[1][i]) do
               love.graphics.setColor(terrain[1][i][j])
               love.graphics.rectangle("fill", j * blocksize, i * blocksize, blocksize, blocksize)
          end
     end
end
Wow you guys are very helpful :D

Now that the level map works, I now need a way to add player collision properties with certian blocks. Like we can have block = ("Block Name", Red, Green, Blue, VAR)
In VAR we can put 0 for no collision(like a sky block), 1 for blocks that you can go on top of and go down, 2 for collidable blocks, 3 for ladder, 4 for death (like a pincher or something) I also need to improve the jumping. But moving left and right works fine

Here is my new main.lua thanks to your help of course:

Code: Select all

-- Made By: CraftedPixel and the love2d fourums :)  CC - BY 4.0 See here: http://creativecommons.org/licenses/by/4.0/ --
function love.load()
    blocksize = 45
    player1 = {}
    player1.x = 0
    player1.y = 0
    player1.xgo = 0
    player1.ygo = 0
	local b = {255, 150, 0} -- localize these and translate into terrain table
    local s = {0, 150, 255} -- {Red, Green, Blue,"Block Name" , Collision(0 = no, 1 = Platform, 2 = Solid)}
    local g = {100, 255, 100}
    terrain = {}    -- table containing all maps
	terrain[#terrain+1] = {    -- new terrain
    	{b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s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s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s},
        {g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g,g},
		}
end

function love.update()
	if love.keyboard.isDown("a") then
		player1.xgo = player1.xgo + 2
	elseif love.keyboard.isDown("d") then
		player1.xgo = player1.xgo - 2
	end

	if love.keyboard.isDown("space") then
		player1.ygo = player1.ygo + 2
	else
		player1.ygo = 0
	end
	if player1.xgo == 0 then
		
	elseif player1.xgo <= 0 then
		player1.xgo = player1.xgo + 1
	elseif player1.xgo >= 0 then
		player1.xgo = player1.xgo - 1
	end
	player1.x = player1.x + player1.xgo
	player1.y = player1.y + player1.ygo
end

function love.draw()
	for i, row in ipairs(terrain[1]) do
		for j, column in ipairs(terrain[1][i]) do
			love.graphics.setColor(terrain[1][i][j])
			love.graphics.rectangle("fill", j * blocksize + player1.x, i * blocksize + player1.y, blocksize, blocksize)
		end
	end
	love.graphics.setColor(200, 50, 255)
	love.graphics.rectangle("fill",blocksize * 9 + player1.xgo, 10 * blocksize + player1.ygo, blocksize, blocksize * 2)
end
Try out the code!
Just get and 800 by 600 window in your conf.lua
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Help needed for platformer

Post by Beelz »

CraftedPixel wrote:Now that the level map works, I now need a way to add player collision properties with certian blocks. Like we can have block = ("Block Name", Red, Green, Blue, VAR)
In VAR we can put 0 for no collision(like a sky block), 1 for blocks that you can go on top of and go down, 2 for collidable blocks, 3 for ladder, 4 for death (like a pincher or something) I also need to improve the jumping. But moving left and right works fine
You can exchange the tile placeholders with tables:

Code: Select all

    local b = {
      id = "block",
      color = {255, 150, 0},
      collides = true
    }

    local s = {
      id = "sky",
      color = {0, 150, 255},
      collides = false
    }

    local g = {
      id = "ground",
      color = {100, 255, 100},
      collides = true
    }
Then inside of draw you will need to fix the setColor, because now its nested into the table:

Code: Select all

-- change
terrain[1][i][j]
-- to
terrain[1][i][j].color

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests