LUA - LÖVE 2D - Спрайт умирает при попадании в коробку

--Хорошо, в основном я настроил почти все, что хотел в игре. Все, что мне нужно сделать сейчас, это создать врагов. Я настроил возрождение и движение, но когда игрок бьет врагов, ничего не происходит. Когда красный шар попадает в блоки, я хочу, чтобы клиент закрылся. (я, очевидно, изменю это позже, но это только временно) Я был бы очень признателен, если бы вы могли запрограммировать код, который будет интерпретировать это в игре. Я новичок в этом языке программирования, и мне всего 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

Просто чтобы вы знали, ребята, я создал файл conf.lua. Моя игра работает отлично, но я хотел бы добавить это! :)

и у меня есть play.bat

конф.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

играть.летучая мышь:

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

person Olangutan    schedule 10.10.2014    source источник


Ответы (1)


Ну, у вас есть два подхода здесь:

  1. Вы можете сделать расширенное столкновение
  2. Вы можете сделать простое столкновение.

Второй проще, но первый намного лучше подходит для такого рода игр.

  1. Первый подход:

Во-первых, вам нужно знать этот код:

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. Второй подход

Здесь используется подход AABB, который будет более полезен, если в будущем вы будете использовать настоящие спрайты. Просто вставьте это для 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
person DavisDude    schedule 22.01.2015