Text Attack From Outer Space

published : 2021-03-18 changed: 2021-03-18

category: global --> Games

tags: Game Java


Somehow you have a little too much time during the pandemic. And since I type a lot, I wanted to write a game with which you can improve a little ... whereby the focus was less on typing and more on trying out.

Idea

The idea was to write a very simple game in Java that lets random words fall on you (similar to space invaders). Every word has to be typed in good time before the word "hits" below emoji people:smirk

Then there should be something like different levels so that the level of difficulty adapts.

Then a little effects, sounds etc. and that's it emoji people:smirk well, almost.

implementation

The whole thing was amazingly easy. It was more complicated to dig difficult words from somewhere on the Internet. And look for sound effects on [soundbible] (https://soundbible.com).

The program actually consists of 2 classes:

  • the MainFrame class is also executable. Start the game window and add the 2nd class
  • the 2nd class is GamePanel - that draws the play area.

Then there are a few objects that should be displayed. These implement an interface called Obj:

public interface Obj {

     void draw(Graphics2D g);

     //returns true, if it needs to be deleted
     boolean animate();

     int getX();

     int getY();
}

A timer then runs in the GamePanel, which callsrepaint ()every 15ms. In the paint method, all objects are iterated anddraw is called for all of them. Then a "drumrum" is also output, such as the current score, how many ships there are still to be destroyed, etc.

the animations

In the mainframe there is a function that - also controlled by a SwingTimer - the animate() Method for all Obj in the game panel. If this returns true, the corresponding object is removed from the game panel because the animation has ended and the object is no longer needed.

Every object makes its animations "by itself", i.e. normally an obj does not know any other. Only the "shot", i.e. the projectile that is shot at the bad ships, knows its target object and "animates itself towards the target", so to speak: smirk: That is also the reason why the objects have getters for X and Y. an example of the animation is Star - these are the asterisks that are in the background buzz around (of which 150 pieces are created at the start):


public class Star implements Obj {
    private int x = 0;
    private int y = 0;
    private int sz = 1;
    private int vy = 0;


    public Star(int x, int y, int r, int v) {
        this.x = x;
        this.y = y;
        this.vy = v;
        this.sz = r;
    }

    @Override
    public void draw(Graphics2D g) {
        if (sz > 1) {
            g.setColor(Color.gray);
            g.fillOval(x - sz, y - sz, sz * 2, sz * 2);

            g.setColor(Color.WHITE);
            g.fillOval(x - (sz / 2), y - (sz / 2), sz, sz);
        } else {
            g.setColor(Color.WHITE);
            g.fillOval(x - sz, y - sz, 2 * sz, 2 * sz);
        }
    }

    @Override
    public boolean animate() {
        y = y + vy;
        if (y > 1090) {
            x = (int) (1920 * Math.random());
            y = 0;
        }
        return false;
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }
}

Well, and actually that's it with the entire animation. Then a KeyListener is connected to MainFrame, which goes through the currently visible ships and "shoots" accordingly.

The game can be found on [Github] (https://github.com/sboesebeck/text_attack_from_outer_space), where you can also download current releases. you can start the game using your local java installation with java -jar TextAttack.jar - but be aware that you need to have a recent JDK version installed (>= JDK15)

The whole project took me about 6 hours in total. Starting with the concept and including the implementation. So please be gentle, if you find anything buggy or suboptimal... Thanks.

created Stephan Bösebeck (stephan)