How do i make letters randomly spawn from the top of the screen and drop down to the bottom? They should be random letters and when typed should be removed....Help?
public void act()
{
long currentTime = System.currentTimeMillis();
elapsedTime += currentTime - lastTime;
lastTime = currentTime;
if (elapsedTime / 100 > 25)
{
GreenfootImage img = new GreenfootImage(100, 50);
img.setColor(Color.BLACK); //Change this to the colour of text that you want
float fontSize = 30.0f; //Change this to the font size that you want
Font font = img.getFont().deriveFont(fontSize); //This adjusts the default font to the correct size.
img.setFont(font);
final int letter = Greenfoot.getRandomNumber(26);
String[] alpha = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
"P","Q","R","S","T","U","V","W","X","Y","Z"};
str = alpha[Greenfoot.getRandomNumber(alpha.length-1)];
}
setLocation(getX(), getY()+1);private static final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private String letter; // then in the constructor, select a letter with letter = ""+letters.charAt(Greenfoot.getRandomNumber(26)); // followed by drawing the letter on the bomb int fontsize = 36; // fontsize of letter (adjust at will) Color white = Color.white, trans = new Color(0, 0, 0, 0); GreenfootImage image = new GreenfootImage(letter, fontsize, white, trans); // creates letter image int xOff = 25; // horizontal letter placement on bomb (adjust at will) int yOff = 15; // vertical letter placement on bomb (adjust at will) getImage().drawImage(image, xOff, 15); // puts letter on bomb
public void act()
{
setLocation(getX(), getY()+1); // fall
if (!Greenfoot.isKeyDown(letter)) // check correct letter pressed
{
// create explosion
// possibly increment a counter of exploded bombs
getWorld().removeObject(this);
}
if (getWorld() != null && getY() == getWorld().getHeight()-1) // check world edge
{
// possibly increment a counter of non-exploded bombs
getWorld().removeObject(this);
}
}getWorld().addObject(new Explosion(), getX(), getY());