메뉴 건너뛰기

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

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 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 날짜 최근 수정일
185 흥크립트 0.9 새 기능 소개 [3] file 혼돈 2289   2007-10-15 2008-03-17 04:37
 
184 흥크립트 Ver0.9 변수 안의 변수 기능 [2] 혼돈 2208   2007-10-16 2008-03-17 04:37
상당히 고급기능이라 꼭 알 필요는 없지만, 잘 활용하면 배열이나 구조체 비슷하게 활용 가능합니다. 참고로 []를 쓰는 숫자변수와 {}를 쓰는 문자변수는 서로 다른 테이블을 쓰므로 이름이 겹쳐도 상관없습니다. [변수X] 1 [변수[변수X]] 10 {변수} "변수" {...  
183 흥크립트로 만든 예제 [1] file 대슬 2051   2007-12-01 2008-03-17 04:37
 
182 흥크립트로 만들 때 알아둘 기본 사항 [6] 대슬 1693   2007-12-01 2008-03-17 04:37
common.mlc, Global.mlc 를 제외한 다른 모든 mlc 파일들은 스크립트 파일 이름과 일치하게 만들어야 읽을 수 있습니다. 예를 들어 MAIN.dlg (또는 txt) 에 대한 mlc 파일은 MAIN.mlc 로 저장해야 스크립트에서 읽어서 씁니다. MAIN에서 대슬랑미.dlg 로 갈아...  
181 흥크립트로 만든 예제 2 [2] file 대슬 2118   2007-12-01 2008-03-17 04:37
 
180 흥크립트에서 반복문 실행하기 (덤추가) [9] 대슬 1790   2007-12-01 2008-03-17 04:37
[엔] 0 ^0:연산하는부분 //원하는 연산 내용을 자유롭게 적으면 됩니다. [엔] [엔]+1 ^1 [체크] ([엔]==10)?2:0 //↑[엔]의 값이 10인지 아닌지를 판별하고 10이면 [체크]에 1이, //10이 아니면 [체크]에 0이 들어갑니다. ([엔]==10)에서 10부분에 다른 수를 //...  
179 반복문 예제 [4] file 대슬 1833   2007-12-01 2008-03-17 04:37
 
178 흥크립트로 만든 예제 4 file 대슬 1723   2007-12-02 2008-03-17 04:37
 
177 흥크립트 기초 질문. [2] 장펭돌 2430   2007-12-03 2008-03-17 04:37
지금 슬슬 설명서를 읽어나가는중... 기본 원리에서 다른건 대충 전부다 이해가 가지만, 몇가지 이해가 안가는 것이 있어서 질문 합니다. 우선 그림파일은 mlc로, 텍스트 파일은 dlg로 바꾸는것이 맞지요? 그렇다면, '게임 전체적으로 쓸 그림이라면 DATA폴더...  
176 흥크립트 질문 [3] A.미스릴 1729   2007-12-07 2008-03-17 04:37
마우스 커서 생겼을때 마우스 커서의 좌표를 취득할순 없나요?-0-; * 똥똥배님에 의해서 게시물 이동되었습니다 (2008-03-11 14:00)  
175 흥크립트배경 어떤식으로 넣어야됨? [2] 세균맨 1751   2007-12-10 2008-03-17 04:37
갈쳐주셈 그리고 그림 순서 똑같게 잡으면 뭐나옴? * 똥똥배님에 의해서 게시물 이동되었습니다 (2008-03-11 14:00)  
174 흥크립트 또 질문 [2] A.미스릴 1712   2008-01-02 2008-03-17 04:37
@그림 0, "XX"를 했었는데 그후 나중에 @그림 0, 'YY"를 해서 그림을 바꿔버릴 수 있나요 추가로 다음버전에서 {{사람이름}체력} 형식도 사용할 수 있게 해주세요 * 똥똥배님에 의해서 게시물 이동되었습니다 (2008-03-11 14:00)  
173 [re] 똥똥배님께 질문 [1] 똥똥배 1682   2008-01-09 2008-03-17 04:37
1. 시스템메모리와 비디오메모리의 차이는 무엇입니까? 시스템 메모리는 말 그대로 컴퓨터에 달린 RAM이고, 비디오 메모리는 비디오카드에 있는 RAM입니다. 이론적으론 비디오 메모리가 용량이 작은 대신 속도가 빠르긴 한데... 시스템 메모리에 부른 것을 비...  
172 똥똥배님께 질문 [6] 흑곰 1730   2008-01-09 2008-03-17 04:37
1. 시스템메모리와 비디오메모리의 차이는 무엇입니까? 각각의 장단점은 무엇인지..? 그리고 무엇을 (주로) 사용하시는지? 2. 흥크립트는 txt파일을 한줄 읽고 처리하고, 또 한줄읽고 처리하나요? 아니면 전부 다 읽고 몇줄씩 한꺼번에 처리하나요? 3. 한줄읽...  
171 흥크립트 반전 버그 원인발견, 그리고 그 후폭풍 [4] 똥똥배 1691   2008-01-15 2008-03-17 04:37
흥크립트의 반전버그의 원인을 알았지만, 그게 간단한 문제가 아니라 거의 시스템을 다 엎어야 할 버그란 걸 깨달았습니다. 일단 흥크립트는 원본 그림은 그대로 둔 채 DuplicateSurface란 명령으로 그림을 복사해서 씁니다. 그런데 사실 복사라고 해서 그림을...  
170 흥크립트 다루다 보니 알아낸게 있는데... [4] 네모상자 1765   2008-01-18 2008-03-17 04:37
그림이 없으면 대화창이 투명해지지 않는군요. * 똥똥배님에 의해서 게시물 이동되었습니다 (2008-03-11 14:03)  
169 흥크립트 버그발견 [7] 네모상자 1754   2008-01-22 2008-03-17 04:37
한 줄에서 맨 앞에 {글자변수}를 사용하면 인식하지를 않음 예) {쥔공이름} : 네, 제 이름은 {쥔공이름}입니다(인식안함) 네, 제 이름은 {쥔공이름}입니다(인식함) * 똥똥배님에 의해서 게시물 이동되었습니다 (2008-03-11 14:03)  
168 흥크립트 원인불명 버그 [5] 흑곰 1891   2008-01-23 2008-03-17 04:37
마우스 클릭하다가 게임 자체가 멈춘다. 마우스 인식, 키 인식이 안되며(Alt+F4포함) 걸리면 도리가 없고, 열받아서 컴퓨터를 부숴버리고 싶어진다. 현재까지는 장펭돌, 흑곰이 걸렸다. 원인은 잘 모르겠지만 게임을 새로 시작할 때는 일어나지 않는 것 같고, ...  
167 흥크립트 질문! [2] 네모상자 1692   2008-01-28 2008-03-17 04:37
글자변수는 조건분기가 안되나요? * 똥똥배님에 의해서 게시물 이동되었습니다 (2008-03-11 14:03)  
166 흥크립트 궁금한점. [5] 장펭돌 1704   2008-01-31 2008-03-17 04:37
1. AVI 같은 동영상 파일이나, 플래시파일 재생 지원 가능한가여? 가능하다면... 뭐라 쓰면 되는지...? 2. 던전앤 러버처럼 마우스 클릭으로 실행되는 이벤트 만드는 방법 설명좀 부탁... * 똥똥배님에 의해서 게시물 이동되었습니다 (2008-03-11 14:03)