I am busy programming a clone of the popular phone game they had on Nokia cellphones a long time ago called Snake II but since I know very little about programming I will be using ASCII graphics instead of a 2D graphics engine.

My idea for implementation is having a main class called game which should refresh a grid of ascii characters every, say 0.2 seconds. Then I have another class called Dot. Each Dot object has x and y coordinates, and a direction in the x and y planes (dirx = -1 means left, dirx = 1 means right, diry = 1 means up, diry = -1 means down, and obviously the snake cant move the diagonals)

The Game class prints a "*" symbol where the Dot is, and what I'm trying to do is get the screen to refresh (I think I need to use the sleep() function for this to slow the game down to a reasonable pace), and go in the direction it is supposed to go.

(I haven't programmed this in yet but the snake will be an array of Dot, and at each refresh Dot at position 0 will pass it's coordinates and direction to Dot at position 1, Dot1 to Dot2, Dot2 to Dot3, etc.

Here's my code so far:

A first class called Game.

Code:

//Not done yet but this is the start to my game of Snake. Basically the class Game generates a Grid of ASCII characters

/*A Dot object has four fields:
* it's x coordinate, its y coordinate, the direction its going respective to x, and the direction its going respective to y;
* There are four possible direction pairs a Dot can be moving:
* left dirX == -1; dirY == 0
* right == dirX = 1; dirY == 0
* up dirY == 1; dirX == 0
* down dirY == -1; dirX == 0
*
* the program should never change the direction vector to anything that is not one of these ordered pairs.
*/

public class Dot {
//have the snake start in the middle and move to the left by default
public int x = 0;
public int y = 0;
public int dirX = -1;
public int dirY = 0;

My problem now is that I am taking input from the Scanner class. What it does is wait for my input, then it goes on to executing the rest of the code in other words refresh the ASCII grid. This is a problem because I need the snake to keep moving at constant pace while listening to the keyboard.
How can I get my while loop to keep going (i will add a sleep() function later) while listening to the keyboard without stopping?

Note: I might have given way more information than needed but maybe the community will like this game =D

03-04-2014, 11:58 AM

gimbal2

Re: My first java program that isn't just a simple exercise.

Note that trying to do this on the command prompt / shell is likely going to make your life unnecessarily difficult. The command prompt is not designed to do a game like snake.

I would rather start with something simpler like Hangman as your first game, and eventually move along to using a very simple Swing GUI in which you do custom painting, once you get the hang of basic Java programming. Then your Snake game is a whole lot easier to do.

Also a second tip: you have to do this for yourself at this point, not for "a community". I speak out of experience when I say that you will get a whole lot of nothing otherwise. At this point your primary target should be learning how to do games, not to actually make one that you think people will want to play. First learn to walk, then you can attempt to run.

03-04-2014, 12:06 PM

xpoferens1

Re: My first java program that isn't just a simple exercise.

gimbal, I forgot to mention I also want to clear the screen at every refresh.
I don't see the problem with using the command prompt: If i can get a while loop that clears the screen, prints out a grid, and get the snake to move at constant speed wouldn't it work beautifully?

03-04-2014, 12:10 PM

gimbal2

Re: My first java program that isn't just a simple exercise.

I can't imagine it, the command prompt is WAY to slow for that. And I mean wicked slow. On Linux/Mac it will probably work quite okay though.

So you have troubles with the Scanner class, like anyone new to Java that immediately wants to use the Scanner class. I suggest you browse a little through recent threads in the "New to Java" forum with titles that contain "Scanner" in it, something tells me that you may find guidance there.

03-04-2014, 12:26 PM

xpoferens1

Re: My first java program that isn't just a simple exercise.

Well, the problem is that the scanner class waits for you to type input and then press enter. Maybe theres another class that just checks for the latest key pressed and uses that input in the current while loop? But maybe you're right about the cmd thing gimbal, I guess it doesn't just let you press a key and do something right away. You have to press enter and that's too slow for a game like snake. I guess it's back to square 1.

Good. I use BufferedReader myself to read and process data line by line. There is always more than one solution, especially in the wonderful world of Java.

03-05-2014, 03:04 AM

jim829

Re: My first java program that isn't just a simple exercise.

I used to play character based games on the old VT52 and VT100 terminals in the 70's and 80's. Adventure comes to mind. But even then the screen was not refreshed over and over. Cursor addressing was used to populate the screen with specific characters. There was even a game called snake. It may be the same one. The original vi editor also used cursor addressing to emulate page edits.