본문 바로가기
[ChatGPT] Sample Code 샘플 코드

[ChatGPT][P5.js][2D RPG게임] P5.js 프로그래밍 언어를 사용하는 간단한 2D RPG 타일맵의 예제 샘플

by byungwoo733 2023. 1. 23.
728x90
반응형
SMALL

P5.js 프로그래밍 언어를 사용하는 간단한 2D RPG 타일맵의 예제 샘플

let tileSheet;
let tileSize = 32;
let map = [  [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];

function preload() {
  tileSheet = loadImage("tiles.png");
}

function setup() {
  createCanvas(512, 512);
}

function draw() {
  for (let y = 0; y < map.length; y++) {
    for (let x = 0; x < map[y].length; x++) {
      let tile = map[y][x];
      let tileX = tile % 8;
      let tileY = floor(tile / 8);
      image(tileSheet, x * tileSize, y * tileSize, tileSize, tileSize, tileX * tileSize, tileY * tileSize, tileSize, tileSize);
    }
  }
}

You'll need to replace "tiles.png" with the path to your own tile sheet image. This example is a basic one, you can customize and expand it to add more functionality such as player movement, collision detection, and more. You can adjust the tileSize variable to change the size of the individual tiles, and the map array to change the layout of the tilemap. You can also use the preload() function to load other assets such as images, sounds, and fonts before the setup() and draw() functions are called. You can also add other functionality such as loading and saving maps, generating maps randomly, and more. Please let me know if there is anything else I could help you with.

[번역]

"tiles.png"를 자신의 타일 시트 이미지 경로로 바꿔야 합니다. 이 예제는 기본 예제이며 사용자 지정하고 확장하여 플레이어 이동, 충돌 감지 등과 같은 더 많은 기능을 추가할 수 있습니다. tileSize 변수를 조정하여 개별 타일의 크기를 변경하고 지도 배열을 조정하여 타일맵의 레이아웃을 변경할 수 있습니다. setup() 및 draw() 함수가 호출되기 전에 preload() 함수를 사용하여 이미지, 사운드 및 글꼴과 같은 다른 자산을 로드할 수도 있습니다. 지도 로드 및 저장, 무작위로 맵 생성 등과 같은 기타 기능을 추가할 수도 있습니다. 다른 도움이 필요하시면 알려주세요.

728x90
LIST

댓글