Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem while programming a simple game of Car moving on a road

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem while programming a simple game of Car moving on a road

    Been lurking here for a while.

    I've just been assigned to Java A, and this is actually one of our first "major" exercises... Problem is that I'm dead-stuck and I'm wondering if you could help me out?

    I'm supposed to make a game that consists of a car moving on a road (the road consisting of whatever signs I want) so it looks like this ->

    | o |
    | |

    "o" being the player. You're supposed to earn points for every "wall segment "(| |) you pass (easy part) and the walls are supposed to move in a random manner, keyboard control is optional (and WAY over my head).

    The idea I had was to save the walls as separate strings consisting of a 5 blank spaces, a small L, and 5 more blanks -> "_____l_____", and making a big for loop with thread sleep to keep it pumping out one "wall" at a time, so print segment1, print player, print segment2, add +1 to score, repeat. The idea is to have a 50% chance to move (Math.random()?) with every loop, and then have the walls move either left or right one step. The idea I had was to accomplish this by removing one blank from one side and adding it to the other, to make the walls closer or further away from the player, and do the opposite on the other segment, so the walls move in unison without forcing the player to move, and then have some simple "if" thing that checks if the player is next to a "l", and then ending the program by printing out the score so far.

    Gah wall of text.

    I have no idea how to do this though - any tips or help on the way is mucho appreciated. . I've been looking around but don't know any way of changing the variables without making some huuuge chunky code.

    Thanks in advance


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Yo! Stuck making a simple game

    I'm still a bit confused sorry. Do you think you could post like a 10 line perfect match of how the output would look, this might help me get an idea of what we are looking at. Then I can suggest some ideas as to how to do it. I'm sure it's not as hard as it looks

    Chris

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Yo! Stuck making a simple game

    EDIT: It kills my spaces. There's supposed to be five blankspace between the " and the l, and then five more before the second ".

    Re-reading what I said it does come of as being awfully confusing. Here's my idea in pseudo code:

    For (however long I want the game to continue)

    print " l ", print player ("o"), print " l "
    If Math.random(); > 0.5
    Check if math.random is over 0.75, if so then remove one blank space from before the l, and add on to the other side, if not then do the opposite (remove one). Thereby moving one of the walls closer to the player, randomized which one for each loop.
    Check if there are no blank spaces to the right of the l in the first string, or left of the l in the second string (therby making it touch the "o"), if so then quit the loop.
    Thread.sleep(some number)
    print int that is = how many times the loop has been run


    ---------------------------
    When run it should look something like this:

    l o l
    l o l
    l o l
    l o l
    l o l
    l o l
    l o l
    l o l
    lo l

    You cleared 9 obstacles!
    Last edited by rojroj; April 1st, 2009 at 03:23 PM.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Yo! Stuck making a simple game

    Hey rojroj,

    I don't think this is anywhere near as complicated as you require but here is an example I wrote:

    import java.math.*;
    import java.text.*;
     
    public class Rojroj {
     
        public static String space1 = " ";
        public static String space2 = " ";
        public static String noSpace = "";
     
        public static String leftWall = "|";
        public static String rightWall = "|";
     
        public static String player = "o";
     
        public static int finalScore = 0;
        public static double randomDouble;
     
     
        public static void randomise(){
     
            DecimalFormat df = new DecimalFormat("#.##");
            double rd = Math.random();
            randomDouble = Double.parseDouble(df.format(rd));
            //System.out.println(randomDouble);
     
            if(randomDouble >= 0.75){
                move();
            }
            else{
                noMove();
            }
     
        }
     
        public static void move(){
            finalScore++;
            System.out.println(leftWall + space1 + player + space2 + rightWall);
            randomise();
        }
     
        public static void noMove(){
     
            System.out.println(leftWall + player + space2 + rightWall);
            System.out.println(" ");
            System.out.println("Game Over! - You cleared " + finalScore + " obsticle(s)");
        }
     
     
        public static void main(String[] args){
     
            System.out.println(leftWall + space1 + player + space2 + rightWall);
            randomise();
     
        }
     
    }
    Example output:

    | o |
    | o |
    | o |
    | o |
    |o |
     
    Game Over! - You cleared 3 obsticle(s)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. [SOLVED] How to start writing java mobile application?
    By Kok in forum Java ME (Mobile Edition)
    Replies: 15
    Last Post: July 30th, 2009, 01:52 AM
  2. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM
  3. stuck on this program can any one help me
    By clive in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2009, 05:54 PM
  4. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 02:33 PM