메뉴 건너뛰기

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

전체 코드입니다.

슬슬 길어지니 요약글 기능이 필요해진달지..


-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 5, 5로 설정.
playerx = 5
playery = 5

-- 움직인 횟수 저장
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

-- 맵 데이터 초기화
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

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
        if (playerx == b and playery == a) then
            tempbuffer = "P"
        end
       
        tempstr = tempstr..tempbuffer
        end
        print(tempstr)
        tempstr = ""
    end
end

while(true) do
    -- 메인 무한 루프
    os.execute("clear") -- 화면을 지운다. OS가 아닌 Scite 환경에서는 제대로 동작하지 않음.
    printmap()
    print("")
    print("움직임: "..moves)
    print("어느 방향으로 이동할까요?")
    print("a: 왼쪽 d: 오른쪽 w: 위 s: 아래 q: 종료 :")
    choose = io.read()
    if (choose == "a") then
        if (map[playery][playerx - 1] == 0) then
            playerx = playerx - 1
            moves = moves + 1
        end
    elseif (choose == "s") then
        if (map[playery + 1][playerx] == 0) then
            playery = playery + 1
            moves = moves + 1
        end
    elseif (choose == "d") then
        if (map[playery][playerx + 1] == 0) then
            playerx = playerx + 1
            moves = moves + 1
        end
    elseif (choose == "w") then
        if (map[playery - 1][playerx] == 0) then
            playery = playery - 1
            moves = moves + 1
        end
    elseif (choose == "q") then
        os.execute("clear")
        print("그럼 안녕!")
        io.read()
        return
    end
end


바뀐 건 if-elseif 부분입니다.

if (choose == "a") then
        if (map[playery][playerx - 1] == 0) then
            playerx = playerx - 1
            moves = moves + 1
        end
    elseif (choose == "s") then
        if (map[playery + 1][playerx] == 0) then
            playery = playery + 1
            moves = moves + 1
        end
    elseif (choose == "d") then
        if (map[playery][playerx + 1] == 0) then
            playerx = playerx + 1
            moves = moves + 1
        end
    elseif (choose == "w") then
        if (map[playery - 1][playerx] == 0) then
            playery = playery - 1
            moves = moves + 1
        end


if 처리가 더 붙었습니다.

플레이어가 앞으로 이동하게 될 좌표의 2차원 배열을 확인해서 0, 즉 빈 공간일 때만 이동할 수 있게 했습니다.

그 이외의 값이 담겨 있다면 벽으로 간주하고 이동의 처리를 생략하고 다름 루프로 넘어갑니다.


그렇게 어려운 처리는 아니었습니다, 다음에는 상자를 만들어보고, 가능하면 상자를 미는 처리도 해보겠습니다.

왠지 날로 먹은 것 같은 포스트 하나지만.. 다음에 봅시다.


다음 Lua 소코반.

언젠가 씁니다.
조회 수 :
1068
등록일 :
2013.09.17
08:31:25 (*.209.135.92)
엮인글 :
게시글 주소 :
https://hondoom.com/xe/study/703458
List of Articles
번호 제목 글쓴이 조회 수 추천 수 날짜 최근 수정일
185 Bootstrap4 container class가 적용된 div의 양 옆에 설정하지 않은 margin이 생김 노루발 662   2024-02-07 2024-02-07 14:25
 
184 express.js 세션 적용 후 리다이렉트 시 세션 적용이 제대로 안 되는 문제점 노루발 554   2024-02-07 2024-02-07 14:23
 
183 illegal character 방지 [3] 노루발 746   2023-07-17 2023-07-19 16:14
 
182 Lua-love2d TCP 통신 [1] 노루발 737   2023-07-14 2023-07-22 16:19
 
181 PHP로 웹게임 만드는 영상 [1] 노루발 1554   2021-06-25 2022-01-28 03:40
 
180 팁 certbot을 이용한 HTTPS 인증서 발급 및 적용 노루발 687   2021-01-12 2021-01-12 16:57
 
179 Love2d 게임 안드로이드로 패키징하기 노루발 703   2021-01-11 2021-02-21 01:45
 
178 팁 자동화된 Lua 스크립트의 문서화 - LDoc 노루발 44088   2021-01-11 2021-01-11 11:53
 
177 팁 리캡챠 적용 [1] 노루발 699   2021-01-08 2021-01-11 12:15
 
176 팁 Oracle cloud에 Nginx/MariaDB 설치하기 노루발 691   2020-12-06 2020-12-06 20:19
 
175 팁 Love2d로 만든 로그라이크 예제 노루발 979   2020-11-30 2020-11-30 22:54
 
174 팁 Love2d 이미지 하얗게 그리기 노루발 658   2020-11-23 2020-11-23 04:11
 
173 팁 루아 스타일 가이드 노루발 708   2020-11-19 2020-11-19 00:58
 
172 팁 턴 기반 시스템 구현에 대한 글 [4] 노루발 1266   2020-11-14 2020-11-18 05:49
 
171 팁 이쁜 눈나가 유니티 개발 알려주는 재생목록 노루발 668   2020-11-12 2020-11-12 05:59
 
170 팁 love2d에서 안드로이드 터치 제스처 인식하기 노루발 645   2020-11-12 2020-11-12 00:38
 
169 팁 특정좌표를 기준으로 zoom in/zoom out하기 노루발 721   2020-11-11 2020-11-11 01:22
 
168 팁 Lua 클래스 만들고 활용하기 노루발 655   2020-11-06 2020-11-06 22:43
 
167 팁 Lua 테이블 안에 함수 저장하기 노루발 652   2020-11-06 2020-11-06 21:07
 
166 팁 Lua 인수로 nil값이 들어왔을 경우 처리하기 노루발 698   2020-11-06 2020-11-06 20:29