Reaction game
Hi there. im trying to develop a reaction testing game... And ive come across a few difficultuies.....
Ive been working with code from the Java Boutique Game Tutorial (full code avalible here http://javaboutique.internet.com/tutorials/Java_Game_Programming/DasErsteSpielEng.html )
and basically im trying to turn this code into a reaction game.
I have modified parts of the code so that only one ball is being displayed and is movable in any direction.. also it bounces off the walls.
The problem i am having is that i have implemented a timer and it seems to be causeing the program not to work properly... it compiles but the timer only seems to work for the first go and then the ball dissapears!
I've got some edited code below from the main segment of the code, perhaps im putting the timer in the wrong place?
If you need any more of the code please ask..
Many thanks
privatelong start;
privatelong total = 0;
player =new Player ();
redball =new Ball (10, 190, 150, 1, -1, 4, Color.red, outnoise, player);
publicboolean mouseDown (Event e,int x,int y)
{
if (!isStoped)
{
if (redball.userHit (x, y))
{
long stop = System.currentTimeMillis();
hitnoise.play();
redball.ballWasHit ();
String s1 ="time = " + (stop - start) +" ms";
System.out.println(s1);
showStatus(s1);
start=0;
}
else
{
shotnoise.play();
}
}
elseif (isStoped && e.clickCount == 2)
{
isStoped =false;
init ();
}
returntrue;
}
publicvoid run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
if (start==0){
start = System.currentTimeMillis();
if (player.getLives() >= 0 && !isStoped)
{
redball.move();
}
repaint();
try
{
Thread.sleep (speed);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
}
publicvoid paint (Graphics g)
{
if (player.getLives() >= 0)
{
g.setColor (Color.yellow);
g.drawString ("Time: " + player.getScore(), 10, 40);
g.drawString ("Attempt: " + player.getLives(), 300, 40);
redball.DrawBall(g);
if (isStoped)
{
g.setColor (Color.yellow);
g.drawString ("Doubleclick on Applet to start Game!", 40, 200);
}
}
Many thanks
Hi there...
Im not actually using a timer() funtion..... I tried to use the system clock.... I have posted the (almost) full code im really not sure why its not working!! The ball shows up but is not animated! (the timer and everything else works)
The program works fine if i take the timer bit out of it.
Many thanks
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.net.*;
public class Main extends Applet implements Runnable
{
private int speed;
boolean isStoped = true;
private Player player;
private Ball redball;
Thread th;
Font f = new Font ("Serif", Font.BOLD, 20);
private Image dbImage;
private Graphics dbg;
Random rnd2 = new Random ();
private int newx;
private int nexy;
private long start;
private long total = 0;
public void init ()
{
if (getParameter ("speed") != null)
{
speed = Integer.parseInt(getParameter("speed"));
}
else speed = 15;
player = new Player ();
redball = new Ball (10, 190, 150, 1, -1, 4, Color.red, outnoise, player);
}
public void start ()
{
th = new Thread (this);
th.start ();
}
public void stop ()
{
th.stop();
}
public boolean mouseDown (Event e, int x, int y)
{
if (!isStoped)
{
if (redball.userHit (x, y))
{
long stop = System.currentTimeMillis();
redball.ballWasHit ();
String s1 = "time = " + (stop - start) + " ms";
System.out.println(s1);
showStatus(s1);
start=0;
}
else
{
}
}
else if (isStoped && e.clickCount == 2)
{
isStoped = false;
init ();
}
return true;
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
if (start==0) {
start = System.currentTimeMillis();
// ive tried taken the following if statement away as well!
if (player.getLives() >= 0 && !isStoped)
{
redball.move();
}
repaint();
try
{
Thread.sleep (speed);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
}
public void paint (Graphics g)
{
if (player.getLives() >= 0)
{
g.drawString ("Time: " + player.getScore(), 10, 40);
g.drawString ("Attempt: " + player.getLives(), 300, 40);
redball.DrawBall(g);
if (isStoped)
{
g.setColor (Color.yellow);
g.drawString ("Doubleclick on Applet to start Game!", 40, 200);
}
}
else if (player.getLives() < 0)
{
isStoped = true;
}
}
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
}
//
/-/
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.*;
public class Ball
{
private int pos_x;
private int pos_y;
private int x_speed;
private int y_speed;
private int radius;
private int first_x;
private int first_y;
private int maxspeed;
Player player;
Random rnd = new Random ();
public Ball (int radius, int x, int y, int vx, int vy, int ms, Color color, AudioClip out, Player player)
{
this.radius = radius;
pos_x = x;
pos_y = y;
first_x = x;
first_y = y;
x_speed = vx;
y_speed = vy;
maxspeed = ms;
this.color = color;
this.out = out;
this.player = player;
}
public void move ()
{
pos_x += x_speed;
pos_y += y_speed;
isOut();
}
public void ballWasHit ()
{
}
public boolean userHit (int maus_x, int maus_y)
{
}
private boolean isOut ()
{
}
public void DrawBall (Graphics g)
{
g.setColor (color);
g.fillOval (pos_x - radius, pos_y - radius, 2 * radius, 2 * radius);
}
}