LUA - LÖVE2D - 击中盒子时精灵死亡

时间:2014-10-10 15:27:28

标签: lua 2d coordinates love2d

- 好吧,基本上我已经在游戏中设置了我想要的一切。我现在所要做的就是创造敌人。我已经设置了产卵和移动,但是当玩家击中敌人时,没有任何反应。当红球击中积木时,我希望客户关闭。 (我显然会在以后改变它,但它只是暂时的)如果你可以编写在游戏中解释这个的代码,我将非常感激。我是这种编程语言的新手,我只有15岁仍在学习如何编码。我非常乐意在游戏中给你信任。

谢谢 - Olee

function love.load()
    love.graphics.setBackgroundColor( 110, 110, 110 ) --Sets background colour
    love.graphics.rectangle("fill",400,300,0,230,230) --Draws game background
    print("Olee's Game") -- prints into the console
    x=140 --x position of sprite
    y=320 --y position of the sprite
    swidth=20 --sprite width
    sheight=20 --sprite height
    evil=900 --red rectangular blocks x position
    evilheight=64 --red rectangular block height
    evilwidth=256 --red rectangular block width
    points=0 --point system created variable
    random1y=math.random(120,480) --creates y position of the first enemy block
    random2y=math.random(120,480) --creates y position of the block
    random3y=math.random(120,480) --creates y position of the block
    random4y=math.random(120,480) --creates y position of the block
    random5y=math.random(120,480) --creates y position of the block
    random6y=math.random(120,480) --creates y position of the block
end

function love.update(dt)
    --BOUNDRIES--(makes the sprite not go off the page)
    if y<120 then
        y=y+20
    end
    if y>520 then
        y=y-20
    end
    if x<20 then
        x=x+20
    end
    if x>780 then
        x=x-20
    end

    --AI (sends the blocks back to the start)
    evil=evil-400*dt 
    if evil<=(-400) then
        evil=1100
        random1y=math.random(120,480)
        random2y=math.random(120,480)
        random3y=math.random(120,480)
        random4y=math.random(120,480)
        random5y=math.random(120,480)
        random6y=math.random(120,480)
    end

    --Points
    points = love.timer.getTime()
    points=math.floor(points)
end

function love.focus(bool)
end

function love.keypressed( key, unicode )
    print("You just pressed "..key)
    print("x="..x.."\ny="..y.."\n#########")
    if key=="escape"then
        print("Bye!")
        os.exit(0)
    end

    if key == "down" then
        y=y+20
    end

    if key == "left" then --A KEY (LEFT)
        x=x-20
    end

    if key == "right" then --D KEY (RIGHT)
        x=x+20
    end

    if key == "up" then --W KEY (UP)
        y=y-20
    end
end

function love.draw()
    --Floor
    love.graphics.setColor(127, 127, 127)
    love.graphics.rectangle("fill",0,540,5000,100)
    --Ceiling
    love.graphics.setColor(127, 127, 127)
    love.graphics.rectangle("fill", 0, 0, 5000, 100)

    --Welcome Message
    love.graphics.setColor(191, 0, 52)
    love.graphics.print("Bonjourno and Welcome to Olee's Game",32,32,0,1,1)

    --Welcome Message HUD Box
    love.graphics.setColor(191, 0, 52)
    love.graphics.rectangle("line",16,18,284,48)

    --Circle (sprite)
    love.graphics.setColor(191, 0, 52)
    love.graphics.circle("fill",x,y,swidth,sheight)

    --SCOREBOARD
    love.graphics.setColor(191, 0, 52)
    love.graphics.print("Score: "..points.."",620, 35)

    --Evil 1
    love.graphics.setColor(191, 0, 52)
    love.graphics.rectangle("fill",evil,random1y,evilwidth,evilheight)

    --Evil 2
    love.graphics.setColor(191, 0, 52)
    love.graphics.rectangle("fill",evil,random2y,evilwidth,evilheight)

    --Evil 3
    love.graphics.setColor(191, 0, 52)
    love.graphics.rectangle("fill",evil,random3y,evilwidth,evilheight)

    --Evil 4
    love.graphics.setColor(191, 0, 52)
    love.graphics.rectangle("fill",evil,random4y,evilwidth,evilheight)

    --Evil 5 
    love.graphics.setColor(191, 0, 52)
    love.graphics.rectangle("fill",evil,random5y,evilwidth,evilheight)

    --Evil 6
    love.graphics.setColor(191, 0, 52)
    love.graphics.rectangle("fill",evil,random6y,evilwidth,evilheight)

    --FPS
    love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 735, 5)
