• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with Java Snake game.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ladies and gentleman
This being my first post on these forums I'm looking forward to becoming involved in this community.

That being said, I have an issue that I cannot solve on my own.
The following is a part of a university project to program a simple game, I have completed all necessary parts of the program but it refuses to work.


The 2 main issues I have with this are 1) Nothing is painted on the JPanel, it just shows the background color and 2) I've added in a line to test the functionality of the keyboard listener by letting the "move()" method print out the coordinates of the snake head every timer tick, however the listener does not seem to be registering my keys as the coordinates move in a single direction regardless of if or which keys I press.

If anyone can give me any tips it would be greatly appreciated.

Thanks, Andy
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your errors could depend on how you're showing this - applet/stand-alone etc

if an applet:
1) focus is a huge problem to control - try KeyBindings instead
2) for images, search for getCodeBase()

also, in Swing components (JPanel), override paintComponent(..) instead of paint(..)
 
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since their is no info going to the snake positions array, I'd put some diagnostics to make sure that you are even reaching the KeyListener.

Applets can be tricky for getting the focus. Until the panel has the focus (e.g., clicking on it) it won't register keystrokes, unless you have programmatically given the JPanel the focus.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

Even I am having a problem with my snake game code and I am preety much not sure whether it is correct or not plzz help.

here's the code:
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you guys in the same class ?

@Sun: Don't use uppercase package names
without Snake.java and Apple.java, hard to help you out

@Shalin: It doesn't compile for me. add(g)
Also what do you think happens with code like:

If g is null, BOOM! which it is.

To both, what EXACTLY is going wrong.
How far do you get?

WP
 
Shalin Shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no we are'nt in same class and acctually i want to make the background black but directly doing setBackground method does'nt do so why I dont know means in a blank frame when i set color of the background nothing happens so i made graphic object and when i set color then every object made using it will have that color so I did what actually happens can you tell me and what should I do to make this thing run..??
 
William P O'Sullivan
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shalin,

Did you read what I posted.

g is NULL.

You cannot invoke a method on a null object!

WP
 
Shalin Shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok then what should i do for black background as i said on blank frame setting the background doest'nt works wy my lat question i till unanswered...ye saw your post

means acctually i want to know how will i add the graphic object to frame as add methood takes component type of object.
 
Shalin Shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after that i changed i++ in snake move methood to i-- which was my mistake but still i m getting problem as shown


Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 1
at java.util.LinkedList.checkElementIndex(LinkedList.java:553)
at java.util.LinkedList.get(LinkedList.java:474)
at snake.Snake.move(Snake.java:61)
at snake.Snake.make_snake(Snake.java:52)
at snake.Snake.<init>(Snake.java:34)
at snake.Snake.main(Snake.java:23)
 
Shalin Shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am now not getting any run time error but frame is blank new code is:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic