Hey can u help me with this code..?? Its a simple game of moving darts I made on applet..
I want to restart the game from starting when i click replay but this ain't happening, please check the code:
import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class game extends Applet implements Runnable, ActionListener
{
Thread t=null;
boolean flag;
int BoardY=0,chngdir=1,hitstop=0,arrX=0,chances=2,arr=3,pts=0,arrspeed;
String pname;
Button Continue,Shoot,Replay,Slow,Medium,Fast;
TextField Name;
Image img;
public void init()
{
BoardY=0;
chances=3;
arr=3;
pts=0;
arrspeed=1;
Label namep = new Label("Name: ", Label.RIGHT);
add(namep);
Name = new TextField(12);
add(Name);
Name.addActionListener(this);
Label Speed = new Label("Arrow Speed");
add(Speed);
Slow = new Button("Slow");
add(Slow);
Slow.addActionListener(this);
Medium = new Button("Medium");
add(Medium);
Medium.addActionListener(this);
Fast = new Button("Fast");
add(Fast);
Fast.addActionListener(this);
Shoot = new Button("Shoot");
add(Shoot);
Shoot.addActionListener(this);
Shoot.setEnabled(false);
Continue = new Button("Continue");
add(Continue);
Continue.addActionListener(this);
Replay = new Button("Replay");
add(Replay);
Replay.addActionListener(this);
Replay.setEnabled(false);
//img = getImage(getDocumentBase(), getParameter("img"));
setBackground(Color.orange);
t = new Thread(this);
flag = false;
t.start();
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Shoot")) //IF SHOOT IS CLICKED
{
Continue.setEnabled(true);
Shoot.setEnabled(false);
hitstop=1;
}
if(str.equals("Replay")) //IF REPLAY IS CLICKED
{
BoardY=0;
chances=3;
arr=3;
pts=0;
arrspeed=1;
repaint();
Shoot.setEnabled(true);
Slow.setEnabled(true);
Medium.setEnabled(true);
Fast.setEnabled(true);
Name.setEnabled(true);
Replay.setEnabled(false);
Continue.setEnabled(true);
init();
}
if(str.equals("Continue")) //IF CONTINUE IS CLICKED
{
repaint();
Continue.setEnabled(false);
chances=arr-1;
arr--;
if(arr>0)
Shoot.setEnabled(true);
else
{Replay.setEnabled(true);
stop();
}
}
if(str.equals("Slow")) // SLOW IS SELECTED
{
Continue.setEnabled(true);
Name.setEnabled(true);
Shoot.setEnabled(true);
Slow.setEnabled(false);
arrspeed= 10;
}
if(str.equals("Medium")) //IF MEDIUM IS SELECTED
{
Name.setEnabled(true);
Shoot.setEnabled(true);
Continue.setEnabled(true);
Medium.setEnabled(false);
arrspeed= 15;
}
if(str.equals("Fast")) //IF FAST IS SELECTED
{
Name.setEnabled(true);
Shoot.setEnabled(true);
Continue.setEnabled(true);
Fast.setEnabled(false);
arrspeed= 20;
}
repaint();
}
public void run()
{
/*BoardY=0;
chances=3;
arr=3;
pts=0;
arrspeed=1;*/
for(chances=3,arr=3;chances>0;)
{
BoardY=0;hitstop=0;chngdir=1;arrX=0;chances=0;
//Loop for every chance
for(;chngdir!=0;)
{
try
{
repaint();
Thread.sleep(100);
//For controlling the movement of the board up and down.
if(chngdir == 1)
{
BoardY+=8;
if(BoardY>=380)
chngdir = -1;
}
if(chngdir==-1)
{
BoardY-=8;
if(BoardY<=20)
chngdir = 1;
}
//For movement of the arrow
if (hitstop==1)
{
if(arrX<395) //if arrow hasnt reached the position of the board increase the x coordinate of arrow(arrX).
arrX+=arrspeed;
else // else increase the points accordingly.
{
chngdir=0;
if(BoardY>=150&&BoardY<=230)
{
if(BoardY<=170||BoardY>=210)
pts+=20;
else if(BoardY<=185||BoardY>=195)
pts+=35;
else pts+=50;
}
Thread.sleep(3000);
}
}
if(flag)
break;
}
catch(InterruptedException e){}
}
}
}
public void paint(Graphics g)
{
g.drawString(("Arrows Left: "+arr),10,430);
g.drawString(("Score: " + pts),200,430);
int x[] = {30+arrX,35+arrX,30+arrX};
int y[] = {245,250,255};
g.fillPolygon(x,y,3);
g.fillRect(15+arrX,249,20,2);
g.setColor(Color.yellow); //Board coordinates
g.fillRect(425,20+BoardY,25,20);
g.setColor(Color.green);
g.fillRect(425,40+BoardY,25,15);
g.setColor(Color.red);
g.fillRect(425,55+BoardY,25,10);
g.setColor(Color.green);
g.fillRect(425,65+BoardY,25,15);
g.setColor(Color.yellow);
g.fillRect(425,80+BoardY,25,20);
g.setColor(Color.black);
g.drawLine(425,20+BoardY,425,100+BoardY); //Border
g.drawString("20",450,35+BoardY); //Pts-score
g.drawString("35",450,53+BoardY);
g.drawString("50",450,65+BoardY);
g.drawString("35",450,78+BoardY);
g.drawString("20",450,95+BoardY);
pname = Name.getText();
if(arr==0&&pts!=0)
g.drawString("Congrats " +pname+" !!! You've scored "+pts+" Points",150,250);
if(arr==0&&pts==0)
g.drawString("Better luck next time " +pname+" !!! You've scored 0 Points",100,250);
}
public void stop()
{
t=null;
flag = true;
}
}