官术网_书友最值得收藏!

Hunting bugs

As proud and satisfied as you may be with your first project, nothing is ever perfect. If you've spent some time actually playing the game, you may have noticed an odd event when quickly mashing the buttons, looking something like this:

The image represents the difference between two sequential updates. It seems that earlier it was facing the right direction and then it's facing left and missing its tail. What happened? Try to figure it out on your own before continuing, as it perfectly illustrates the experience of fixing game flaws.

Playing around with it some more reveals certain details that narrow down our problem. Let's break down what happens when a player starts mashing keys quickly:

  • The snake is facing right.
  • Any arrow key other than the left or right is pressed.
  • The direction of the snake gets set to something else, let's say up.
  • The right key is pressed before the game has a chance to update.
  • Since the snake's direction is no longer set to right or left, if statement in the input handler is satisfied and sets the direction to left.
  • The game updates the snake and moves it left by one space. The head collides with its tail and it gets cut off.

Yes, it seems that our direction checking is flawed and causes this bug. Once again, spend some time trying to think of a way to fix this before moving on.

主站蜘蛛池模板: 棋牌| 项城市| 凌源市| 霍林郭勒市| 尚义县| 行唐县| 凤冈县| 澄城县| 依安县| 平远县| 桂东县| 岢岚县| 巴中市| 吴忠市| 兰州市| 遂川县| 阜城县| 从化市| 雅江县| 奉化市| 固阳县| 墨竹工卡县| 道真| 琼海市| 奉贤区| 二手房| 鄢陵县| 临朐县| 上栗县| 武隆县| 延寿县| 深水埗区| 绥棱县| 仁化县| 资兴市| 阿克苏市| 阆中市| 宣威市| 扎兰屯市| 天全县| 荔波县|