• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with Java Snake game.

 
Sun Ang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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(..)
 
Phil Freihofner
Ranch Hand
Posts: 120
3
  • Mark post as helpful
  • send pies
  • 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.
 
Shalin Shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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:
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
i am now not getting any run time error but frame is blank new code is:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic