I'm trying to make an SAT word game. When you destroy an asteroid, a letter appears. Once the whole word has been spelled out the same way, the definition comes up. Any suggestions? I'm very new to programming.
OK, you have an idea for something, and you explained it fairly well (but only in general terms). So, my answer is going to be in general terms.
What you are attempting will take a small 'database' of info (words and their definitions) which will take a two-dimensional array of type String (String ). String will contain the words and String will contain the definitions. You will need an integer pointer to indicate the location in the String array of the current word. Finally, you will need at least two classes; one for the individual letters of the word, and one for the definition. The letters can either line themselves up as the word is being made or a seperate class can be made to show the word as it is being built. If the former, the letters will need to know what position they appear in the word. Beyond that, you will need to be more specific as to your needs.
theDoctor, as Danpost suggests you need to give a more specific question(s). Usually I like to say "post your code!" - even if it has chunks missing (where you don't know what to put) or doesn't work, it is much easier for people to suggest what to do if you already have some code written.
If you're at the level where you don't even know how to begin writing code yet, go through the tutorials (on the Documentation page) first.
Ok, I know how to write code in general, but this is a bit more advanced for me. I'm not quite sure where I'm supposed to start, or what I should begin with. I think I have to make a letter class and then once an asteroid is destroyed, I need to change the image of the asteroid to that of the letter. I'm not quite sure how I should go about this, or where I should put my code.
I think that first off you should just get some code down for the game in general (the whole destroying-the-asteroids part), and then once that's done you can start worrying about the turning-to-letters part.
For the turning-to-letters part, here's a little something:
Also, its probably not any help because it doesn't have definitions with it, but here's some .txt files of words that I found lying around in my hard drive:
click me
Alternatively: there's this one
The first link is a .zip with the files in (has words arranged by length, and also by letter, whereas the second link is just all of them
Where should I add this code? I've got the game made. I just need to implement this code to change the image of the asteroids once they've been destroyed.
String letter = "A";
int fontSize = 50;
Color textColor = Color.RED;
setImage(new GreenfootImage(letter, fontSize, textColor, new Color(0, 0, 0, 0)));
It depends. You really need to put it wherever you actually destroy the asteroid; however the code as written will only work if you do that from within the Asteroid class (and even then I'm not sure it's the best solution - it's probably best to remove the asteroid, then add in the letter, rather than replace the asteroid image).
As I said before, it's much easier to provide help if you post your code (in particular the part which destroys asteroids - and don't forget to mention which class it's in!).
Ok, this is the code that I have so far for the Asteroid class:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
* A rock in space.
*
* @author Poul Henriksen
*/
public class Asteroid extends SmoothMover
{
/** Size of this asteroid */
private int size;
/** When the stability reaches 0 the asteroid will explode */
private int stability;
public Asteroid()
{
this(50);
}
public Asteroid(int size)
{
super(new Vector(Greenfoot.getRandomNumber(360), 2));
setSize(size);
}
public Asteroid(int size, Vector speed)
{
super(speed);
setSize(size);
}
public void act()
{
move();
setRotation(getRotation() - 1);
}
/**
* Set the size of this asteroid. Note that stability is directly
* related to size. Smaller asteroids are less stable.
*/
public void setSize(int size)
{
stability = size;
this.size = size;
GreenfootImage image = getImage();
image.scale(size, size);
}
/**
* Return the current stability of this asteroid. (If it goes down to
* zero, it breaks up.)
*/
public int getStability()
{
return stability;
}
/**
* Hit this asteroid dealing the given amount of damage.
*/
public void hit(int damage)
{
stability = stability - damage;
if(stability <= 0)
breakUp ();
}
/**
* Break up this asteroid. If we are still big enough, this will create two
* smaller asteroids. If we are small already, just disappear.
*/
private void breakUp()
{
Greenfoot.playSound("Explosion.wav");
if(size <= 16)
{
getWorld().removeObject(this);
}
else
{
int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
double l = getMovement().getLength();
Vector speed1 = new Vector(r + 60, l * 1.2);
Vector speed2 = new Vector(r - 60, l * 1.2);
Asteroid a1 = new Asteroid(size/2, speed1);
Asteroid a2 = new Asteroid(size/2, speed2);
getWorld().addObject(a1, getX(), getY());
getWorld().addObject(a2, getX(), getY());
a1.move();
a2.move();
getWorld().removeObject(this);
}
}
}
Now I just need to implement code to remove the asteroid and then add in a letter, rather than replace asteroid image with a letter. I agree with davmac about this. Any suggestions as to how I'd go about doing this?
Do you want to keep the existing behavior where large asteroids break up into smaller ones, and only the smaller ones get replaced by letters, or do you want large asteroids to turn into a letter immediately?
Ok. It looks like when your asteroids are hit, the hit() method is called, and that then calls breakup() if the asteroid has been hit enough times.
So, you could put the code in either of those methods - depending on whether you want it to happen with the first hit (hit() method) or after several hits (breakup() method). You should remove all the existing code in the chosen method and replace it with new code.
You need to add the letter, and remove the asteroid:
getWorld().addObject(new Letter(), getX(), getY());
getWorld().removeObject(this);
You'll need to write a Letter class for this to work of course!
Alright thanks a bunch. One more quick question, do I need to write an array for the letters and assign them to different asteroids? I need a new letter to appear every time an asteroid is destroyed.
I'm not sure what you mean - do you want the order of letters appearing to be dependent on the order that the asteroids are destroyed, or do you want the letters to appear in the same order regardless of the order in which the asteroids are destroyed?
I just need the letters to appear randomly, but they can't all appear as the same letter. I'm trying to spell "iconoclast", so I need one of the asteroids when destroyed to appear as an "I", one as a "c", one as an "o", etc. Does that clear it up?