Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an object to keep track of the state instead of multiple variables #8

Open
Vages opened this issue Nov 29, 2021 · 1 comment
Open

Comments

@Vages
Copy link
Owner

Vages commented Nov 29, 2021

Because svelte can react to assignments within an object as well as to an object itself, perhaps it's best to replace this monstrosity with an object:

  // Letting the state variables go uninitialized is safe,
  // because they are initialized by resetGame before they are ever read
  let apple;
  let gameState;
  let headDirection;
  let headDirectionQueue;
  let score;
  let snake;
  let willGrow;

  function resetGame() {
    const initialSnake = [
      { x: 4, y: 4 },
      { x: 4, y: 3 },
      { x: 4, y: 2 },
    ];
    apple = drawRandomOpenSpace(BOARD_DIMENSIONS, initialSnake);
    gameState = GAME_STATES.START_SCREEN;
    headDirection = DIRECTION.SOUTH;
    headDirectionQueue = [];
    score = 0;
    snake = initialSnake;
    willGrow = false;
  }
@Vages
Copy link
Owner Author

Vages commented Sep 3, 2022

I'm not sure if this will make anything better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant