Page 1 of 1

Help i am making a game for a project but i have some trouble

Posted: Sat Jun 05, 2021 9:47 am
by 5rikar_98
I am making a game where the character needs to collect trash to get the points but I am having trouble spawning multiple trash bags. The code I wrote spawns one trash bag and changes the x point but I need multiple bags with different x points. How can I do this? Here is the code. I can try this on my own but if you know can you tell me how can I update the score.

Code: Select all

function love.load()
  button = {}
  button.x = love.graphics.getWidth()/2
  button.y = love.graphics.getHeight()/2
  button.size = 100
  timer = 0
  ym = 0
  yt = 0
  xt = 100
  xt5 = 400
  xt4 = 500
  xt3 = 0
  xt2 = 600
  r = 0
  xm = 50
  score = 0


  back_image = love.graphics.newArrayImage("home image2.png")
  game_image = love.graphics.newArrayImage("game image.png")
  character = love.graphics.newArrayImage("character.png")
  trashimg =love.graphics.newArrayImage("trash bag1.png")
  milk = love.graphics.newArrayImage("milk bottle.png")
  myfont = love.graphics.newFont("VIDEOPHREAK.ttf", 50)
  myinstruct = love.graphics.newFont("VIDEOPHREAK.ttf", 17)
end

function love.draw()
  love.graphics.setFont(myfont)

  love.graphics.setColor(255, 255, 255)
  love.graphics.draw(back_image)

  love.graphics.setColor(0, 255, 0)
  love.graphics.circle("fill", button.x, button.y, button.size)
  love.graphics.rectangle("fill", button.x - 205, 25, 430, 100)
  love.graphics.rectangle("fill", 15, 440, 770, 50)

  love.graphics.setColor(0, 0, 0)
  love.graphics.print("PLAY!", button.x - 75, button.y - 35)
  love.graphics.print("THE RECYCLER", button.x - 190, 40)

  love.graphics.setFont(myinstruct)
  love.graphics.print("Use Arrow Keys To Move And Collect Trash By Touching And Avoid Milk Bottles", 25, 450)

end

function love.mousepressed(x, y, b, isTouch)
  if b == 1 then
    if button.size > distanceBetween(button.x, button.y, love.mouse.getX(), love.mouse.getY()) then
      x = (love.graphics.getWidth()/2) - 30
      y = 360
      function love.update(dt)
        timer = timer + 1
        ym = ym + 0.5
        yt = yt + 1
        if yt == 400 then
          yt = 0
        end
        if ym == 430 then
          ym = 0
        end
        if timer%860 == 0 then
          xm = math.random(20,780)
        end
        if love.keyboard.isDown("left") then
          x = x - 5
          if x + 60 <= 0 then
            x = love.graphics.getWidth() - 70
          end
        end

        if love.keyboard.isDown("right") then
          x = x + 5
          if x - 60 >= love.graphics.getWidth() - 70  then
            x = 0
          end
        end

        if love.keyboard.isDown("up") then
          y = y - 5
          if y + 60 <= 0  then
            y = love.graphics.getHeight()
          end
        end

        if love.keyboard.isDown("down") then
          y = y + 5
          if y - 60 >= love.graphics.getHeight() - 70  then
            y = 0
          end
        end
        player = {
                  score = 0
        }
        trash = {}

        if timer%0.01 == 0 then
          for i = 1, 5 do
            trash[#trash+1] = {
              y = yt,
              speed = 5,
              x = math.random(40,love.graphics.getWidth() - 70),
              score = 10,
            }
          end
        end

        function trash.update(dt)
         for i,v in ipairs(trash) do
           if v.y > 400 then
            table.remove(trash, i)
           end

           if collisiondetect(x,y,v.x,v.y) == true then
             table.remove(trash, i)
           end
         end
        end


        for i,v in ipairs(trash) do
         if collisiondetect(x,y,v.x,v.y) == true then
           player.score = player.score + 1000 * dt
           y = 300
         end
        end
      end
      function love.draw()
        love.graphics.setColor(255, 255, 255)
        love.graphics.draw(game_image)
        love.graphics.draw(character,x,y)
        love.graphics.draw(milk,xm,ym)
        love.graphics.print("score: " .. player.score)
        for i, v in ipairs(trash) do
          love.graphics.draw(trashimg, v.x, v.y)
        end
      end

      function collisiondetect(xa,ya,xb,yb)
        if 31.5 > distanceBetween(xa - 50,ya - 55,xb,yb) then
          return true
        end
      end
    end
  end
end

function distanceBetween(x1,y1,x2,y2)
  return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
end
Edit: Here is the .love file https://gofile.io/d/Tj5Dwl

Re: Help i am making a game for a project but i have some trouble

Posted: Sun Jun 06, 2021 7:37 am
by Jeeper
The reason is that you are declaring the trash table in your mousepressed callback function. This means that each time you set it to be a new, empty, table.

Apart from that your code is a little bit confusing, which makes it harder for you to find issues like this. I would recommend against nesting everything inside of the mousepressed-function.

Re: Help i am making a game for a project but i have some trouble

Posted: Sun Jun 06, 2021 12:26 pm
by 5rikar_98
Jeeper wrote: Sun Jun 06, 2021 7:37 am The reason is that you are declaring the trash table in your mousepressed callback function. This means that each time you set it to be a new, empty, table.

Apart from that your code is a little bit confusing, which makes it harder for you to find issues like this. I would recommend against nesting everything inside of the mousepressed-function.
Thank you for your help I fixed it and I will try to not nesting everything in a function.