1
\$\begingroup\$

I'm making a simple Breakout clone in Python that will have very many powerups/powerdowns (so far I came up with 26). Some will affect the paddle (paddle missile, two paddles, short paddle, etc.), some will affect the ball (slow ball, destructo-ball, invisible ball, etc.), some will affect the bricks (brick scramble, move up, bricks indestructible, etc.), and some will affect other game aspects (extra life, more points, less points, etc.). I'm pretty sure I have the code to draw the falling powerups and test for collisions with the paddle down, but I'm confused about how to code the effects of the powerups. Since there are very many powerups, it seemed inefficient to add specific methods to each component as done in this tutorial. However, I can't think of an other ways to implement the powerups. I found a page that hints at some way to design powerup behavior using classes, but I'm at a loss for how to do that. (A short example would help.) Please give me a short code example of another way to implement the effects of the powerups.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ possible duplicate of Implementing Power-Ups in a game \$\endgroup\$
    – House
    Jun 19, 2012 at 20:41
  • 1
    \$\begingroup\$ The answer for that one seems to be to put modifiers in every class that is affected by the powerup. (The link to the other answer is broken :( ) That is the same solution as the tutorial I linked to. Is that the only way to do it? \$\endgroup\$
    – Eva
    Jun 19, 2012 at 21:09
  • \$\begingroup\$ Eva, I've posted an answer that might help. It's my interpretation of the accepted answer on the question I linked. Additionally, I didn't have any problems with the link in the other answer. Perhaps try again? \$\endgroup\$
    – House
    Jun 19, 2012 at 21:50

1 Answer 1

1
\$\begingroup\$

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);
        ...
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ That makes more sense. Thanks for the explanation. \$\endgroup\$
    – Eva
    Jun 19, 2012 at 22:00
  • \$\begingroup\$ Why not pass the paddle as a parameter to the powerup, and use the fact that they all implement an interface to make them each handle their own effects? That way you could throw out the ugly case statement, and it would be very easy to add in more powerups and effects. \$\endgroup\$
    – SomeGuy
    Jun 20, 2012 at 4:15
  • \$\begingroup\$ @SomeGuy I think that would work just fine. However, the paddle is not the only thing that can be affected. Additionally, I wasn't sure how or if interfaces worked in Python, so I didn't want to rely on an interface. \$\endgroup\$
    – House
    Jun 20, 2012 at 5:02
  • \$\begingroup\$ @Byte56 Now that you mentioned it, I don't think interfaces are standard in Python. Guess I just got used to PEAK... \$\endgroup\$
    – SomeGuy
    Jun 20, 2012 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .