메뉴 건너뛰기

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

안녕하세요, 노루발입니다.

오늘로 그래픽 소코반을 끝내기로 했습니다.

아쉽지만 끝내야죠.


일단 걸음수와 이것저것을 뿌려주기로 합니다.

그런데 이미 화면이 꽉 차버려서 뿌려줄 공간이 없네요...

없으면 만들면 되지. 창을 늘립시다.


conf.lua를 수정.


function love.conf(t)
    t.title = "Lua 소코반 EX!"
    t.screen.width = 256
    t.screen.height = 288
end


세로를 32 더 늘렸습니다.


그리고 한글을 출력하고 싶으면 한글 폰트가 있어야 합니다.

'은바탕' 폰트를 첨부해 두었습니다.


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
   
    -- 폰트 불러오기
    korfont = love.graphics.newFont("UnBatang.ttf", 15) -- 은바탕 폰트, 15 크기로 불러온다.
end


그리고 love.draw 콜백에서 그립니다.

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
   
    -- 글씨를 쓴다.
    love.graphics.setFont(korfont)
    love.graphics.print("움직임: "..moves, 0, 255)
    if (successed() == true) then
        love.graphics.print("축하합니다.", 0, 270)
    else
        love.graphics.print("a: 왼쪽 s: 아래 d: 오른쪽 w: 위", 0, 270)
    end
end


모든 상자가 목표 위에 올라간 상태라면 축하 메세지를 띄우고

아니라면 조작을 설명하는 메세지를 띄웁니다.



function love.keypressed(key)
    if (successed() == false) then
        if (key == "a") then

~~ 너무 길어서 이하 생략


모든 상자가 목표 위에 올라간 상태가 아닐 경우에만 조작의 처리를 실행합니다.

모든 상자가 목표 위에 올라가 있다면 조작의 처리가 실행되지 않습니다.


완성된 버젼의 전체 코드를 요약글로 올립니다.

conf.lua:

function love.conf(t)
    t.title = "Lua 소코반 EX!"
    t.screen.width = 256
    t.screen.height = 288
end


main.lua:

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 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
   
    -- 폰트 불러오기
    korfont = love.graphics.newFont("UnBatang.ttf", 15)
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
   
    -- 글씨를 쓴다.
    love.graphics.setFont(korfont)
    love.graphics.print("움직임: "..moves, 0, 255)
    if (successed() == true) then
        love.graphics.print("축하합니다.", 0, 270)
    else
        love.graphics.print("a: 왼쪽 s: 아래 d: 오른쪽 w: 위", 0, 270)
    end
end

function love.keypressed(key)
    if (successed() == false) then
        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
end


