• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • paul wheaton
  • Paul Clapham
Saloon Keepers:
  • Piet Souris
Bartenders:

Java Swing Snake Game

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My goal is to create a simple snake game. I was expecting to get the snake to move around on the screen, but I am getting the error:



I'm not sure what this means but it has to do with private class Key implements @KeyListener at the bottom of the Screen class. I've seen that the problem could be that you have to put it in a different method, but mine already is. So here's my code:

Snake class:



Bodypart class:



Frame class:



Thanks in advance for your help!
 
Master Rancher
Posts: 3327
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The code should be:



Read the section from the Swing tutorial on How to Write a KeyListener for more information.

Also, note you really should be using Key Bindings instead of a KeyListener. Read the Swing tutorial on How to Use Key Bindings.

For more information you can also check out Motion Using The Keyboard for example of both approaches.
 
Mona Smith
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob Camick, it was a dumb mistake on my part!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Marshal
Posts: 81759
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

In future, please use the code button; I shal; use it for you, so you can see how much better the post looks I think your code is incomplete at the end.
Do you have a question, or are you trying to explain an improvement?
 
Campbell Ritchie
Marshal
Posts: 81759
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid your code is very difficult to understand with multiple languages; please supply a version all in English.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic