This version of the Pong Game builds on the previous, but the paddles are controlled by a logitech controller's two joysticks. This version of the game is for two players, each of which control one of the joysticks. The game keeps track of the number of times that the ball has hit each player's paddle and displays this score on the screen.
Currently the program does not end nicely; there is a Null Pointer Exception which terminates the run when the ball runs off the edge of the screen. I am not sure on the source of the exception.
The video below shows the game being played.
Much of the code is the same as the previous post including the classes Ball, Paddle, and ButtonListener. The class Listener is similar, but now the mouseMoved method doesn't do anything. GamePanel now includes two more labels which display the player's scores (scoreLabel1 and scoreLabel2). It is also passed a JoyStickController object which gives axes to the method GetYAxesValues and the variables Y1Value and Y2Value which contain the most recent values of the two joysticks on the Logitech Controller (more on that below).
The picture shows that the left and right paddles' ycor variables are set to increase with the value of the left and right joysticks on the Logitech controller (respectively). So if the left joystick is centered (Y1Value is zero), then leftPaddle.ycor stays constant. If the left joystick is pressed up, then Y1Value is negative and leftPaddle.ycor decreases causing the leftPaddle to move up on the screen (recall that the y-coordinate increases as you move down the screen). If the left joystick is pressed down, then Y1Value increases and the leftPaddle moves down the screen. The multiplier *20 just makes the paddles move at a reasonable rate.
The class JoystickController includes two methods, SetController and GetYAxesValues. The first method is called once when the main method of the PongGame class is run, and it scrolls through all the attached devices looking for one of type "Stick" . It then sets a local variable (logitech) of type Controller to be that device.
The GetYAxesValues method calls a Controller method called poll, then sets an array called components to include the names of each of the parts of the Logitech controller. It scrolls through the components array until it finds the array element called "Z Rotation" then stores the value of that component using getPollData in Y1Value. Similarly, it stores the value of "Y Axis" in Y2Value. Each value is subjected to a deadband, so that if the value is between -0.1 and 0.1 it is set to 0.
In the PongGame class, most everything is the same, but the main method now just creates an instance of PongGame, then called two methods (setupPlayers and initializeGame). This is a much more elegant way compared to the mouse version, which does all the work in the main method.
Full game code can be found here: https://github.com/gcronin/PongGame/blob/JoystickControl/PongGame/src/ponggame/PongGame.java .