Ok for my uni project im wondering if its possible to make a simple game where by the user must click a button ten times to win.
the button has to move after every click to a random point on the screen.
Does anyone know if there are any tutorials for this kind of thing available?
or would it be easy to script?
Thanks
James

Hey man, whatsup!
Check out the little piece of code here:

int btnClicked = 0;

public btnActionPerformed(actionEvent e) {
    if(btnClicked==10){System.out.println("You win!");
    else{btnClicked++;}
}

Then with the button moving and all that, there's a layoutManager (not sure which one) that you can set the x and y coordinates of the button in the JFrame. So just do some research in that. And then in your code, change the x and y coordinates for the button in the actionlistener after the btnClicked++ part..

It really won't be that hard I think..
Hope this helped!

What you can also do, use math.random() to generate some radom number, parse it to an int, then use those ints as the value for the x and y coordinates. Then the button will just pop up where ever the heck it feels like! It will be crazy like a cartoon or something!!

public class Nnnn extends Applet implements Runnable , MouseListener

{

    int x,y,str,r=0,s=0;



    Thread t;
    public void init()
    {
      setBackground(Color.yellow);
       if(t==null)
        {
           t=new Thread(this);
          t.start();
       }

      addMouseListener(this);
          }
    public void run()
    {
    while(true)
        {
    try
    {
        t.sleep(1000);
    }
    catch (InterruptedException e)
    {
    }


    repaint();
    }
    }
   public  void paint(Graphics g)
    {
        Font f=new Font("arial",Font.BOLD,24);
        g.setFont(f);


        Random r=new Random();
            x=r.nextInt()%450;
            if(x<0)
                x=x*(-1);
            y=r.nextInt()%450;
             if(y<0)
                 y=y*(-1);

           if(x<30)
               x=35;
           if(y<30)
               y=35;



        g.setColor(Color.red);
          g.fillOval(x,y,25,25);
          showStatus(str+"      click the ball to get a point");
         g.drawString("POINTS :"+str,10,50);
    }

public void mouseClicked(MouseEvent e)
{
       r=e.getX();
          s=e.getY();
     if((r<=x+25&&r>=x-25)||(s<=y+25&&s>=y-25) )

              {     

                str+=1;

    } 

}

public void mousePressed(MouseEvent e)
{

}
public void mouseReleased(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}
public void mouseExited(MouseEvent e)
{

}
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.