0

I am programming the game breakout in Java using an applet. But when the applet opens I want the text: "Press up to start" instead of the automatic "applet started".

I know I can put the message in my code with showStatus("Press up to start"). But I also want the text to dissapear immediately so other texts can be places there (for example when game over).

However, I have no clue where to put this in my code. This is my code.

public class Breakout extends Applet implements Runnable {
    // all the properties are stated here but this is not important for my question and saves a lot of space

    public void init() {
        hitsound = getAudioClip(getDocumentBase(), sound);
        hitsound.play();
        buffer = createImage(FieldWidth, FieldHeight);
        gContext = buffer.getGraphics();
        gContext.setColor(FieldColor);
        brick = new Field();
        paddle = new Paddle();
        ball = new Ball(brick, hitsound);
        gContext.fillRect(0, 0, FieldWidth, FieldHeight);
        paddle.draw(gContext);
        ball.draw(gContext);
        brick.draw(gContext);
    }

    public void start() {
        if(animate == null) {
            animate = new Thread(this);
            animate.start();
        }
    }
    public void restart() { }   

    public void stop() {
        if(animate != null) {
            animate = null;
        }
    }

    public void paint(Graphics g) {
        try {
            g.drawImage(buffer, 0, 0, this);
        }
        catch(Exception e) {}

// In the bottom of the screen the number of lives left is displayed
        g.drawString("Number of lives left: " + (ExtraLives- numberlost), 0, FieldHeight-22);
    }

    public void run() {
        while(true) {
            paddle.clear(gContext);
            ball.clear(gContext);

            if(leftArrow) {
                paddle.move(-PaddleSpeed);
                if(ballready && paddle.moved)
                    ball.move(-PaddleSpeed,0,gContext);
            }if(rightArrow) {
                paddle.move(PaddleSpeed);
                if(ballready && paddle.moved)
                    ball.move(PaddleSpeed,0,gContext);
            }if(!ballready) {
                ball.moves(gContext,paddle,this);
            }if(brick.bricksLeft()==0)
                win();

            paddle.draw(gContext);
            ball.draw(gContext);

            try {
                Thread.sleep(Slow);
            }
            catch (InterruptedException e) {}

            repaint();
        } 
    }

// This paints the graphics on the screen
    public void update(Graphics g) {
        paint(g);
    }

// When a key is pressed an action happens. 
// If the up arrow (key 1004) is pressed, the game starts
// if the left arrow (key 1006) or right arrow (key 1007) is pressed the paddle moves (true)
    public boolean keyDown(Event e, int key) {
        showStatus("Let's destroy those bricks haha");
        if(key==1004 && ballready) {
            ballready = false;
            ball.xchange = BallSpeedX;
            ball.ychange = BallSpeedY;
            showStatus("");
        }
        if(key==1006)
            leftArrow = true;
        if(key==1007)
            rightArrow = true;
        return true;
    }

// When a key is released and action happens
// if the left arrow (key 1006) or right arrow (key 1007) is pressed the paddle stops moving (false)
    public boolean keyUp(Event e, int key) {
        if(key==1006)
            leftArrow = false;
        if(key==1007)
            rightArrow = false;
        return true;
    }

// This method is called when you win 
    public void win() {
        showStatus("Victory!");
        gContext.setColor(FieldColor);
        gContext.fillRect(0,0,FieldWidth,FieldHeight);
        repaint();
    }

// This method is called when you miss the ball with the paddle, and the game starts over
    public void lose() {
// You lost a life, but number of lives lost is smaller than number of extra lives, so game continues
        if(numberlost < ExtraLives) {
            numberlost++;
            showStatus("You just lost a life, try again. Press up to start!");
            gContext.setColor(FieldColor);
            paddle.clear(gContext);
            ball.clear(gContext);
            paddle.go(FieldWidth/2-(PaddleWidth/2), PaddlePosition);
            ball.go(FieldWidth/2-BallSize, PaddlePosition-PaddleHeight);
            ballready=true;
            PaddleWidth -= -20;
        }
        else {
// You are game over and the game is restarted
            numberlost=0;
            showStatus("You loose. Press up to start over");
            gContext.setColor(FieldColor);
            gContext.fillRect(0,0,FieldWidth, FieldHeight);
            paddle.go(FieldWidth/2-(PaddleWidth/2), PaddlePosition);
            ball.go(FieldWidth/2-BallSize, PaddlePosition-PaddleHeight);
            brick.restart();
            brick.draw(gContext);
            ballready = true;
        }
    }

    public boolean getLeftArrow() {
        return leftArrow;
    }

    public boolean getRightArrow() {
        return rightArrow;
    }
}

Does somebody know the answer?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Maris
  • 91
  • 1
  • 2
  • 10
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Mar 02 '16 at 02:39
  • Yes indeed, we have to use an applet, and we cannot use Swing. I cannot help that this is my assignment, right? But can you answer my question? – Maris Mar 02 '16 at 08:07
  • *"I cannot help that this is my assignment, right?"* So, you've referred your teacher to that link, right? – Andrew Thompson Mar 02 '16 at 08:55

0 Answers0