Need help with java racing game

Started by
2 comments, last by Gilbrilthor 11 years, 10 months ago
Okay so I'm trying to make a simple racing game. Ive gone over it for a couple of days and I can't see whats wrong with it. Everything seems to be ok but when i run it, it just shows a blank screen instead of the race track. Im starting to think its my computer. Could someone run it on there computer and see if it works for them or if they see whats wrong with the code let me know

thanks


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class S2P2 extends JFrame
{
final int WIDTH = 900; int HEIGHT = 650;
double p1Speed =.5,p2Speed=.5;
//create rectangles that will represent the left,right,top,bottom and centre

Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*9,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*9,WIDTH,HEIGHT/9);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)
((HEIGHT/9)*2.5),(int)((WIDTH/9)*5),(HEIGHT/9)*4);
//these obstacles will obstruct the path and make navigating harder
Rectangle obstacle = new
Rectangle(WIDTH/2,(int)((HEIGHT/9)*7),WIDTH/10,HEIGHT/9);

Rectangle obstacle2 = new
Rectangle(WIDTH/3,(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);

Rectangle obstacle3 = new
Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);

Rectangle obstacle4 = new
Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);

Rectangle obstacle5 = new
Rectangle(WIDTH/2,(int)((HEIGHT/9)*1.5),WIDTH/30,HEIGHT/4);

//the following rectangle is the finish line for both players
Rectangle finish = new
Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);




Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2, WIDTH/30,WIDTH/30);


Rectangle p2 = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+
(HEIGHT/10),WIDTH/30,WIDTH/30);
//the constructor
public S2P2()
{
//the following code creates the JFrame
super("Radical Racing");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

//start the inner class (which works on it's own because it is a thread)
Move1 m1 = new Move1();
Move2 m2 = new Move2();
m1.start();
m2.start();

}
//this will draw the cars and the racetrack
public void paint(Graphics g)
{
super.paint(g);

//draw the backround for the racetrack
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,WIDTH,HEIGHT);

//when we draw, the border will be green
g.setColor(Color.GREEN);

//the following retangle is the start line for the outer player
Rectangle lineO=new
Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
//the following rectangle is the start line for the inner player
Rectangle line1=new
Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),
(HEIGHT/2)+(HEIGHT/10),
(int)((WIDTH/9)*1.5)/2,HEIGHT/140);

//now, using the rectangles, draw it
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);

//set the starting line with colour to white
g.setColor(Color.WHITE);

//now draw the starting line
g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
g.fillRect(line1.x,line1.y,line1.width,line1.height);

//set the colour of the finish line to yellow
g.setColor(Color.YELLOW);
//now draw the finish line
g.fillRect(finish.x,finish.y,finish.width,finish.height);

//set the colour to blue for p1
g.setColor(Color.BLUE);
//now draw the actual player
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);

//set the color to red for p2
g.setColor(Color.RED);
//now draw the actual player
g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);


//set the color to blue for p1
g.setColor(Color.BLUE);
//now draw the actual player
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);

//set the color to red for p2
g.setColor(Color.RED);
//now draw the actual player
g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);

}
private class Move1 extends Thread
{
public void run()
//This should all be in an infinite loop so that the process repeats.
{
while(true)
{
//now put in the try block. This will let
//the program exit if there is an error
try
{
//first refresh the screen
repaint();
//increase speed a bit
if(p1Speed<=5)
p1Speed+=.2;
p1.y-=p1Speed;

//this delays the refresh rate
Thread.sleep(75);
}
catch(Exception e)
{
//if there is an exception (an error), exit the loop
break;

}

}
}
}

private class Move2 extends Thread
{
public void run()
{
//this should all be in an infinite loop so the process repeats
while(true)
{
//now put the code in a "try" block.
//this will let the program exit if there is an error
try
{
//first refresh the screen
repaint();
//increase the speed a bit
if(p2Speed<=5)
p2Speed+=.2;
p2.y-=p2Speed;

//this delays the refresh rate
Thread.sleep(75);
}
catch(Exception e)
{
//if there is an exception (an error), exitthe loop
break;
}
}
}

}
public static void main(String[]args)
{
new S2P2();
}

}
Advertisement
Hello,

Well I am new to Java (just took a class) but it I implemented a version of double buffering that cleared up your course visibility problem.
You are right in clearing the screen with a rectangle (from my beginner level of knowledge), but it causes a flicker that is for some reason became a greater problem by your sleeping thread. You might want to consider a game loop instead of using the thread.sleep() method. Instead decide on how fast you want your fps to be, then use that to calculate how long you want your thread to sleep.
e.g.
time started
draw stuff (takes time)
calculate stuff (some time)
thread.sleep(time per frame - (time finish - time start) )

Something like that, I'm still learning myself so I hope this helps.

So, what I did was created an image or screen to be painted after everything was painted on this image, that way you don't see the gray rectangle being constantly painted.

