LUA-LÖ;VE 2D-精灵在击中盒子时死亡

LUA-LÖ;VE 2D-精灵在击中盒子时死亡,lua,2d,coordinates,love2d,Lua,2d,Coordinates,Love2d,--好的,基本上我已经在游戏中设置了我想要的一切。我现在要做的就是创造敌人。我已经设置了产卵和移动,但是当玩家击中敌人时,什么都不会发生。当红球击中障碍时,我希望客户关闭。(很明显,我会在以后更改,但这只是暂时的)如果你能编写代码在游戏中解释这一点,我将不胜感激。我不熟悉这种编程语言,只有15岁还在学习如何编写代码。我非常高兴在这场比赛中给你荣誉 谢谢你-奥利 function love.load() love.graphics.setBackgroundColor( 110, 110,

--好的,基本上我已经在游戏中设置了我想要的一切。我现在要做的就是创造敌人。我已经设置了产卵和移动,但是当玩家击中敌人时,什么都不会发生。当红球击中障碍时,我希望客户关闭。(很明显,我会在以后更改,但这只是暂时的)如果你能编写代码在游戏中解释这一点,我将不胜感激。我不熟悉这种编程语言,只有15岁还在学习如何编写代码。我非常高兴在这场比赛中给你荣誉

谢谢你-奥利

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
play.bat:

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

这里有两种方法:

  • 你可以做高级碰撞
  • 你可以做简单的碰撞
  • 第二种更简单,但第一种更适合这种游戏

  • 第一种方法:
  • 首先,您需要了解以下代码:

    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 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
    
    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