Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
possible duplicate of Implementing Power-Ups in a game –  Byte56 Jun 19 '12 at 20:41
1  
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? –  Eva Jun 19 '12 at 21:09
    
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? –  Byte56 Jun 19 '12 at 21:50
add comment

1 Answer 1

up vote 1 down vote accepted

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);
        ...
    }
}
share|improve this answer
    
That makes more sense. Thanks for the explanation. –  Eva Jun 19 '12 at 22:00
    
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. –  SomeGuy Jun 20 '12 at 4:15
    
@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. –  Byte56 Jun 20 '12 at 5:02
    
@Byte56 Now that you mentioned it, I don't think interfaces are standard in Python. Guess I just got used to PEAK... –  SomeGuy Jun 20 '12 at 21:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.