Here is the updated code.


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class S2P2 extends JFrame
{
final int WIDTH = 900; int HEIGHT = 650;
double p1Speed =.5,p2Speed=.5;
//create rectangles that will represent the left,right,top,bottom and centre
//Graphics object
private Graphics g;
//Image to paint on
private Image img;
//Size of the screen for the image
private Dimension dim;
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*9,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*9,WIDTH,HEIGHT/9);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)
((HEIGHT/9)*2.5),(int)((WIDTH/9)*5),(HEIGHT/9)*4);
//these obstacles will obstruct the path and make navigating harder
Rectangle obstacle = new
Rectangle(WIDTH/2,(int)((HEIGHT/9)*7),WIDTH/10,HEIGHT/9);
Rectangle obstacle2 = new
Rectangle(WIDTH/3,(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle3 = new
Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle4 = new
Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);
Rectangle obstacle5 = new
Rectangle(WIDTH/2,(int)((HEIGHT/9)*1.5),WIDTH/30,HEIGHT/4);
//the following rectangle is the finish line for both players
Rectangle finish = new
Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);

Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2, WIDTH/30,WIDTH/30);

Rectangle p2 = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+
(HEIGHT/10),WIDTH/30,WIDTH/30);
//the constructor
public S2P2()
{
//the following code creates the JFrame
super("Radical Racing");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//get the size of the screen
dim = getSize();
//create image based on screen size
img = createImage(dim.width, dim.height);
//assign image to Graphics
g = img.getGraphics();
//start the inner class (which works on it's own because it is a thread)
Move1 m1 = new Move1();
Move2 m2 = new Move2();
m1.start();
m2.start();
}
//this will draw the cars and the racetrack
//Changed this to gfx, as g will be used for painting the screen
public void paint(Graphics gfx)
{
super.paintComponents(g);
//draw the backround for the racetrack
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,WIDTH,HEIGHT);
//when we draw, the border will be green
g.setColor(Color.GREEN);
//the following retangle is the start line for the outer player
Rectangle lineO=new
Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
//the following rectangle is the start line for the inner player
Rectangle line1=new
Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),
(HEIGHT/2)+(HEIGHT/10),
(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
//now, using the rectangles, draw it
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
//set the starting line with colour to white
g.setColor(Color.WHITE);
//now draw the starting line
g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
g.fillRect(line1.x,line1.y,line1.width,line1.height);
//set the colour of the finish line to yellow
g.setColor(Color.YELLOW);
//now draw the finish line
g.fillRect(finish.x,finish.y,finish.width,finish.height);
//set the colour to blue for p1
g.setColor(Color.BLUE);
//now draw the actual player
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//set the color to red for p2
g.setColor(Color.RED);
//now draw the actual player
g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);

//set the color to blue for p1
g.setColor(Color.BLUE);
//now draw the actual player
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//set the color to red for p2
g.setColor(Color.RED);
//now draw the actual player
g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
//paint the next image
gfx.drawImage(img, 0, 0, this);
}
public void update(Graphics gfx){
paint(gfx); // call paint without clearing the screen
}
private class Move1 extends Thread
{
public void run()
//This should all be in an infinite loop so that the process repeats.
{
while(true)
{
//now put in the try block. This will let
//the program exit if there is an error
try
{
//first refresh the screen
repaint();
//increase speed a bit
if(p1Speed<=5)
p1Speed+=.2;
p1.y-=p1Speed;
//this delays the refresh rate
Thread.sleep(75);
}
catch(Exception e)
{
//if there is an exception (an error), exit the loop
break;
}
}
}
}
private class Move2 extends Thread
{
public void run()
{
//this should all be in an infinite loop so the process repeats
while(true)
{
//now put the code in a "try" block.
//this will let the program exit if there is an error
try
{
//first refresh the screen
repaint();
//increase the speed a bit
if(p2Speed<=5)
p2Speed+=.2;
p2.y-=p2Speed;
//this delays the refresh rate
//Thread.sleep(75);
}
catch(Exception e)
{
//if there is an exception (an error), exitthe loop
break;
}
}
}
}
public static void main(String[]args)
{
new S2P2();
}
}
well its a lot better then it was, i can actually see the track now. only thing is that it only shows player1 (the blue square)
You might try looking into a buffer strategy. It takes care of double buffering in Java, so you don't have to worry about that stuff right now. You would have to rework your paint/update code, but I think it would be a lot better in the end.

You game's looking like it's getting off to a great start! One thing that might help is if you try an object oriented approach to it, instead of trying to hard code everything in. If you would like some more help, I wouldn't mind pointing you in the right direction.

After alittle testing, I found your problem with your second player. Your delay 75 millisecs was commented out in move2. So, by the first frame, you were already like negative 20 million on the y coordinate. Hope that helps!

This topic is closed to new replies.

Advertisement