954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Some guidance please?

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

jamesyrawr
Newbie Poster
14 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

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!

Dean_Grobler
Posting Whiz in Training
222 posts since Aug 2010
Reputation Points: 72
Solved Threads: 9
 

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!!

Dean_Grobler
Posting Whiz in Training
222 posts since Aug 2010
Reputation Points: 72
Solved Threads: 9
 

hello jamesyrawr
i think this code will help you....

sharathg.satya
Posting Whiz in Training
264 posts since Oct 2010
Reputation Points: 0
Solved Threads: 5
 

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)
{

}
}

sharathg.satya
Posting Whiz in Training
264 posts since Oct 2010
Reputation Points: 0
Solved Threads: 5
 

excuse if any errors .......

sharathg.satya
Posting Whiz in Training
264 posts since Oct 2010
Reputation Points: 0
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: