[Solved] Love2d programming question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Dzemal
Prole
Posts: 5
Joined: Tue Sep 27, 2016 2:59 pm

[Solved] Love2d programming question

Post by Dzemal »

Hello Guys,

im programming a little game and I have a problem. My problem is that the rectangle doesnt go out of the little gap . He can move within the zone. But i want him to move out of the little gap in the right upper corner. How can i do that? Help and suggestions are appreciated.

Thank you

Code: Select all

function love.load()
  
  quadX = 30
  quadY = 540
  
end
 
function zeichneQuad(x,y,b,h)
  love.graphics.rectangle("fill",x,y,b,h)
end



function love.draw()
  love.graphics.line(5,5,725,5)
  love.graphics.line(5,5,5,595)
  love.graphics.line(5,595,795,595)
  love.graphics.line(795,5,795,595)
  zeichneQuad(quadX,quadY,30,30)
end


function love.keypressed(key)                       
  if key == "up"  and quadY +30 > 35 then
    quadY = quadY -5
   
    end  
  if key == "down" and quadY + 5 < 570 then
    quadY = quadY + 5 
    
    end 
  if key == "right" and quadX +5  < 770  then
      quadX = quadX + 5
      
    end  
  if key == "left" and quadX +30 > 35 then
      quadX = quadX - 5
     
    end    
end          
Attachments
Screenshot_1.png
Screenshot_1.png (3.44 KiB) Viewed 3629 times
Last edited by Dzemal on Wed Sep 28, 2016 6:04 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Love2d programming question

Post by raidho36 »

What you have there is hard-coded boundaries check, you not gonna get far with this. You need a collision detection system. There is a built-in Box2D engine, you can use that if you don't feel like implementing your own system.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Love2d programming question

Post by HugoBDesigner »

Just replace line 24 of your code from this:

Code: Select all

if key == "up"  and quadY +30 > 35 then
With this:

Code: Select all

if key == "up"  and (quadY +30 > 35 or quadX > 725) then
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Love2d programming question

Post by MadByte »

I send you a detailed PM regarding that topic.
Here is a small example demonstrating why it is importent to avoid hard-coded values:
Fiddle (view in browser)

Code: Select all

local screenWidth, screenHeight = love.graphics.getDimensions()
local borderSize
local rect = {}

function love.load()
  borderSize = love.math.random(8, 128)
  rect.width = love.math.random(8, 64)
  rect.height = rect.width
  rect.x = screenWidth/2-rect.width/2
  rect.y = screenHeight/2-rect.height/2
  rect.speed = 400
end


function rect.update(dt)
  local keyDown = love.keyboard.isDown
  if keyDown("left") then rect.x = rect.x - rect.speed * dt end
  if keyDown("right") then rect.x = rect.x + rect.speed * dt end
  if keyDown("up") then rect.y = rect.y - rect.speed * dt end
  if keyDown("down") then rect.y = rect.y + rect.speed * dt end
  rect.x = math.max(rect.x, borderSize)
  rect.x = math.min(rect.x, screenWidth-rect.width-borderSize)
  rect.y = math.max(rect.y, borderSize)
  rect.y = math.min(rect.y, screenHeight-rect.height-borderSize)
end


function rect.draw()
  love.graphics.rectangle("fill", rect.x, rect.y, rect.width, rect.height)
end


function love.update(dt)
  -- Reduce border Size --
  if screenHeight-(borderSize*2) > rect.width then
    borderSize = borderSize + 20 * dt
  end
  rect.update(dt)
end


function love.draw()
  love.graphics.rectangle("line", borderSize, borderSize, screenWidth-(borderSize*2), screenHeight-(borderSize*2))
  rect.draw()
  love.graphics.print("Space - Resize Border/Player", 10, 10)
  love.graphics.print("Arrow Keys - Move", 10, 30)
  love.graphics.print("Escape - Quit", 10, 50)
end


function love.keypressed(key)
  if key == "escape" then love.event.quit()
  elseif key == "space" then love.load() end
end
borderExample.love
(866 Bytes) Downloaded 119 times
When pressing "space" the dimensions of the player and the border change. try to implement this with hard-coded values, it won't work :P
Dzemal
Prole
Posts: 5
Joined: Tue Sep 27, 2016 2:59 pm

Re: [Solved] Love2d programming question

Post by Dzemal »

Hello again,

Your posts helped me a lot. I understand the collision now. Really appreciate it!

Dzemal
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 44 guests