Skip to main content
replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

I think this question should still be closed and I'm just using this space to better explain the answer found herehere. Sorry, I haven't used Python in years, and only used it a little bit so this answer is not Python oriented at all.

I think this question should still be closed and I'm just using this space to better explain the answer found here. Sorry, I haven't used Python in years, and only used it a little bit so this answer is not Python oriented at all.

I think this question should still be closed and I'm just using this space to better explain the answer found here. Sorry, I haven't used Python in years, and only used it a little bit so this answer is not Python oriented at all.

Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

I think this question should still be closed and I'm just using this space to better explain the answer found here. Sorry, I haven't used Python in years, and only used it a little bit so this answer is not Python oriented at all.

With your paddle class, you would have something like this (which you probably already have):

Paddle {
    private int paddleWidth;

    public void setPaddleWidth(int aWidth) {
        paddleWidth = aWidth;
    }
    public int getPaddleWidth() { return paddleWidth;}

}

Then as an example powerup, you'd have something like:

PaddlePowerup implements Powerup {
    PowerupType type
    EffectType effect;

    public static void pickedUp(Paddle targetPaddle) {
        switch(effect) {
            case PowerupType.GROW:
                 targetPaddle.setPaddleWidth(targetPaddle.paddleWidth() * 2);
                 break;
            case PowerupType.SHRINK:
                 targetPaddle.setPaddleWidth(targetPaddle.paddleWidth() * 0.5f);
                 break;
            case PowerupType.INVISIBLE:
                 targetPaddle.setVisible(false);
                 break;
            ...
        }
    }
    public static void pickedUp(Ball targetBall) {
        switch(effect) {
            case PowerupType.INVISIBLE:
                 targetBall.setVisible(false);
                 break;
            case PowerupType.SLOW:
                 targetBall.setSpeed(targetBall.getSpeed() * 0.5f);
                 break;
            ...
        }
    }
}

The other answer doesn't go into how to do different power ups for different objects, like the ball, bricks or global parameters.

So when your paddle picks up a power up you could call something like this in your main game code (somewhere that has access to all your objects):

public void triggerPickup(Powerup powerup) {
    switch(powerup.type) {
        case PowerUpType.PaddlePowerup:
             pickedUp(playerPaddle);
        case PowerUpType.BallPowerup:
             pickedUp(playerBall);
        ...
    }
}