메뉴 건너뛰기

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

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 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

function printmap()
-- 맵을 표시하는 printmap 함수
    tempstr = ""

    for a = 1, 8 do
        for b= 1, 8 do
            tempbuffer = ""
            if (map[a][b] == 0) then
                tempbuffer = " "
            elseif (map[a][b] == 1) then
                tempbuffer = "#"
            end
            
        for c = 1, #box do
            if (box[c]["x"] == b and box[c]["y"] == a) then
                tempbuffer = "o"
                break
            end
        end
        
        for c = 1, #goal do
            if (goal[c]["x"] == b and goal[c]["y"] == a) then
                if (tempbuffer == "o") then -- 상자와 목표 지점이 겹친다면 @로 표시
                    tempbuffer = "@"
                else
                    tempbuffer = "."
                end
                break
            end
        end
            
        if (playerx == b and playery == a) then
            tempbuffer = "P"
        end
        
        tempstr = tempstr..tempbuffer
        end
        print(tempstr)
        tempstr = ""
    end
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

while(true) do
    -- 메인 무한 루프
    os.execute("clear") -- 화면을 지운다. OS가 아닌 Scite 환경에서는 제대로 동작하지 않음.
    printmap()
    print("")
    print("움직임: "..moves)
    if (successed() == true) then -- 모든 상자를 목표 지점으로 옮김.
        print("축하합니다!")
        print("당신은 주어진 과업을 무사히 완수하였습니다!")
        io.read()
        return
    else --아직 더 옮겨야 함! 게임이 안 끝났음...
        print("어느 방향으로 이동할까요?")
        print("a: 왼쪽 d: 오른쪽 w: 위 s: 아래 q: 종료 :")
        choose = io.read()
        if (choose == "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 (choose == "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 (choose == "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 (choose == "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
        elseif (choose == "q") then
            os.execute("clear")
            print("그럼 안녕!")
            io.read()
            return
        end
    end
end


코드는 요약글로,


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


목표지점에 관한 코드는 박스에 관한 코드와 상당히 유사합니다.


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


모든 목표 지점 위에 박스가 놓여 있는지를 확인한다.

주석이랑은 말이 살짝 다른데, 그거나 그거나...


if (goal[c]["x"] == b and goal[c]["y"] == a) then
                if (tempbuffer == "o") then -- 상자와 목표 지점이 겹친다면 @로 표시
                    tempbuffer = "@"
                else
                    tempbuffer = "."
                end
                break
            end


목표 지점과 상자가 겹칠 경우, 알기 쉽게 저렇게 표시한다.


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


맵 위에 목표 지점을 만든다.


    if (successed() == true) then -- 모든 상자를 목표 지점으로 옮김.
        print("축하합니다!")
        print("당신은 주어진 과업을 무사히 완수하였습니다!")
        io.read()
        return
    else --아직 더 옮겨야 함! 게임이 안 끝났음...


모든 목표 지점 위에 박스가 올라가있다면 축하한다고 메세지를 보여주고 끝낸다.


이제 게임의 뼈대는 다 만들어졌습니다. 갖고 놀 수 있어요.

그리고, 방금 게임을 더 재미있게 만들기 위해서 생각난 기능이 있습니다.

첫번째는 맵 에디터와 맵 고르기 기능입니다. 맵 하나만 플레이하자니 지루하고

또 코드를 직접 뜯어고치자니 번거로우므로 맵 데이터 프로그램을 하나 만들어서 맵을 만들고

만들어진 맵을 읽어와서 플레이 할 수 있게 하는 것도 좋을 것 같고요.

또 하나는 랭킹입니다.

최대한 적은 움직임 수로 상자를 옮긴 사람 순으로 기록해서 플레이어들에게 경쟁심을 유발시키는 겁니다.


뭐.. 이런 게 생각났다는 것이지, 만들 생각은 아직 없습니다.

(언제는 무슨 맵에디터 만들 것 처럼 굴더니만!)

나중에 잉여력이 넘치면 만들어보도록 하겠습니다.


첨부 파일은 완성된 파일입니다. (본 글에는 포함되어 있지 않음.)

nokoban.lua 라는 것으로 exe가 아니라 아쉽게도 실행할 수 없습니다.

lua 인터프리터를 설치하면 PC에서도 맥에서도 리눅스에서도(제가 리눅스입니다!), 심지어 안드로이드나 ios 환경에서도 실행이 가능합니다.. 만 여러분은 lua 인터프리터도 없고 그걸 깔지도 않으시겠죠.

그러므로 리눅스 환경에서의 구동 스크린샷을 보여드리겠습니다. (본 글에는 포함되어 있지 않음.)

조회 수 :
754
등록일 :
2013.09.17
08:33:59 (*.209.135.92)
엮인글 :
게시글 주소 :
https://hondoom.com/zbxe/index.php?mid=study&document_srl=703465
List of Articles
번호 제목 글쓴이 조회 수sort 추천 수 날짜 최근 수정일
185 express.js 세션 적용 후 리다이렉트 시 세션 적용이 제대로 안 되는 문제점 노루발 4   2024-02-07 2024-02-07 14:23
문제: logout.js router.get('/', (req, res) => { req.session.destroy(); res.redirect('/login'); }); login.js if (result[0] !== undefined) { // 로그인에 성공하였으므로 세션을 할당 req.session.uid = result[0].uid; req.session.us...  
184 Bootstrap4 container class가 적용된 div의 양 옆에 설정하지 않은 margin이 생김 노루발 5   2024-02-07 2024-02-07 14:25
문제: <div class="container"> TEST </div> 위와 같은 페이지를 브라우저에서 렌더링 시 div의 양 옆에 설정하지 않은 빈 margin이 생김. 해결: <div class="container-fluid"> TEST </div> margin을 0으로 주고 width를 100%로 주고 등등 별걸 다 해봤는데 ...  
183 리캡챠 적용 [1] 노루발 11   2021-01-08 2021-01-11 12:15
XE 회원가입 시 구글 리캡챠 인증 추가하기 : 네이버 블로그 (naver.com)  
182 certbot을 이용한 HTTPS 인증서 발급 및 적용 노루발 18   2021-01-12 2021-01-12 16:57
snap 설치 및 업데이트 sudo snap install core; sudo snap refresh core certbot 설치 sudo snap install --classic certbot 심볼릭 링크 생성 sudo ln -s /snap/bin/certbot /usr/bin/certbot nginx에 맞춰 자동 설정 sudo certbot --nginx 알아서 다 해주기...  
181 love2d에서 안드로이드 터치 제스처 인식하기 노루발 20   2020-11-12 2020-11-12 00:38
-- 안드로이드/iOS 등등의 터치스크린 입력을 받는 기기에서는 총 3가지의 콜백 함수를 사용한다: -- love.touchpressed / love.touchmoved / love.touchreleased -- 다만 위 3가지 함수는 마우스 클릭으로는 발생하지 않으므로 추가적인 처리가 필요하다. -- ...  
180 Lua-love2d TCP 통신 [1] 노루발 22   2023-07-14 2023-07-22 16:19
서버: Lua 클라이언트: Love2d(Lua) 서버 구동에는 luasocket 라이브러리가 필요하며, luarocks로 설치할 수 있음. 별도 패키지 관리자가 있는 리눅스 시스템에서는 apt-get install lua-socket 등의 패키지 관리자 명령어로 설치 가능하며 Windows에서 구동시...  
179 illegal character 방지 [3] 노루발 22   2023-07-17 2023-07-19 16:14
문제점: 클라이언트 <-> 서버 통신을 하면서 다음과 같이 메세지를 주고받기로 함 패킷종류|파라미터1|파라미터2 예) MOTDREQ -> 서버에 MOTD를 요청 (파라미터 없음) MOTD|공지사항입니다 -> MOTD는 "공지사항입니다" 임 (파라미터 1개) MSG|김덕배|안녕하세...  
178 Lua 클래스 만들고 활용하기 노루발 24   2020-11-06 2020-11-06 22:43
------------------------ stairs.lua ------------------------ Stairs = {} function Stairs:new(x, y, floor, direction, locked) local newStairs = {x = x, y = y, floor = floor, direction = direction, locked = locked} self.__index = self retur...  
177 Lua 테이블 안에 함수 저장하기 노루발 28   2020-11-06 2020-11-06 21:07
테이블 안에 함수를 저장할 수 있다. function move(object, direction) print(object .. " moved to ".. direction) end scheduler = {} scheduler.queue = {} function scheduler.newentry(action, args) scheduler.queue[#scheduler.queue + 1] = {} schedu...  
176 루아 스타일 가이드 노루발 34   2020-11-19 2020-11-19 00:58
http://lua-users.org/wiki/LuaStyleGuide https://github.com/Olivine-Labs/lua-style-guide  
175 Lua 인수로 nil값이 들어왔을 경우 처리하기 노루발 37   2020-11-06 2020-11-06 20:29
or 연산자를 사용한다. 예제 코드: function moveplayer(direction) direction = direction or "nowhere" print("player moved to ".. direction) end moveplayer() --> "player moved to nowhere" moveplayer("north") -- "player moved to north" 이런 코드...  
174 이쁜 눈나가 유니티 개발 알려주는 재생목록 노루발 37   2020-11-12 2020-11-12 05:59
https://www.youtube.com/watch?v=Ur2jN6_si6c&list=PLi-ukGVOag_1lNphWV5S-xxWDe3XANpyE https://www.youtube.com/watch?v=sJClf9S7AMA&list=PLi-ukGVOag_0HR09oTs966Wt81IYYXlFH 유니티를 배우고 있는 건 아닌데 만들어진 라이브러리를 다루는게 아...  
173 Love2d 이미지 하얗게 그리기 노루발 42   2020-11-23 2020-11-23 04:11
아래와 같은 코드를 사용해 이미지에 색상을 적용할 수 있다. hamster = love.graphics.newImage("hamster.png") love.graphics.setColor(1, 0, 0) -- 빨간색으로 그리기 love.graphics.draw(hamster) love.graphics.setColor(1, 1, 1) 하지만 이미지를 하얗게...  
172 Love2d 게임 안드로이드로 패키징하기 노루발 48   2021-01-11 2021-02-21 01:45
http://hondoom.com/zbxe/index.php?mid=study&document_srl=797993 버전이 바뀌면서 빌드 방법이 바뀌었기에 다시 정리한다. 1. Android studio 설치 https://developer.android.com/studio/index.html SDK 플랫폼 - Android 11.0 [API 30] SDK 버전 - An...  
171 특정좌표를 기준으로 zoom in/zoom out하기 노루발 59   2020-11-11 2020-11-11 01:22
-- x,y - 줌인/줌아웃시 기준점 (좌표 이동이나 scale에 영향받지 않는 순수한 화면 좌표) -- scale - 현재 scale(1:그대로 0.5:1/2 사이즈 2:2배 사이즈) -- scaleinc - 얼마나 scale을 변화시킬것인가 (I usually use 0.1 or -0.1) -- camx - 카메라의 x좌표...  
170 Windows To Go와 R-Studio를 이용한 손실된 데이터 복구하기 노루발 89   2020-01-30 2020-01-30 19:52
하기의 복구 방법으로 모든 자료를 100% 복구할 수는 없으므로 자료의 손실 이전에 신뢰성 있고 주기적인 백업이 선행되어야 한다. 준비물: - 부팅 USB 혹은 외장 HDD/SSD - 복구한 자료를 저장할 외부 저장장치 (옵션) - R-Studio https://www.r-studio.com/ ...  
169 Oracle cloud에 Nginx/MariaDB 설치하기 노루발 93   2020-12-06 2020-12-06 20:19
https://itreport.tistory.com/624  
168 Love2d DPI 이슈 해결 [3] 노루발 97   2019-06-29 2019-07-01 06:34
이런 love 프로젝트가 있다고 하자. (conf.lua) function love.conf(t) t.window.width = 640 t.window.height = 360 end 창 크기를 640*480으로 설정한 뒤 실행하면 어떻게 보일까? 당연히 창 크기가 640*480 크기로 보여야겠지만 내 컴퓨터에서는 이렇게 보...  
167 Love2d로 만든 로그라이크 예제 노루발 253   2020-11-30 2020-11-30 22:54
https://gitlab.com/Jalexander39/roguelikedev-does-the-complete-roguelike-tutorial 이걸 왜 여태 몰랐지...  
166 love.update(dt) 에서 버벅이는 현상. 노루발 256   2013-09-17 2013-09-17 08:15
윈도우를 잡고 흔들거나 윈도우 사이로 잠시 전환하거나.. 등으로 원치 않는 렉이 발생할 시 dt의 값이 평소보다 크게 들어갑니다. (예를 들면, 평소에 0.25가 들어간다면 이번에는 3.1이 들어갑니다.) 이건 평소보다 더 많이 크기 때문에, dt를 가지고 타이...