My code detects Key Presses in Java but does detect when the key is lifted in StdDraw?

48 Views Asked by At

So my project requires me to only use StdIn, StdOut and StdDraw. I have got code to detect when certain keys are pressed but it doesn't stop moving until the boundary is hit. I need it to only move 1 space but it moves across the entire the board and I cant figure out why???

I'm not allowed to use JFrame, ActionListener or anything besides StdIn, StdOut and StdDraw.

if (GUIenabled)
                    {
                        if (StdDraw.isKeyPressed(81)) //Check if Q key is pressed
                        {
                            StdOut.println("Termination: User terminated game!");
                            EndGame(moveCounter, m, moveCounterScore);
                        }
                        
                        if (StdDraw.isKeyPressed(87)) //Check if W key is pressed
                        {
                            if (rowSelect == 0)
                            {
                                rowSelect = 0;
                            }
                            else 
                            {
                                rowSelect--;
                                StdDraw.setPenColor(StdDraw.ORANGE);
                                StdDraw.filledSquare(columnSelect + 0.5, m - rowSelect + 1.5, 0.2);
                            }
                        }
                      
                        if (StdDraw.isKeyPressed(83)) //Check if S key is pressed
                        {
                            if (rowSelect == m)
                            {
                                rowSelect = m;
                            }
                            else 
                            {
                                rowSelect++;
                                StdDraw.setPenColor(StdDraw.ORANGE);
                                StdDraw.filledSquare(columnSelect + 0.5, m - rowSelect + 1.5, 0.2);
                            }
                        }
              
                        if (StdDraw.isKeyPressed(65)) //Check if A key is pressed
                        {
                            if (columnSelect == 0)
                            {
                                columnSelect = 0;
                            }
                            else 
                            {
                                columnSelect--;
                                StdDraw.setPenColor(StdDraw.ORANGE);
                                StdDraw.filledSquare(columnSelect + 0.5, m - rowSelect + 1.5, 0.2);
                            }
                        }
              
                        if (StdDraw.isKeyPressed(68)) //Check if D key is pressed
                        {
                            if (columnSelect == m)
                            {
                                columnSelect = m;
                            }
                            else 
                            {
                                columnSelect++;
                                StdDraw.setPenColor(StdDraw.ORANGE);
                                StdDraw.filledSquare(columnSelect + 0.5, m - rowSelect + 1.5, 0.2);
                            }
                        }
}

If there is anyone that can shed light on this it would be greatly appreciated.

0

There are 0 best solutions below