Page 1 of 1

Platformer Blocks

Posted: Sat Jun 21, 2014 9:41 pm
by squidborne_destiny
Hey, I was wondering if there is a better way to use tile maps and spawn blocks for a platformer. My current code is:

Code: Select all

block = {}
blockimg = love.graphics.newImage("block.png")
function place_block(x,y,kind,color,status)
table.insert(block, {x=x,y=y,kind=kind,status=status,color=color})
end
function draw_block()
	for i,v in ipairs(block) do
	if v.color == 1 then
		love.graphics.setColor(255,0,0)
	end
	if v.kind == 1 then
	love.graphics.setColor(50,50,50)
		--love.graphics.draw(blockimg,v.x,v.y)
	end
	love.graphics.draw(blockimg,v.x,v.y)
	end
end
function update_block(dt)
	for i,v in ipairs(block) do
		if v.kind == 2 then 
			if player.ypos > v.y-22 and
			player.ypos < v.y+10 and
			player.xpos >= v.x-15 and
			player.xpos<=v.x+15  and
			v.y>player.ypos+9 then
				player.yvel = 0
				player.ypos = v.y - 21
				player.state = "stand"
			--	player.yvel = 0

			
			elseif  player.ypos > v.y-21 and
			player.ypos < v.y+10 and
			player.xpos >= v.x-21 and
			player.xpos<=v.x+21  and
			v.y<player.ypos+10 and
			love.keyboard.isDown("d") then
			
				player.xvel = 0
				player.xpos = v.x - 22				
		
			elseif  player.ypos > v.y-21 and
			player.ypos < v.y+10 and
			player.xpos >= v.x-21 and
			player.xpos<=v.x+21  and
			v.y<player.ypos+10 and
			love.keyboard.isDown("a") then
				
				player.xvel = 0
				player.xpos = v.x + 22

		
			elseif  player.ypos > v.y-5 and
			player.ypos < v.y+21 and
			player.xpos >= v.x-20 and
			player.xpos<=v.x+20  and
			v.y+10<player.ypos  then
				player.yvel = 0
				player.ypos = v.y + 21

			end
		end
		
	end
end