테트리스)다음 블록(랜덤index)알 수 있게 수정

Blackeichi·2023년 1월 7일
0
post-custom-banner
  const init = () => {
    const playground = document.querySelector(".background");
    setScore(0);
    for (let i = 0; i < rows; i++) {
      createLine(playground);
    }
    generateNewBlock(playground);
  };
  const generateNewBlock = (background: any) => {
    clearInterval(autoDrop);
    autoDrop = setInterval(() => {
      moveBlock("top", 1, background);
    }, speed);
    const randomIndex = Math.floor(Math.random() * blockArray.length);
    movingItem.type = blockArray[randomIndex][0];
    movingItem.top = 0;
    movingItem.left = 3;
    movingItem.direction = 0;
    tempMovingItem = { ...movingItem };
    renderBlocks(background);
  };
  
  -------->
  
  let nexRandomIndex
  const [test, setTest] = useState(0) as any;
  ...........
  const init = () => {
    const playground = document.querySelector(".background");
    setScore(0);
    for (let i = 0; i < rows; i++) {
      createLine(playground);
    }
    const firstRandom = Math.floor(Math.random() * blockArray.length);
    generateNewBlock(playground, firstRandom);
  };
  const generateNewBlock = (background: any, firstRandom?: number) => {
    clearInterval(autoDrop);
    autoDrop = setInterval(() => {
      moveBlock("top", 1, background);
    }, speed);
    const randomIndex = firstRandom ? firstRandom : nexRrandomIndex;
    movingItem.type = blockArray[randomIndex][0];
    movingItem.top = 0;
    movingItem.left = 3;
    movingItem.direction = 0;
    tempMovingItem = { ...movingItem };
    renderBlocks(background);
    nextRandomIndex = Math.floor(Math.random() * blockArray.length);
    setTest(nextRandomIndex);
  };
..........
return
	<NextBlock src={`/img/tetris/${blockArray[test][0]}.PNG`} />
profile
프론트엔드 주니어 개발자 한정우입니다. 😁
post-custom-banner

0개의 댓글