조회 수 :
913
등록일 :
2013.09.18
08:05:48 (*.209.135.92)
엮인글 :
게시글 주소 :
https://hondoom.com/zbxe/index.php?mid=study&document_srl=703968
List of Articles
번호 제목 글쓴이 조회 수 추천 수sort 날짜 최근 수정일
» Lua 소코반 EX: 포팅: 3 (머나먼 여행길 안녕 친구여) 노루발 913   2013-09-18 2013-09-18 08:05
안녕하세요, 노루발입니다. 오늘로 그래픽 소코반을 끝내기로 했습니다. 아쉽지만 끝내야죠. 일단 걸음수와 이것저것을 뿌려주기로 합니다. 그런데 이미 화면이 꽉 차버려서 뿌려줄 공간이 없네요... 없으면 만들면 되지. 창을 늘립시다. conf.lua를 수정. fun...  
24 CCTextFieldTTF 0바이트 메모리 누수 버그 해결법 똥똥배 692   2014-01-13 2014-01-13 10:38
CCTextFieldTTF::textFieldWithPlaceHolder("", FONT_NAME, FONT_SIZE); 이런 식으로 텍스트필드를 생성하면 ""가 0바이트라서 해체를 못하고 0바이트 누수가 발생한다. 그냥 스페이스(" ")라도 넣어주면 누수는 발생하지 않는다. 이 버그가 발생하는 버전은 2...  
23 Cocostudio의 ActionNode 메모리 누수 해결법(cocos2d-x 2.2.2) [2] 똥똥배 776   2014-03-09 2014-05-31 19:26
Cocostudio로 액션을 만들고 액션매니저를 이용해서 ActionManager::shareManager()->playActionByName 아래와 같이 실행을 해주면 액션이 실행되는 경우 메모리 누수가 발생한다. (액션을 실행하지 않았을 때는 메모리 누수 없음) 이 메모리 누수를 없애려면 ...  
22 cocos2d-x 2.2.2 윈도우 환경 기본 메모리 누수 똥똥배 640   2014-03-10 2014-03-11 03:51
cocos2d-x 2.2.2를 윈도우에서 실행시키고 나서 종료하면 기본으로 4바이트의 메모리 누수가 발생한다. 이건 CCScriptEngineManager가 원인인데 CCObject들은 delete를 실행할 때마다 CCScriptEngineManager의 sharedManager를 호출하기 때문이다. 결국 모든 C...  
21 cocos2d-x 2.2.2 UILabelBMFont 메모리 누수 해결법 [2] 똥똥배 783   2014-03-10 2014-05-31 19:30
만약 CocoStudio로 UI를 만든 후 GUIReader를 써서 지정 폰트를 불러오면 엔진 버그로 인해서 메모리 누수가 생긴다. 이걸 고치려면 LabelBMFont.cpp를 아래와 같이 수정해야 한다. void LabelBMFont::setFntFile(const char *fileName) { if (!fileName || st...  
20 cocos2d-x 2.2.2 문자열 출력 버그 [2] 똥똥배 601   2014-06-10 2014-08-23 22:35
cocos2d-x 2.2.2, 맥에서만 나타는 문제다. 윈도우, 안드로이드에서는 아무 이상 없다. 줄 수가 꽤 긴 문자열을 출력할 경우, 문자가 전부 다 나오지 않고 잘린다. 잘리는 기준은 줄이다. 문자 수가 어떻든 간에 줄 수에 따라서 잘려버린다. 아마 문자열의 줄 ...  
19 Love2d 게임 중간에 광고 표시 [1] 노루발 377   2015-11-12 2015-11-17 00:16
http://love2d.org/forums/viewtopic.php?f=11&t=81224  
18 구글 인앱 구매 Soomla로 구현해본 후 팁 똥똥배 577   2014-09-20 2014-09-20 18:45
1. 일단 알파 테스트라도 앱을 출시시켜야 인앱이 작동한다. 그리고 APK 업로드 후에 바로 인앱이 적용되지 않는다. 구글에 게시되는 데, 시간이 걸리므로 인앱 등록 - APK 등록을 마친 후 다음날부터 구현하는 게 깔끔하다. 이걸로 모르고 하루종일 왜 안되나...  
17 Love2d 여러 플랫폼으로 빌드 자동화 노루발 470   2014-11-12 2014-11-12 17:35
https://github.com/MisterDA/love-release/blob/master/README.md http://www.ambience.sk/lua-love2d-game-distribution/  
16 Love2d 안드로이드 빌드하기 노루발 572   2014-12-15 2014-12-15 00:59
Love2d의 안드로이드 포트는 알파 단계입니다. 차차 개선되어 나가긴 하겠지만 아직 불안정한 부분이 많으며, 일어날 수 있는 오작동과 그로 인한 결과는 일절 책임지지 않습니다.   이 문서는 Windows 사용자 기준입니다. Linux나 Mac을 사용할 정도의 내공...  
15 Love2d 안드로이드 게임 패키징하기 [3] 노루발 546   2014-12-15 2021-01-11 12:11
이 문서는 개발 환경이 갖추어져 있는 상태이고, 빌드를 무사히 마친 뒤라고 가정합니다. 또한 이 문서는 https://bitbucket.org/MartinFelis/love-android-sdl2/wiki/Game%20Packaging#markdown-header-how-to-package-the-apk-with-your-own-love-090-game ...  
14 [번역] gamedev레딧의 Getting Started 문서 번역 [5] priling 1891   2014-12-26 2018-07-24 10:33
처음인 분들을 위한 '게임만들기' 가이드이 글은 [레딧 게임개발 커뮤니티의 /u/LordNed님의 포스팅]을 베이스로 작성한 것입니다. 이 글의 목적은 게임을 만들고 싶어하는 분들이 어떻게 시작할 수 있을지에 대해 명확한 가이드라인을 보여드리는 것입니다.  ...  
13 김프로 이미지 맵 만들기 노루발 393   2015-11-11 2015-11-11 08:05
https://docs.gimp.org/en/plug-in-imagemap.html  
12 RPG Maker MV 로컬라이징 방법 file 똥똥배 915   2015-10-27 2015-10-27 05:51
 
11 리캡챠 적용 [1] 노루발 11   2021-01-08 2021-01-11 12:15
XE 회원가입 시 구글 리캡챠 인증 추가하기 : 네이버 블로그 (naver.com)  
10 자동화된 Lua 스크립트의 문서화 - LDoc 노루발 43227   2021-01-11 2021-01-11 11:53
다운로드 https://github.com/lunarmodules/LDoc penlight 설치가 필요 luarocks install penlight 프로젝트가 있는 폴더에서 아래의 명령행을 실행 lua /path/to/ldoc/ldoc.lua $* https://stevedonovan.github.io/ldoc/manual/doc.md.html 문서 코멘트라는걸...  
9 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...  
8 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 알아서 다 해주기...  
7 PHP로 웹게임 만드는 영상 [1] 노루발 406   2021-06-25 2022-01-28 03:40
Simple PHP Strategy Game - YouTube  
6 Lua-love2d TCP 통신 [1] 노루발 21   2023-07-14 2023-07-22 16:19
서버: Lua 클라이언트: Love2d(Lua) 서버 구동에는 luasocket 라이브러리가 필요하며, luarocks로 설치할 수 있음. 별도 패키지 관리자가 있는 리눅스 시스템에서는 apt-get install lua-socket 등의 패키지 관리자 명령어로 설치 가능하며 Windows에서 구동시...