2014-10-10 13 views
0

- 好吧,基本上我已經在遊戲中設置了幾乎所有我想要的東西。我現在要做的就是創造敵人。我已經設定了產卵和移動,但是當玩家擊中敵人時,什麼也沒有發生。當紅球擊中塊,我希望客戶關閉。 (我會在稍後的日子改變這一點,但它只是暫時的)如果你能編寫能在遊戲中解釋這些代碼的代碼,我將非常感激。我是這種編程語言的新手,我只有15歲,仍然在學習如何編寫代碼。我非常樂意在遊戲中給你信用。LUA - LVEVE 2D - 雪碧擊中盒子時死亡

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

好了,你有兩種方法在這裏:

  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 
  • 第二種方法
  • 這使用了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