• 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

Snake Remake - Clear screen issue

 
Greenhorn
Posts: 1
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
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
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.
 
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic