• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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!
 
Rancher
Posts: 3287
30
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic