Skip to content

Release 0.2.5: Movement bug fix

Pre-release
Pre-release

Choose a tag to compare

@Cristi-Croitoru Cristi-Croitoru released this 30 Jun 15:26
· 180 commits to main since this release
fefa31e

Bug fixes

  • Fixed a bug where the player could rotate 180 degrees in the same column by swiping:
    • For example, previously, if the snake was heading right and the player swiped down then immediately up, the snake would erroneously change direction to down and then up.

Technicalities

if (this.canChangeDirection && this.direction !== direction && this.direction !== Math.abs(180 - direction)) {

  • The issue stemmed from incorrect direction comparison logic:
    • Originally, I compared the current direction (this.direction, which was DOWN in the above example) against the opposite of the current direction (UP): DOWN !== UP. This comparison always returned true, allowing illegal movements.
    • Instead of the correct approach of checking the requested direction (UP) with the opposite of the current direction (opposite of DOWN is UP): UP !== UP is false).
  • Furthermore, Math.abs(180 - direction) didn't consistently return the expected result.
    • For instance, if the current direction (this.direction) is 90 (right), the expected result is 270 (left) (the opposite of the current direction, right), but the calculation would return 90 instead.
    • Now, the result is determined via a separate function to guarantee a correct result:
    function oppositeDegrees(degrees) {
      if (degrees === 0) {
        return 180;
      } else if (degrees === 90) {
        return 270;
      } else if (degrees === 180) {
        return 0;
      } else if (degrees === 270) {
        return 90;
      }
    }

What's Changed

  • Bug can change direction 180 degree immediately after directin change by @IceTheCoder in #17

Full Changelog: 0.2.4...0.2.5

30 June 2024