• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

KeyClicked from KeyListener is not responding

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am making a game which develops on top of the original Snake game. The problem is that when the user snake hits a purple apple or the wall or it's own snake body, the game ends. To restart the game, I have initialized the user to press SPACE in order to restart and it is not responding on press. This was working before I made changes to the drawWalls() method but I have tried backtracking to the source of the bug but I have had no luck so far. I know I should be using KeyBindings instead of KeyListener but I need to specifically use KeyListener for this project. Any help will be appreciated. Also if an MCVE needs to be created I would like advice on how to condense this to do so.

I have also provided images of both in game and when the user needs to press SPACE to restart to where it will navigate to. I have taken out some code to attempt to simplify the post.

Code:

gameProcess.JPG
[Thumbnail for gameProcess.JPG]
 
Rancher
Posts: 3288
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use a KeyListener. It can only respond to events when a component has focus.

Instead you can use Key Bindings. You can configure key bindings to work even when a component doesn't have focus.
 
Rancher
Posts: 4904
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Small note: Panel is the name of a java class:  java.awt.Panel
Using the same name as one in the Java SE packages can be confusing.
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have said that I need to use KeyListener. Can you provide help based on those guidelines?
 
Norm Radder
Rancher
Posts: 4904
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code with the main method so the code can be executed for testing?
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My program features other files apart from Panel.java hence you may view the entire project on my github. The link is here: https://github.com/zkhan122/snakeboost
 
Norm Radder
Rancher
Posts: 4904
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you debugging the code?  Add some print statements to the code so you know when the Space bar is pressed.
What is the code supposed to do when Space is pressed?
 
Sheriff
Posts: 27527
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zayaan Khan wrote:I have said that I need to use KeyListener.



But you didn't say why.
 
Norm Radder
Rancher
Posts: 4904
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code has lots of bugs that need to be found and fixed.

Note: The existing listener is called when buttons are pressed.
 
Marshal
Posts: 77162
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid it also has design problems, like using only one action listener, and extending display classes.
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When space is pressed, it show the user the screen with the slider so they can choose the number of bad apples and play again. I will attempt to further debug this
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By buttons, are you referring to JButton or keypad key buttons? And yes there are several bugs which need to be fixed, I will try to debug them.
 
Norm Radder
Rancher
Posts: 4904
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by defining the detailed steps that the restart method needs to do.
Currently it looks like it is trying to startup a new instance of the Panel class.  However it does not stop the timer or remove the listeners that are associated with the old instance of the Panel class.  Leaving them will probably be a problem.
Can the code be tailored to use the existing Panel class vs creating a new instance?   What variables need to be reset?
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I believe that the bug lies in the restart() method as my KeyClicked class which extends the KeyAdapter interface is actually working which I have tested through print statements and I am getting feedback in the console (I have attached an image to show this for further understanding) but I am still not too sure if it is a problem of refocusing on components and how I can fix this as although the actual KeyClicked interface is working, on click it does not restart the game. Further help will be appreciated.



misc.JPG
[Thumbnail for misc.JPG]
 
Norm Radder
Rancher
Posts: 4904
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

KeyClicked class which extends the KeyAdapter interface is actually working


Yes that is not the problem.  

on click it does not restart the game.


The restart method needs to be rewritten.
Did you read my last post?

Start by defining the detailed steps that the restart method needs to do.
Currently it looks like it is trying to startup a new instance of the Panel class.  However it does not stop the timer or remove the listeners that are associated with the old instance of the Panel class.  Leaving them will probably be a problem.
Can the code be tailored to use the existing Panel class vs creating a new instance?   What variables need to be reset?



Look at continuing to use the current instance of the Panel class.  Define a method to reset whatever variables need to changed to restart the game at the  beginning.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic