메뉴 건너뛰기

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

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

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

아쉽지만 끝내야죠.


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

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

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


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 날짜 최근 수정일
65 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...  
64 Lua 클래스 만들고 활용하기 노루발 23   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...  
63 특정좌표를 기준으로 zoom in/zoom out하기 노루발 58   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좌표...  
62 love2d에서 안드로이드 터치 제스처 인식하기 노루발 20   2020-11-12 2020-11-12 00:38
-- 안드로이드/iOS 등등의 터치스크린 입력을 받는 기기에서는 총 3가지의 콜백 함수를 사용한다: -- love.touchpressed / love.touchmoved / love.touchreleased -- 다만 위 3가지 함수는 마우스 클릭으로는 발생하지 않으므로 추가적인 처리가 필요하다. -- ...  
61 이쁜 눈나가 유니티 개발 알려주는 재생목록 노루발 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 유니티를 배우고 있는 건 아닌데 만들어진 라이브러리를 다루는게 아...  
60 턴 기반 시스템 구현에 대한 글 [4] 노루발 440   2020-11-14 2020-11-18 05:49
플레이어와 NPC들의 모든 행동에 1턴이 소요된다면 턴 기반 시스템의 구현이 쉽겠지만 (사실 엄청 쉽지는 않다... 게임엔진은 실시간으로 돌아가는데 행동은 턴으로 제약해야 하니) 전략성을 요구하기 위해 행동에 소모되는 턴을 다르게 설정한다면 다소 생각...  
59 루아 스타일 가이드 노루발 33   2020-11-19 2020-11-19 00:58
http://lua-users.org/wiki/LuaStyleGuide https://github.com/Olivine-Labs/lua-style-guide  
58 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) 하지만 이미지를 하얗게...  
57 Love2d로 만든 로그라이크 예제 노루발 252   2020-11-30 2020-11-30 22:54
https://gitlab.com/Jalexander39/roguelikedev-does-the-complete-roguelike-tutorial 이걸 왜 여태 몰랐지...  
56 Oracle cloud에 Nginx/MariaDB 설치하기 노루발 93   2020-12-06 2020-12-06 20:19
https://itreport.tistory.com/624  
55 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 크기로 보여야겠지만 내 컴퓨터에서는 이렇게 보...  
54 cocos2d-x 외부파일을 이용한 한글 처리 [1] 똥똥배 1938   2013-07-08 2013-09-13 07:29
cocos2d-x에서는 한글을 그냥 출력하려고 하면 깨져서 나온다. 이유는 VS 편집기에서는 ANSI코드 한글을 사용하는데, cocos2d-x에서 문자는 UTF-8 형식을 쓰기 때문이다. 이것을 해결하는 간단한 방법은 wchar_t wmsg[] = L"한글"; char msg[128]; WideChar...  
53 QT 프로젝트 배포에 필요한 거 똥똥배 602   2013-07-11 2013-07-11 04:25
일단 빌드한 EXE와 리소스 파일은 당연히 필요할 테고, 그 다음으로, QtCore4.dll QtGui4.dll 파일이 필요하다. 디버그 모드라면 QtCored4.dll, QtGuid4.dll 가 필요하겠지만, 디버그 모드를 배포할 생각은 아닐 것이므로 필요없을 듯. 하지만 이것만으로 끝난...  
52 cocos2d-x에서 schedule_selector 정의 변화 [1] 똥똥배 521   2013-08-19 2013-09-13 07:28
예전에 적은 글에는 터치와 업데이트를 활성화 시키는 법을 적어놨다. http://hondoom.com/zbxe/index.php?mid=study&document_srl=385031 이글은 cocos2d-1.0.1-x-0.9.1를 기준으로 적은 글인데, 최신버전으로 오면서 update의 정의에 대해서 변화가 생겼...  
51 cocos2d-x 게임을 iOS에 이식할 때 생기는 문제들 똥똥배 543   2013-08-26 2013-09-13 07:28
1. 윈도우 한정 명령어는 사용할 수 없다. 너무도 당연한 문제. 2. 한글 인코딩 문제 최신 VS을 사용하면 겪지 않을 문제일지 모르겠지만, 보통은 h나 cpp를 ANISI형식 문서로 만들 것이다. XCode에서는 Unicode나 UTF-8 형식을 쓰므로 소스에 박아둔 한글은 ...  
50 cocos2d-x Clipping Layer 수정 똥똥배 670   2013-09-10 2013-09-13 07:28
흔히 구글에서 찾아보면 다음과 같은 Clipping Layer 소스를 찾을 수 있다. CCEGLView::sharedOpenGLView()->setScissorInPoints( // scissorRect is the rectangle you want to show. clippingRegion.origin.x + getPosition().x, clippingRegion.origin.y + ...  
49 cocos2d-x CCMenuItem 자신을 지웠을 때 생기는 에러 똥똥배 442   2013-09-13 2013-09-13 07:28
CCMenuItem으로 메뉴를 생성하고 타겟과 셀렉터를 정해서 그 명령 안에서 자신을 지우게 되었을 경우 activate() 안에서 에러가 발생한다. 아직 activate()를 실행해야 되는데 remove 당했기 때문에 생긴 문제인데, 구글에서 검색해 본 결과, https://github.c...  
48 [Lua] 테이블 노루발 1422   2013-09-17 2013-09-17 08:13
이것은 Lua 문법입니다. Love2D와는 관계가 없습니다.  안녕하세요, 노루발입니다. 오늘은 테이블에 대해서 설명을 해보겠습니다.   테이블이란 쉽게 말해 변수를 묶은 겁니다. Lua상에서 뭔가 쌈빡하게 테이블을 쓰는 법도 있는 듯 하지만 저는 그냥 타 언어...  
47 love.update(dt) 에서 버벅이는 현상. 노루발 256   2013-09-17 2013-09-17 08:15
윈도우를 잡고 흔들거나 윈도우 사이로 잠시 전환하거나.. 등으로 원치 않는 렉이 발생할 시 dt의 값이 평소보다 크게 들어갑니다. (예를 들면, 평소에 0.25가 들어간다면 이번에는 3.1이 들어갑니다.) 이건 평소보다 더 많이 크기 때문에, dt를 가지고 타이...  
46 [Lua] Split 노루발 361   2013-09-17 2013-09-17 08:16
상황을 가정해보자. 웹 서버에 부탁해서 유희왕 덱의 리스트를 가져왔다. 원문은 대략 이렇다고 가정하자.   셰이프스내치x3 모린팬x3 트렌트x3 ...    보기엔 이렇지만 이걸 가져오면 아래와 같이 뜰 것이다. (웹 서버니까 HTML로 줌) 셰이프스내치x3<br>모...