메뉴 건너뛰기

창작에 관련된 질문이나 간단한 팁, 예제를 올리는 곳

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 5, 5로 설정.
playerx = 5
playery = 5

-- 박스의 위치를 저장하는 배열.
box = {}

-- 박스 목표 지점의 위치를 저장하는 배열.
goal = {}

-- 움직인 횟수 저장
moves = 0

-- 맵 데이터를 저장하는 8*8의 2차원 배열 Map 생성
map = {}
for a = 1, 8 do
    map[a] = {}
    for b = 1, 8 do
        map[a][b] = 0
    end
end

function makebox(x, y)
-- 새 박스를 만든다.
    box[#box + 1] = {}
    box[#box]["x"] = x
    box[#box]["y"] = y
end

function makegoal(x, y)
-- 새 목표 지점을 만든다.
    goal[#goal + 1] = {}
    goal[#goal]["x"] = x
    goal[#goal]["y"] = y
end

function successed()
-- 모든 박스가 목표 지점 위에 놓여 있는가?
for a = 1 , #goal do
    if boxexist(goal[a]["x"], goal[a]["y"]) == 0 then
        return false
    end
end
return true
end

-- 맵 데이터 초기화
for a = 1, 8 do
    for b = 1, 8 do
        if (a == 1 or a == 8 or b == 1 or b == 8) then
            map[a][b] = 1
        end
    end
end

makebox(4,3)
makebox(4,6)

makegoal(5, 3)
makegoal(5, 6)

function boxexist(x, y)
    for a = 1, #box do
        if (box[a]["x"] == x and box[a]["y"] == y) then
            return a
        end
    end
    return 0
end

function love.load()
    tile = love.graphics.newImage("nokoban.png")
    -- 쿼드 만들기
    tileq = {}
    for a = 1, 6 do
        tileq[a] = love.graphics.newQuad(32 * (a - 1), 0, 32, 32, 192, 32)
    end
end

function love.draw()
    tempbuffer = 0
    for a = 1, 8 do
        for b = 1, 8 do
            if (map[a][b] == 0) then
                tempbuffer = 3
            elseif (map[a][b] == 1) then
                tempbuffer = 2
            end
           
            for c = 1, #box do
                if (box[c]["x"] == b and box[c]["y"] == a) then
                    tempbuffer = 5
                    break
                end
            end
           
            for c = 1, #goal do
                if (goal[c]["x"] == b and goal[c]["y"] == a) then
                    if (tempbuffer == 5) then -- 상자와 목표 지점이 겹친다면
                        tempbuffer = 6
                    else
                        tempbuffer = 4
                    end
                    break
                end
            end
           
            if (playerx == b and playery == a) then
                tempbuffer = 1
            end
           
            -- 그린다
            love.graphics.drawq(tile, tileq[tempbuffer], (b - 1) * 32, (a - 1) * 32)
        end
    end
end

function love.keypressed(key)
    if (key == "a") then
        if (map[playery][playerx - 1] == 0) then
            if (boxexist(playerx - 1, playery) ~= 0) then
                if (boxexist(playerx - 2, playery) == 0) then
                    if map[playerx - 2][playery] == 0 then
                        box[boxexist(playerx - 1, playery)]["x"] = box[boxexist(playerx - 1, playery)]["x"] - 1
                        playerx = playerx - 1
                        moves = moves + 1
                    end
                end
            else
                playerx = playerx - 1
                moves = moves + 1
            end
        end
    elseif (key == "s") then
        if (map[playery + 1][playerx] == 0) then
            if (boxexist(playerx, playery + 1) ~= 0) then
                if (boxexist(playerx, playery + 2) == 0) then
                    if map[playerx][playery + 2] == 0 then
                        box[boxexist(playerx, playery + 1)]["y"] = box[boxexist(playerx, playery + 1)]["y"] + 1
                        playery = playery + 1
                        moves = moves + 1
                    end
                end
            else
                playery = playery + 1
                moves = moves + 1
            end
        end
    elseif (key == "d") then
        if (map[playery][playerx + 1] == 0) then
            if (boxexist(playerx + 1, playery) ~= 0) then
                if (boxexist(playerx + 2, playery) == 0) then
                    if map[playerx + 2][playery] == 0 then
                        box[boxexist(playerx + 1, playery)]["x"] = box[boxexist(playerx + 1, playery)]["x"] + 1
                        playerx = playerx + 1
                        moves = moves + 1
            end
                end
            else
                playerx = playerx + 1
                moves = moves + 1
            end
        end
    elseif (key == "w") then
        if (map[playery - 1][playerx] == 0) then
            if (boxexist(playerx, playery - 1) ~= 0) then
                if (boxexist(playerx, playery - 2) == 0) then
                    if map[playerx][playery - 2] == 0 then
                        box[boxexist(playerx, playery - 1)]["y"] = box[boxexist(playerx, playery - 1)]["y"] - 1
                        playery = playery - 1
                        moves = moves + 1
                    end
                end
            else
                playery = playery - 1
                moves = moves + 1
            end
        end
    end
end



역시 변경된 부분만 설명드리겠습니다.



function love.keypressed(key)
    if (key == "a") then
        if (map[playery][playerx - 1] == 0) then
            if (boxexist(playerx - 1, playery) ~= 0) then
                if (boxexist(playerx - 2, playery) == 0) then
                    if map[playerx - 2][playery] == 0 then
                        box[boxexist(playerx - 1, playery)]["x"] = box[boxexist(playerx - 1, playery)]["x"] - 1
                        playerx = playerx - 1
                        moves = moves + 1
                    end
                end
            else
                playerx = playerx - 1
                moves = moves + 1
            end
        end
    elseif (key == "s") then
        if (map[playery + 1][playerx] == 0) then
            if (boxexist(playerx, playery + 1) ~= 0) then
                if (boxexist(playerx, playery + 2) == 0) then
                    if map[playerx][playery + 2] == 0 then
                        box[boxexist(playerx, playery + 1)]["y"] = box[boxexist(playerx, playery + 1)]["y"] + 1
                        playery = playery + 1
                        moves = moves + 1
                    end
                end
            else
                playery = playery + 1
                moves = moves + 1
            end
        end
    elseif (key == "d") then
        if (map[playery][playerx + 1] == 0) then
            if (boxexist(playerx + 1, playery) ~= 0) then
                if (boxexist(playerx + 2, playery) == 0) then
                    if map[playerx + 2][playery] == 0 then
                        box[boxexist(playerx + 1, playery)]["x"] = box[boxexist(playerx + 1, playery)]["x"] + 1
                        playerx = playerx + 1
                        moves = moves + 1
            end
                end
            else
                playerx = playerx + 1
                moves = moves + 1
            end
        end
    elseif (key == "w") then
        if (map[playery - 1][playerx] == 0) then
            if (boxexist(playerx, playery - 1) ~= 0) then
                if (boxexist(playerx, playery - 2) == 0) then
                    if map[playerx][playery - 2] == 0 then
                        box[boxexist(playerx, playery - 1)]["y"] = box[boxexist(playerx, playery - 1)]["y"] - 1
                        playery = playery - 1
                        moves = moves + 1
                    end
                end
            else
                playery = playery - 1
                moves = moves + 1
            end
        end
    end
end


쓸데없이 코드 양은 많은데 바뀐 건 없습니다, 복사/붙여넣기니까요.

io.read()를 사용해서 입력을 받다가 이제 love.keypressed 콜백으로 받습니다.

key 변수에 무슨 키를 눌렀는지를 입력받게 되고

a, s, d, w에 맞는 처리를 하게 됩니다.


왠지 날로 먹는 이 날의 글은 이것으로 끝. 이거 구현하는데 몇일 걸린 걸 생각하면 그저 엉엉.

다음부터는 옮긴 횟수와 설명을 보여주고, 모든 상자가 제 자리로 돌아가면 끝내는 걸 마지막으로 끝내겠습니다.

소코반 끝내고 다음에 뭐 만들지? 슈팅 게임이라도 만들까? 아니면 5분짜리 RPG 같은 것?
조회 수 :
832
등록일 :
2013.09.18
08:04:21 (*.209.135.92)
엮인글 :
게시글 주소 :
https://hondoom.com/zbxe/index.php?mid=study&document_srl=703965
List of Articles
번호 제목 글쓴이 날짜 조회 수sort
65 여러분 질문있어요. 성심성의껏좀 알려주세요 흑흑 [6] 케르메스 2006-02-11 2022
64 [re] 혼돈님께 질문 혼돈 2007-12-25 2048
63 흥크립트로 만든 예제 [1] file 대슬 2007-12-01 2051
62 게임 만들때는 게임 기획을 해야 합니다. [7] 똥똥배 2008-02-12 2106
61 흥크립트로 만든 예제 2 [2] file 대슬 2007-12-01 2118
60 Divx6를 받아서 인코딩을 하려는데... 에러뜸... [3] 장펭돌 2008-02-09 2154
59 웹 프로그래밍을 배우려고 합니다. [2] Kadalin 2008-03-22 2171
58 조금씩 게임 소스가 완성 되어 가는데요. [2] 짜스터 2010-11-16 2188
57 흥크립트 Ver0.9 변수 안의 변수 기능 [2] 혼돈 2007-10-16 2208
56 '@클릭'이거 어떻게 사용하는거죠? [4] 네모상자 2008-01-26 2244
55 명령문 질문드립니다. [1] X-tra 2008-03-26 2244
54 저는 사실 이걸 잘 못해요. [3] 앟랄 2008-03-26 2282
53 흥크립트 0.9 새 기능 소개 [3] file 혼돈 2007-10-15 2289
52 흥크립트 키입력 질문 [1] A.미스릴 2008-03-15 2363
51 흥크립트 기초 질문. [2] 장펭돌 2007-12-03 2430
50 데이터 축소에 대하여 [2] 짜스터 2010-10-13 2430
49 그림 출력의 순서를 알고 싶습니다. [3] X-tra 2008-03-12 2466
48 이상하군요. [2] 대슬 2008-05-16 2490
47 2중 for문을 돌릴 때 [3] 똥똥배 2011-04-06 2539
46 퀴즈소스입니다 허클베리핀님 라컨 2005-08-23 2546