메뉴 건너뛰기

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

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

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

아쉽지만 끝내야죠.


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

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

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


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
165 COgg 질문 [3] A.미스릴 2008-06-29 3710
164 문D라이브로 더블드래곤을 만들자(2) [6] file 똥똥배 2008-04-18 3611
163 C++ 질문 2 [3] A.미스릴 2008-12-22 3565
162 문D라이브로 더블드래곤을 만들자(7) file 똥똥배 2008-04-27 3480
161 lua와 C의 연동에서 상수(define이나 enum) 값처리 똥똥배 2011-05-25 3447
160 흥크립트 질문. 글자에 관해서 [1] 에리 2009-03-21 3430
159 문D라이브로 더블드래곤을 만들자(3) file 똥똥배 2008-04-18 3430
158 [수정]이거왜이러는거죠;; [4] file 상상악수 2008-08-19 3254
157 그럼 질문으로... [1] 쿠로쇼우 2008-09-26 3243
156 문D라이브로 더블드래곤을 만들자(4) [2] file 똥똥배 2008-04-20 3213
155 OgreOde 사용기 똥똥배 2008-03-25 3184
154 문D라이브로 더블드래곤을 만들자(6) [2] file 똥똥배 2008-04-23 3114
153 문D라이브로 더블드래곤을 만들자(5) [6] file 똥똥배 2008-04-21 3072
152 C++ 데이터의 바이트 용량 임의로 정의할수 없나영 [1] A.미스릴 2008-04-21 3059
151 똥똥배의 게임대회 이야기(3) [1] 혼돈 2007-02-08 3007
150 문D라이브로 더블드래곤을 만들자(8) [1] file 똥똥배 2008-05-16 2973
149 똥똥배의 게임대회 이야기(1) 혼돈 2007-02-06 2953
148 문D 라이브 질문 [5] 대슬 2008-05-15 2889
147 문D라이브로 더블드래곤을 만들자(9) 똥똥배 2008-05-17 2889
146 똥똥배의 게임대회 이야기(4) [1] 혼돈 2007-02-10 2881