Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By xpoferens1

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

  1. #1
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

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

    Hi,

    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.

    Java Code:
    //Not done yet but this is the start to my game of Snake. Basically the class Game generates a Grid of ASCII characters 
    
    import java.util.Scanner; 											
    
    public class Game {
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		final int WIDTH = 79; 
    		int topY;
    		int topX;
    		int height;
    		boolean running = true;
    		topX = -10;		
    		topY = 10;		
    		height = 20;		
    	
    	
    		
    		Dot player = new Dot(0, 0, -1, 0); //(start x coordinate, start y coordinate, start x direction, start y direction)
    		
    		System.out.println();
    		while (running){
    			System.out.println("Which way do you want to go?");
    			int k = keyboard.nextInt();
    			if (k == 0){
    				running = false;
    			}
    			else if(k == 8){
    				player.moveDotUp();
    			}
    			else if(k == 2){
    				player.moveDotDown();
    			}
    			else if (k == 4){
    				player.moveDotLeft();
    			}
    			else if(k == 6){
    				player.moveDotRight();
    			}
    			for (int y = topY; y > topY - height; y--) {	
    				for (int x = topX; x < topX + WIDTH; x++) {
    					
    					if ((x == player.getDotX()) && (y == player.getDotY())) {
    						System.out.print("*");
    					} else {
    						System.out.print(" ");
    					}
    				}
    				System.out.println();
    			}	
    		}
    	}
    }
    and a second class called Dot

    Java Code:
    /*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;
    	
    	//constructor taking 2 points and two coordinates of the direction vector as arguments
    	public Dot(int startX, int startY, int startDirX, int startDirY){
    		x = startX;
    		y = startY;
    		dirX = startDirX;
    		dirY = startDirY;
    	}	
    	
    	//movedots
    	public void moveDotLeft(){
    		x = x - 1;
    		dirX = -1;
    		dirY = 0;
    	}
    	
    	public void moveDotRight(){
    		x = x + 1;
    		dirX = 1;
    		dirY = 0;
    	}
    	
    	public void moveDotUp(){
    		y = y + 1;
    		dirY = 1;
    		dirX = 0;
    	}
    	
    	public void moveDotDown(){
    		y = y - 1;
    		dirY = -1;
    		dirX = 0;
    	}
    	
    	//get dot's coordinates
    	public int getDotX(){
    		return x;
    	}
    	public int getDotY(){
    		return y;
    	}
    	
    	
    	
    }
    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

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default 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?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default 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.

  6. #6
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

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

    gimbal2 likes this.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

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

    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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default 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.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  2. Replies: 0
    Last Post: 03-10-2013, 11:00 PM
  3. simple exercise for class, help please?
    By Boomer1 in forum Other IDEs
    Replies: 10
    Last Post: 11-02-2009, 06:12 AM
  4. JGrasp simple exercise?
    By Boomer1 in forum Other IDEs
    Replies: 2
    Last Post: 11-02-2009, 04:23 AM
  5. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 04:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •