wood burning stoves 2.0*
The moose likes Game Development and the fly likes Need help with Java Snake game. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Need help with Java Snake game." Watch "Need help with Java Snake game." New topic
Author

Need help with Java Snake game.

Sun Ang
Greenhorn

Joined: Mar 31, 2012
Posts: 1
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
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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(..)
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
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.
Shalin Shah
Greenhorn

Joined: Apr 15, 2012
Posts: 5
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:
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

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

Joined: Apr 15, 2012
Posts: 5
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

Joined: Mar 28, 2012
Posts: 859

Shalin,

Did you read what I posted.

g is NULL.

You cannot invoke a method on a null object!

WP
Shalin Shah
Greenhorn

Joined: Apr 15, 2012
Posts: 5
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

Joined: Apr 15, 2012
Posts: 5
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

Joined: Apr 15, 2012
Posts: 5
i am now not getting any run time error but frame is blank new code is:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Java Snake game.