patorjk / JavaScript-Snake

The game of Snake written in JavaScript.
MIT License
536 stars 631 forks source link

Gamepad #88

Open Rb64 opened 2 years ago

Rb64 commented 2 years ago

Added Gamepad support.

patorjk commented 2 years ago

This is a cool idea. I hadn't worked with the gamepad API before so I had to read up on it some. I made some changes to make the game work in Chrome. It's still not ready yet but it's closer. It still needs arrow button support and possibly some tightening up of the controls.

The main problem you were running into was you were putting the gamepad code in the Snake when it needed to be in the Board (which has an instance of the snake). It's been forever since I've looked at this code so I was a bit confused too. I may need to go in and clean things up a bit as some of the variables were a bit confusing (ie: isFirstMove - I think that relates to queued moves, which have long been removed so that may not be needed anymore).

Rb64 commented 2 years ago

Thanks! I took a quick glance at what you changed, and it seems to make a lot more sense now. I will hopefully find some time to finish this soon. Yes, it’s my first time working with the gamepad API to, but it seems quite interesting it’s just a pain that there is no capability for handling gamepad button press or joystick events. The whole project is great work by the way!