wood burning stoves 2.0*
The moose likes Game Development and the fly likes Snake Remake - Clear screen issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Snake Remake - Clear screen issue" Watch "Snake Remake - Clear screen issue" New topic
Author

Snake Remake - Clear screen issue

Brandon Young
Greenhorn

Joined: Mar 12, 2013
Posts: 1

Title may not be the right terms but its my issue. I started learning Java around a month ago and like most people took on a project that was beyond my knowledge. So I decided to make something easier, a remake of the classic Snake game. I had help from some tutorials but once those were done, I decided I wanted to add more to the game. My first idea was to add in a score per food eaten, which works. Then I wanted a game over screen to show up when you hit the edge or yourself. On this screen it would say "Game Over ! Score: xxxx" and "Made by: Me". I got the game over screen to display when I die, however my game just restarts after 5 seconds (plan to change this later) and this is where the issue shows up. The score displayed on screen resets to 0, but the game over screen stays shown. It doesn't clear (not sure on the right term to use here) and show the game again so you can play another round.

This is the code from the main class and is where the issue is. I appreciate any help you all can provide me as its probably something silly that I've overlooked.

Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
You have these two lines in your paint() method:

dbg.setColor(Color.BLACK);
dbg.fillRect(0, 0, getWidth(), getHeight());

It seems to me these are used to clear the screen. If so, they should occur every time, not just when gameOver is true. Thus, I would place them at the start of the paintComponent(). They should be called every time, to give you a blank slate, whether or not the game is over or not.

However, I'm not able to follow your code. So my suggestions might not work without a bit more restructuring of your program than you are hoping for!

Usually, when using a component like JPanel or JComponent (both of which handle double buffering automatically) for display, one does the rendering by overwriting paintComponent() not paint().

Then, paintComponent() is called via a repaint() command in the "game loop", usually last. It looks to me like tick() is your game loop, so I would set things up so that the last line of tick() was a repaint(). The game loop should probably be set to run something like 20 to 60 times per second. This is often handled by having a "sleep" command in the game loop (sleeping a variable amount, depending on how much time was spent on the given iteration). An alternative is to have a java.util.timer run the "tick" code repeatedly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Snake Remake - Clear screen issue