end

function love.quit()
end

只是为了让你们知道,我已经设置了一个conf.lua文件。我的游戏运作完美,但我想补充一下! :)

我有一个play.bat

conf.lua:

function love.conf(t)
    t.modules.joystick = true   -- Enable the joystick module (boolean)
    t.modules.audio = true      -- Enable the audio module (boolean)
    t.modules.keyboard = true   -- Enable the keyboard module (boolean)
    t.modules.event = true      -- Enable the event module (boolean)
    t.modules.image = true      -- Enable the image module (boolean)
    t.modules.graphics = true   -- Enable the graphics module (boolean)
    t.modules.timer = true      -- Enable the timer module (boolean)
    t.modules.mouse = true      -- Enable the mouse module (boolean)
    t.modules.sound = true      -- Enable the sound module (boolean)
    t.modules.timer = true      -- Enable the timer module (boolean)
    t.modules.thread = true
    t.modules.math = true       -- Enable the math module (boolean)
    t.modules.physics = true    -- Enable the physics module (boolean)
    t.console = true            -- Attach a console (boolean, Windows only)
    t.title = "Olee's Game"     -- The title of the window the game is in (string)
    t.author = "Olee"           -- The author of the game (string)
    t.screen.fullscreen = false -- Enable fullscreen (boolean)
    t.screen.vsync = false      -- Enable vertical sync (boolean)
    t.screen.fsaa = 0           -- The number of FSAA-buffers (number)
    t.screen.height = 600       -- The window height (number)
    t.screen.width = 800        -- The window width (number)
end

play.bat:

@ECHO OFF
start "" "C:\Program Files (x86)\LOVE\love.exe" .

1 个答案:

答案 0 :(得分:1)

嗯,你有两种方法:

  1. 您可以进行高级碰撞
  2. 您可以进行简单的碰撞。
  3. 第二个更简单,但第一个对于这种游戏来说要好得多。

    1. 第一种方法:
    2. 首先,您需要知道此代码:

      function circleAndRectangleOverlap( circleX, circleY, circleRadius, rectangleX, rectangleY, rectangleWidth, rectangleHeight )
          local distanceX = math.abs( circleX - rectangleX - rectangleWidth / 2 )
          local distanceY = math.abs( circleY - rectangleY - rectangleHeight / 2 )
      
          if distanceX > ( rectangleWidth / 2 + circleRadius ) or distanceY > ( rectangleHeight  /2 + circleRadius ) then
              return false
          elseif distanceX <= ( rectangleWidth / 2 ) or distanceY <= ( rectangleHeight / 2 ) then
              return true
          end
      
          return ( math.pow( distanceX - rectangleWidth / 2, 2 ) + math.pow( distanceY - rectangleHeight / 2, 2 ) ) <= math.pow( circleRadius, 2 )
      end
      

      然后您可以将其添加到此代码的其余部分。

      function love.load()
          love.graphics.setBackgroundColor( 110, 110, 110 ) --Sets background colour
          love.graphics.rectangle("fill",400,300,0,230,230) --Draws game background
          print("Olee's Game") -- prints into the console
          x=140 --x position of sprite
          y=320 --y position of the sprite
          sradius=20 --sprite radius
          evil=900 --red rectangular blocks x position
          evilheight=64 --red rectangular block height
          evilwidth=256 --red rectangular block width
          points=0 --point system created variable
          random1y=math.random(120,480) --creates y position of the first enemy block
          random2y=math.random(120,480) --creates y position of the block
          random3y=math.random(120,480) --creates y position of the block
          random4y=math.random(120,480) --creates y position of the block
          random5y=math.random(120,480) --creates y position of the block
          random6y=math.random(120,480) --creates y position of the block
      end
      
      function love.update(dt)
          --BOUNDRIES--(makes the sprite not go off the page)
          if y<120 then
              y=y+20
          end
          if y>520 then
              y=y-20
          end
          if x<20 then
              x=x+20
          end
          if x>780 then
              x=x-20
          end
      
          --AI (sends the blocks back to the start)
          evil=evil-400*dt 
          if evil<=(-400) then
              evil=1100
              random1y=math.random(120,480)
              random2y=math.random(120,480)
              random3y=math.random(120,480)
              random4y=math.random(120,480)
              random5y=math.random(120,480)
              random6y=math.random(120,480)
          end
      
          --Points
          points = love.timer.getTime()
          points=math.floor(points)
      
          -- Check collisions
          if circleAndRectangleOverlap( x, y, sradius, evil, random1y, evilwidth, evilheight ) 
          or circleAndRectangleOverlap( x, y, sradius, evil, random2y, evilwidth, evilheight )
          or circleAndRectangleOverlap( x, y, sradius, evil, random3y, evilwidth, evilheight )
          or circleAndRectangleOverlap( x, y, sradius, evil, random4y, evilwidth, evilheight )
          or circleAndRectangleOverlap( x, y, sradius, evil, random5y, evilwidth, evilheight )
          or circleAndRectangleOverlap( x, y, sradius, evil, random6y, evilwidth, evilheight ) then
              love.event.quit()
          end
      end
      
      function love.focus(bool)
      end
      
      function love.keypressed( key, unicode )
          print("You just pressed "..key)
          print("x="..x.."\ny="..y.."\n#########")
          if key=="escape"then
              print("Bye!")
              love.event.quit()
          end
      
          if key == "down" then
              y=y+20
          end
      
          if key == "left" then --A KEY (LEFT)
              x=x-20
          end
      
          if key == "right" then --D KEY (RIGHT)
              x=x+20
          end
      
          if key == "up" then --W KEY (UP)
              y=y-20
          end
      end
      
      function love.draw()
          --Floor
          love.graphics.setColor(127, 127, 127)
          love.graphics.rectangle("fill",0,540,5000,100)
          --Ceiling
          love.graphics.setColor(127, 127, 127)
          love.graphics.rectangle("fill", 0, 0, 5000, 100)
      
          --Welcome Message
          love.graphics.setColor(191, 0, 52)
          love.graphics.print("Bonjourno and Welcome to Olee's Game",32,32,0,1,1)
      
          --Welcome Message HUD Box
          love.graphics.setColor(191, 0, 52)
          love.graphics.rectangle("line",16,18,284,48)
      
          --Circle (sprite)
          love.graphics.setColor(191, 0, 52)
          love.graphics.circle("fill",x,y,sradius)
      
          --SCOREBOARD
          love.graphics.setColor(191, 0, 52)
          love.graphics.print("Score: "..points.."",620, 35)
      
          --Evil 1
          love.graphics.setColor(191, 0, 52)
          love.graphics.rectangle("fill",evil,random1y,evilwidth,evilheight)
      
          --Evil 2
          love.graphics.setColor(191, 0, 52)
          love.graphics.rectangle("fill",evil,random2y,evilwidth,evilheight)
      
          --Evil 3
          love.graphics.setColor(191, 0, 52)
          love.graphics.rectangle("fill",evil,random3y,evilwidth,evilheight)
      
          --Evil 4
          love.graphics.setColor(191, 0, 52)
          love.graphics.rectangle("fill",evil,random4y,evilwidth,evilheight)
      
          --Evil 5 
          love.graphics.setColor(191, 0, 52)
          love.graphics.rectangle("fill",evil,random5y,evilwidth,evilheight)
      
          --Evil 6
          love.graphics.setColor(191, 0, 52)
          love.graphics.rectangle("fill",evil,random6y,evilwidth,evilheight)
      
          --FPS
          love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 735, 5)
      end
      
      function love.quit()
      end
      
      1. 第二种方法
      2. 这使用了AABB aproach,如果你将来使用实际的精灵,它会更有用。 只需将此插入circleAndRectangleOverlap(以及宽度和高度参数而不是半径):

        function checkAABB( spriteX, spriteY, spriteWidth, spriteHeight, rectangleX, rectangleY, rectangleWidth, rectangleHeight )
            if ( ( spriteX >= rectangleX + rectangleWidth)
            or ( spriteX + spriteWidth <= rectangleX ) 
            or ( spriteY >= rectangleY + rectangleHeight ) 
            or ( spriteY + spriteHeight <= rectangleY ) ) then
               return false 
           else return true
               end
           end
        end