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
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
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!
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:
Code :
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:
Code :
| o |
| o |
| o |
| o |
|o |
Game Over! - You cleared 3 obsticle(s)