메뉴 건너뛰기

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

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 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 추천 수 날짜 최근 수정일
165 COgg 질문 [3] A.미스릴 3710   2008-06-29 2013-11-23 08:43
밑에 대슬님 질문 보니까 멤버변수를 등록할때 동적할당을 하는게 씌여 있는데 그냥 Cogg m_ogg_oggplayer; 이런식으로 그냥 일반변수로 등록을 하고 사용할순 없나요? 동적할당을 굳이 써야하는지 ㅡ.ㅡ;; (개인적으로 4바이트 추가와 잘못되면 메모리 누수가...  
164 문D라이브로 더블드래곤을 만들자(2) [6] file 똥똥배 3611   2008-04-18 2013-11-23 08:43
 
163 C++ 질문 2 [3] A.미스릴 3565   2008-12-22 2008-12-23 18:47
바이너리(2진수) 저장 아시죠? 그런데 포인터 변수를 만들어서 동적 할당을 했을때 그 포인터 변수도 포함되어 저장되면 정상적으로 저장이 안 되는 건가요? 왜 그러냐면 제가 저장 방식을 모든 데이터를 하나의 클래스 속에 넣어서 그 1개의 클래스 인스턴스...  
162 문D라이브로 더블드래곤을 만들자(7) file 똥똥배 3480   2008-04-27 2008-04-28 04:22
 
161 lua와 C의 연동에서 상수(define이나 enum) 값처리 똥똥배 3448   2011-05-25 2011-05-25 23:34
이 문제의 해결에 4가지 방법이 나왔다. 물론 환경이나 상황에 따라 더 많은 해결방법이 나올 수 있고, 어떤 것이 답인지는 그때 그때 마다 다른다. 일단 상황부터 정리. 스프라이터를 처리하는 툴에서는 스프라이터 애니메이션을 숫자가 아닌 특정 코멘트를 ...  
160 흥크립트 질문. 글자에 관해서 [1] 에리 3430   2009-03-21 2009-03-21 09:30
예를 들어 글자입력으로 ㅇㅇㅇㅊ ㄱㄴㄱㅇ ㅅㅌ ㅋㄴㅇ ㄴㄴㅇ 대충 이런 글자들을 입력했다 치고 분기 명령어로 분기를 만들 수 있나요?  
159 문D라이브로 더블드래곤을 만들자(3) file 똥똥배 3430   2008-04-18 2008-04-18 18:27
 
158 [수정]이거왜이러는거죠;; [4] file 상상악수 3254   2008-08-19 2008-08-21 03:24
 
157 그럼 질문으로... [1] 쿠로쇼우 3243   2008-09-26 2009-01-07 22:05
c++에서 텍스트파일을 입,출력 하는것은 어느정도 알겠습니다. 문자라든가, 띄어쓰기, <┘ 세고, 출력하는것도 어느정도 알겠고요,, 근데 한줄 단위로 입력하고, 한줄 단위로 출력하는건 어떻해야 하나요? 오늘 아침 질문(??) 올린거에서 텍스트 파일 입력한거 ...  
156 문D라이브로 더블드래곤을 만들자(4) [2] file 똥똥배 3213   2008-04-20 2008-04-21 07:20
 
155 OgreOde 사용기 똥똥배 3184   2008-03-25 2008-03-25 21:37
OgreSDK 버전 1.4.7 OgreOde 버전 0.95(아마도) 사실 오우거 엔진 쓴지도 얼마 안 되고 물리엔진은 처음 만져봤습니다. 처음에 Ogre Wiki에 나온대로 따라서 만들었는데 crateCube.mesh와 plane.mesh 때문에 에러가 나서 바닥은 직접 만들고 crateCube 대신 오...  
154 문D라이브로 더블드래곤을 만들자(6) [2] file 똥똥배 3114   2008-04-23 2008-04-25 05:01
 
153 문D라이브로 더블드래곤을 만들자(5) [6] file 똥똥배 3072   2008-04-21 2008-04-24 03:23
 
152 C++ 데이터의 바이트 용량 임의로 정의할수 없나영 [1] A.미스릴 3059   2008-04-21 2008-04-21 07:18
int는 4바이트로 정해져 있는데 약간의 수만 있으면 되는 수도 있는데 괜히 많은 숫자를 사용해서 메모리를 많이 사용하는 건 아닐지 ㅡㅡ; 3바이트라던지 4비트라던지... 데이터의 바이트 사용량을 임의로 바꿀수 없나요  
151 똥똥배의 게임대회 이야기(3) [1] 혼돈 3007   2007-02-08 2011-02-11 12:53
3. 아마추어다운 게임이란 무엇인가? 아마추어는 자유롭습니다. 자신이 원한다면 무엇이든 표현할 수 있습니다. (심하게 비윤리적이라면 문제가 되겟지만) 하지만 프로는 그렇지 못합니다. 일단 상업성 때문에 '돈이 되는' 게임을 만들어야 하고, 수많은 사람...  
150 문D라이브로 더블드래곤을 만들자(8) [1] file 똥똥배 2973   2008-05-16 2009-01-07 22:05
 
149 똥똥배의 게임대회 이야기(1) 혼돈 2953   2007-02-06 2008-03-17 04:37
지금부터 제 이야기를 잘 들어 두시는 게 수상의 열쇠가 될지도 모릅니다. 제가 끝판 보스도 아니고, 공지 내놓고 마감일까지 기다리는 것은 지루하니 이런 저런 이야기를 들려 드리도록 하죠. 1. 지금 여러분이 해야 할 일 지금 개학인데 왜 하필 시작하냐는 ...  
148 문D 라이브 질문 [5] 대슬 2889   2008-05-15 2008-05-16 06:13
1. 음악이나 사운드는 어떻게 불러와서 출력하나요. 배웠었는데 까먹었음. 2. 그림을 원하는 각도로 자유롭게 회전시켜서 출력하는 기능은 없나요?  
147 문D라이브로 더블드래곤을 만들자(9) 똥똥배 2889   2008-05-17 2008-05-17 03:07
9편 : 체계화된 동작 지금까지 열심히 이 강의를 따라오신 분들이라면 지금 게임의 여러 버그가 산재해 있음을 깨달으셨을겁니다. 그걸 왜 그냥 내버려 뒀나면... 귀찮아서~ ~는 훼이크고 일단 가르치는 주제에서 벗어나면 집중력이 떨어지고 어느 정도 이해력...  
146 똥똥배의 게임대회 이야기(4) [1] 혼돈 2881   2007-02-10 2011-02-11 12:52
4. 공모전도 커뮤니케이션 게임을 만들기 전에 먼저 생각해야 하는 것은 게임을 하는 사람들입니다. 그래서 게임회사라면 시장조사가 가장 먼저 해야 할 일입니다. 회사에서 만든 게임을 얼마나 많은 게이머들이 즐길지 알고 예상되는 수익을 계산하고 거기에 ...