메뉴 건너뛰기

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

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 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 추천 수 날짜 최근 수정일
45 SRPG을 구현하게 되면 ... [3] 짜스터 2709   2010-10-05 2013-11-23 04:38
지금 도트 제작단계로 넘어갈 예정인데 SRPG 시스템을 지원 해주실거면 무슨 툴로 하실건가요? 해상도에 따라서 도트 크기도 맞춰야 하는터라. 혹시나 생각하신것이 있으면 알려주세요.  
44 질문요.C++ [7] 질문자01 2729   2007-05-10 2008-04-03 05:32
도대체 파라미터가 뭔지 모르겠어요.  
43 DirectX 창모드에서 화면 지워지는 문제 [7] file 똥똥배 2790   2010-12-12 2010-12-19 03:36
 
42 똥똥배의 게임대회 이야기(2) [2] 혼돈 2816   2007-02-07 2008-03-17 04:37
2. 100KB 공모전의 추억 1회 규칙이 10M로 제한 된 것은 과거 100KB 공모전이 담겨있어서 입니다. 과거 100KB 공모전이라고 있었는데, 그 제한된 용량을 이용해서 다양한 작품이 나왔었죠. 그 중에 우수상을 탔던 것이 '삭제 되었수다'로서 오히려 제한된 용량...  
41 또질문 [2] 쿠로쇼우 2854   2008-09-29 2008-09-29 06:43
#include <stdio.h> int main (void) { int i=1,a=1,b=0,c=0,d=0; char ch[50]; char ca=0; FILE * file = fopen("a.txt", "rt"); if(file==NULL){ printf("NULL"); return 1; } while(1) { fgets(ch, 50, file); if(feof(file)!=0){ break;} else { fscanf(fi...  
40 똥똥배의 게임대회 이야기(4) [1] 혼돈 2881   2007-02-10 2011-02-11 12:52
4. 공모전도 커뮤니케이션 게임을 만들기 전에 먼저 생각해야 하는 것은 게임을 하는 사람들입니다. 그래서 게임회사라면 시장조사가 가장 먼저 해야 할 일입니다. 회사에서 만든 게임을 얼마나 많은 게이머들이 즐길지 알고 예상되는 수익을 계산하고 거기에 ...  
39 문D라이브로 더블드래곤을 만들자(9) 똥똥배 2889   2008-05-17 2008-05-17 03:07
9편 : 체계화된 동작 지금까지 열심히 이 강의를 따라오신 분들이라면 지금 게임의 여러 버그가 산재해 있음을 깨달으셨을겁니다. 그걸 왜 그냥 내버려 뒀나면... 귀찮아서~ ~는 훼이크고 일단 가르치는 주제에서 벗어나면 집중력이 떨어지고 어느 정도 이해력...  
38 문D 라이브 질문 [5] 대슬 2889   2008-05-15 2008-05-16 06:13
1. 음악이나 사운드는 어떻게 불러와서 출력하나요. 배웠었는데 까먹었음. 2. 그림을 원하는 각도로 자유롭게 회전시켜서 출력하는 기능은 없나요?  
37 똥똥배의 게임대회 이야기(1) 혼돈 2953   2007-02-06 2008-03-17 04:37
지금부터 제 이야기를 잘 들어 두시는 게 수상의 열쇠가 될지도 모릅니다. 제가 끝판 보스도 아니고, 공지 내놓고 마감일까지 기다리는 것은 지루하니 이런 저런 이야기를 들려 드리도록 하죠. 1. 지금 여러분이 해야 할 일 지금 개학인데 왜 하필 시작하냐는 ...  
36 문D라이브로 더블드래곤을 만들자(8) [1] file 똥똥배 2973   2008-05-16 2009-01-07 22:05
 
35 똥똥배의 게임대회 이야기(3) [1] 혼돈 3007   2007-02-08 2011-02-11 12:53
3. 아마추어다운 게임이란 무엇인가? 아마추어는 자유롭습니다. 자신이 원한다면 무엇이든 표현할 수 있습니다. (심하게 비윤리적이라면 문제가 되겟지만) 하지만 프로는 그렇지 못합니다. 일단 상업성 때문에 '돈이 되는' 게임을 만들어야 하고, 수많은 사람...  
34 C++ 데이터의 바이트 용량 임의로 정의할수 없나영 [1] A.미스릴 3059   2008-04-21 2008-04-21 07:18
int는 4바이트로 정해져 있는데 약간의 수만 있으면 되는 수도 있는데 괜히 많은 숫자를 사용해서 메모리를 많이 사용하는 건 아닐지 ㅡㅡ; 3바이트라던지 4비트라던지... 데이터의 바이트 사용량을 임의로 바꿀수 없나요  
33 문D라이브로 더블드래곤을 만들자(5) [6] file 똥똥배 3072   2008-04-21 2008-04-24 03:23
 
32 문D라이브로 더블드래곤을 만들자(6) [2] file 똥똥배 3114   2008-04-23 2008-04-25 05:01
 
31 OgreOde 사용기 똥똥배 3184   2008-03-25 2008-03-25 21:37
OgreSDK 버전 1.4.7 OgreOde 버전 0.95(아마도) 사실 오우거 엔진 쓴지도 얼마 안 되고 물리엔진은 처음 만져봤습니다. 처음에 Ogre Wiki에 나온대로 따라서 만들었는데 crateCube.mesh와 plane.mesh 때문에 에러가 나서 바닥은 직접 만들고 crateCube 대신 오...  
30 문D라이브로 더블드래곤을 만들자(4) [2] file 똥똥배 3213   2008-04-20 2008-04-21 07:20
 
29 그럼 질문으로... [1] 쿠로쇼우 3243   2008-09-26 2009-01-07 22:05
c++에서 텍스트파일을 입,출력 하는것은 어느정도 알겠습니다. 문자라든가, 띄어쓰기, <┘ 세고, 출력하는것도 어느정도 알겠고요,, 근데 한줄 단위로 입력하고, 한줄 단위로 출력하는건 어떻해야 하나요? 오늘 아침 질문(??) 올린거에서 텍스트 파일 입력한거 ...  
28 [수정]이거왜이러는거죠;; [4] file 상상악수 3254   2008-08-19 2008-08-21 03:24
 
27 문D라이브로 더블드래곤을 만들자(3) file 똥똥배 3430   2008-04-18 2008-04-18 18:27
 
26 흥크립트 질문. 글자에 관해서 [1] 에리 3430   2009-03-21 2009-03-21 09:30
예를 들어 글자입력으로 ㅇㅇㅇㅊ ㄱㄴㄱㅇ ㅅㅌ ㅋㄴㅇ ㄴㄴㅇ 대충 이런 글자들을 입력했다 치고 분기 명령어로 분기를 만들 수 있나요?