inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part2: 자료구조와 알고리즘

맵 만들기

Mac환경등에서 콘솔이 제대로 출력되지 않는 분들은 이 java 코드를 활용해보세요.

해결된 질문

505

피로한 문어

작성한 질문수 10

0

제가 직접 java GUI 로 포팅한 코드입니다. 
콘솔의 환경설정 문제 때문에 콘솔이 이상하게 나온다면 이번 강의와 이어지는 알고리즘 강의를 들을 때 이 코드를 활용해보세요.
다음 강의인 유니티 강의부터는 필요없어지겠지만 이번 강의는 들을 수 있을 것입니다. 혹시라도 저와 같은 문제를 겪게될 사람들을 위해 공유합니다.
이클립스 IDE나 JDK를 깔면 어떤 환경에서도 실행이 될 것입니다.
(java문법은 C#과 크게 다르지 않습니다. 틀만 그대로 활용하시면 됩니다. )

//<Program.java>

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Program extends JFrame {
	private static final long serialVersionUID = 1L;
	private final int BORDER_SIZE = 26;
	private static Board board;
	private static Player player;
	public Program() {
		initUI();
	}

	public static void main(String[] args) throws InterruptedException {
		Program ex = new Program();
		ex.setVisible(true);

		final int FPS = 30; // 30프레임
		final int WAIT_TICK = 1000 / FPS;
		long lastTick = System.currentTimeMillis();
		long currentTick = 0;
		while (true) {
			//// # 프레임 관리
			currentTick = System.currentTimeMillis();
			if (currentTick - lastTick < WAIT_TICK) {
				Thread.sleep(WAIT_TICK - (currentTick - lastTick));
			}
			int deltaTick = (int) (System.currentTimeMillis() - lastTick);
			lastTick = System.currentTimeMillis();
			//// # 프레임 관리

			// 입력

			// 로직

			// 렌더링
			board.repaint();
		}
	}

	private void initUI() {
		JPanel content = new JPanel(new GridBagLayout());
		content.setBackground(Color.BLACK);
		content.setBorder(new EmptyBorder(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE));
		board = new Board();
		board.setBackground(Color.BLACK);

		player = new Player();
		board.Initialize(25);
		player.Initialize(1, 1, board._size-2, board._size-2, board);

		content.add(board);
		add(content, BorderLayout.CENTER);
		setResizable(false);
		pack();
		setTitle("미로 길찾기");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
//<Board.java>

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Board extends JPanel {
	private static final long serialVersionUID = 1L;
	private final int CELL_SIZE = 20; // 픽셀 크기

	public TileType[][] _tile; // 배열
	public int _size;

	public enum TileType {
		Empty, Wall,
	}

	public void Initialize(int size) {
		_size = size;
		setPreferredSize(new Dimension(_size * CELL_SIZE, _size * CELL_SIZE));
		_tile = new TileType[_size][_size];

		// 원하는 보드를 생성한다.
		for (int y = 0; y < _size; y++) {
			for (int x = 0; x < _size; x++) {
				if (x == 0 || x == _size - 1 || y == 0 || y == _size - 1) // 가장자리
					_tile[y][x] = TileType.Wall;
				else
					_tile[y][x] = TileType.Empty;
			}
		}
	}

	@Override
	public void paintComponent(Graphics g) {
		for (int y = 0; y < _size; y++) {
			for (int x = 0; x < _size; x++) {
				g.setColor(GetTileColor(_tile[y][x]));
				g.fillOval(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
			}
		}
	}

	Color GetTileColor(TileType type) {
		switch (type) {
		case Empty:
			return Color.GREEN;
		case Wall:
			return Color.RED;
		default:
			return Color.GREEN;
		}
	}
}
//<Player.java>

import java.util.ArrayList;
import java.util.List;

class Pos {
	public int Y;
	public int X;
	public Pos(int y, int x) {
		Y = y;
		X = x;
	}
}
public class Player {
	public int PosY;
	public int PosX;
	public int getPosY() {
		return PosY;
	}
	private void setPosY(int posY) {
		PosY = posY;
	}
	public int getPosX() {
		return PosX;
	}
	private void setPosX(int posX) {
		PosX = posX;
	}

	Board _board;

	enum Dir {
		Up, Left, Down, Right,
	}
	int _dir = Dir.Up.ordinal();
	List<Pos> _points = new ArrayList<Pos>();

	public void Initialize(int posY, int posX, int destY, int destX, Board board) {
		PosY = posY;
		PosX = posX;

		_board = board;
	}
}

C#

답변 1

0

Rookiss

ㅎㅎㅎ 감사합니다!

게임개발에서 주로 어느부분에 알고리즘들이 쓰이는지 궁금합니다

0

170

2

글꼴 바꿔도 자간이 좁아 찌그러져보이시는 분들

0

86

1

NullReferenceException 예외) 같은 실수하시는분 계실까봐 남겨요

0

66

1

parent를 Pos타입으로 만든 이유

0

74

1

콘솔창에 격자가 안나옴 미로 생성 X

0

133

1

격자 생성 안됨 무한루프

0

113

1

BFS 질문

0

143

2

격자 무한 출력

0

166

2

A* 의 PriorityQueue 관련 질문입니다

0

155

2

vscode에서 원그리기

0

179

1

환결설정 강의 원 그리기

0

122

1

15-17분

0

86

1

3:16초에 근데 이렇게 해가지고 부분에 "{}"를 만들어서 자식 node들을 생성하던데 왜 중괄호로 감싸게 만드는 건가요?

0

141

2

동적 배열 관련 질문입니다!

0

209

1

Big-o 표기법에서 시간 복잡도

0

167

1

7:40에서 언급하신 색상이 날아가는 문제 이해를 못하겠습니다

0

151

1

트리구현연습 강의 질문있어요

0

142

1

창은 뜨는데 맵이 나타나지 않아요.

0

174

1

Ctrl F5 하면 나오는 창은 어디서 설정할까요??

0

271

1

void CalcPathFromParent(Pos[,] parent)에 대해서

0

202

2

NullReferenceException예외가 발생했을때 어떻게 해야하나요?

0

228

1

[해결] 환경설정 강의에서 원이 이상하게 그려지는 문제

3

308

2

오른손 법칙에서 플레이어 점이 안 움직입니다

0

243

2

맵 만들기 오류

0

178

1