Help coderanch get a
new server
by contributing to the fundraiser

Mac Subedi

Greenhorn
+ Follow
since Nov 11, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Mac Subedi

Hello Guys i made a Wack a mole program where moles appears in random position. But I don't know how i can write the code to record the score when user hit the mole. Can i get help

Thank You
Here is my code:


import java.awt.Color;
import java.awt.event.MouseEvent;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;
import acm.graphics.*;


public class Walkamole extends GraphicsProgram
{


public final static double MOLE_SIZE=40;

public void run()
{

setSize(400,430);
addMouseListeners();
GLabel();
createmole();


}

public void createmole()
{
RandomGenerator rgen= RandomGenerator.getInstance();
double StartX=50;
double StartY=50;
for( int i=1; i<=NUM_ROUNDS; i++)
{

GOval mole= new GOval(StartX,StartY,MOLE_SIZE,MOLE_SIZE);
mole= new GOval(45*rgen.nextInt(0,8), 45*rgen.nextInt(0,8),40,40);
mole.setFillColor(Color.YELLOW);
mole.setFilled(true);
pause(800);
add(mole);

}

}

public void mouseClicked(MouseEvent event)
{
GObject itemClicked = getElementAt(event.getX(), event.getY());
if(itemClicked !=null)
{
remove(itemClicked);
}


}



public void GLabel()
{
GLabel label= new GLabel("SCORE:", 0,30);
label.setColor(Color.RED);
add(label);


}
}
















12 years ago
would you give me some hints in code. ThankYOU
12 years ago
Here i Created Ovals and loop it until 3 . My question is how can i

1) Draw a Oval at a random location and
2) If there are more than 5 Oval present how can i remove a random Oval, So far i have done this:









I hope this question make sense whoever is trying to help me. Thank YOU













12 years ago

Chris Waguespack wrote:Hello Mac, and welcome to JavaRanch. I'm having trouble understanding what problems you're having; could you give a description of either the error or incorrect result you're getting? Also, you can use the code tags to make your code easier to read.

- Chris



Hello Chris thank You for looking at my question. Basically what i am trying to do is ,to create GOval at random position and remove GOval if there are more than 5 Govals present at the same time. I tried to use getAtElement to find the random position but it does not work.

Thank YOU
12 years ago
Hello guys i am trying to creak a simple wack a mole program where mole pops up after certain seconds and when user click on it will increment score by 1. I try to create moles and remove at random position but it is not working. Basically i am trying to loop mole until 40 but don't want to appear more than 5 loop at a time.

here is my code please help me :


12 years ago