Skip to content Skip to sidebar Skip to footer

How To Make Ball Bounce Off Paddle

import pygame BLACK = pygame.color.Color('Black') YELLOW = pygame.color.Color('Yellow') BLUE = pygame.color.Color('Blue') pygame.init() screen = pygame.display.set_mode([700,500]

Solution 1:

This should work:

# Inside the main loop.if ball_rect.collidelist([blue_rect, yellow_rect]) > -1:
    ball_x_speed = -ball_x_speed

Hope it helps!

Solution 2:

This is my ball class in java...........

Pong Ball

publicclassBall{

public int width = 30;
public int height = 30;
public int motionX;
public int motionY;
public int x, y;
private RectAnimation pong;
public Random random;

public int amountOfHits;

public Ball(RectAnimation pong) {
    this.random = new Random();
    this.pong = pong;
    spawn();
}

public void update(paddle paddle1, paddle paddle2) {

    int speed = 5;

    this.x += motionX * speed;
    this.y += motionY * speed;
    if (this.y + height > pong.height || this.y < 0) {
        if (this.motionY < 0) {
            this.y = 0;
            this.motionY = random.nextInt(4);
            if (motionY == 0) {
                motionY = 1;
            }
        } else {
            this.y = pong.height - height - motionY;
            this.motionY = -random.nextInt(4);
            if (motionY == 0) {
                motionY = -1;
            }

        }
    }
    if (checkCollision(paddle1) == 1) {
        this.motionX = 1 + (amountOfHits / 5);
        this.motionY = -2 + random.nextInt(4);
        if (motionY == 0) {
            motionY = 1;
        }
        amountOfHits++;
    } elseif (checkCollision(paddle2) == 1) {
        this.motionX = -1 - (amountOfHits / 5);
        this.motionY = -2 + random.nextInt(4);
        if (motionY == 0) {
            motionY = -1;
        }
    }

    if (checkCollision(paddle1) == 2) {
        paddle2.score++;
        spawn();
    } elseif (checkCollision(paddle2) == 2) {
        paddle1.score++;
        spawn();
    }
}

public void spawn() {
    this.amountOfHits = 0;
    this.x = pong.width / 2 - this.width / 2;
    this.y = pong.height / 2 - this.height / 2;
    this.motionX = -1 + random.nextInt(4);

    if (motionY == 0) {
        motionY = 1;
    }
    if (random.nextBoolean()) {
        motionX = 1;
    } else {
        motionX = -1;
    }

}

public int checkCollision(paddle pad) {

    if (this.x < pad.x + pad.width && this.x + width > pad.x && this.y < pad.y + pad.height && this.y + height > pad.y) {
        return1; //bounce
    } elseif (pad.x > x + width && pad.paddleNumber == 1 || pad.x < x && pad.paddleNumber == 2) {
        return2; //score
    }

    return0;

}

public void render(Graphics g) {

    g.setColor(Color.WHITE);
    g.fillOval(x, y, width, height);

}

}

How do it move you ask and instantiated....

publicclassRectAnimationimplementsActionListener, KeyListener {

publicstatic RectAnimation rectAnimation;
public gamePong renderer;

public paddle player1;
public paddle player2;

public Ball ball;
publicintwidth=700, height = 700;

publicbooleanbot=false;
publicbooleanw=false;
publicbooleans=false;
publicbooleanup=false;
publicbooleandown=false;
publicintgameStatus=0;
publicint botMoves;
publicintbotCoolDown=0;
publicintbotDifficulty=0;
publicRectAnimation() {

    Timertimer=newTimer(20, this);

    JFrameframe=newJFrame("Rectangle Animation");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);
    frame.setResizable(true);
    frame.setSize(width + 15, height + 30);
    frame.setLocation(375, 55);

    renderer = newgamePong();
    frame.add(renderer);

    frame.addKeyListener(this);

    timer.start();
}


publicvoidrender(Graphics2D g) {

    g.setColor(Color.BLACK);
    g.fillRect(0, 0, width, height);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    if (gameStatus == 0) {
        g.setColor(Color.WHITE);
        g.setFont(newFont("Ariel", 1, 50));
        g.drawString("PONG", width / 2 - 70, 50);

        g.setFont(newFont("Ariel", 1, 20));
        g.drawString("Press Space to Play", width / 2 - 90, height / 2 + 50);
    }

    if (gameStatus == 1 || gameStatus == 2) {
        g.setColor(Color.WHITE);
        g.setStroke(newBasicStroke(10));
        g.drawLine(width / 2, 0, width / 2, height);
        g.drawOval(width / 2 - 100, height / 2 - 100, 200, 200);
        g.setFont(newFont("Ariel", 1, 50));
        g.drawString(String.valueOf(player1.score), width / 2 - 70, 50);

        g.setFont(newFont("Ariel", 1, 50));
        g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);

        player1.render(g);
        player2.render(g);
        ball.render(g);
    }
    if (gameStatus == 1) {
        g.setColor(Color.WHITE);
        g.setFont(newFont("Ariel", 1, 50));
        g.drawString("PAUSED", width / 2 - 100, 50);
    }

}

publicvoidstart() {
    player1 = newpaddle(this, 1);
    player2 = newpaddle(this, 2);
    ball = newBall(this);
    gameStatus = 2;
}

publicvoidupdate() {

    if (w) {
        player1.move(true);
    } elseif (s) {
        player1.move(false);
    } elseif (!bot) {
        if (up) {
            player2.move(true);
        }
        if (down) {
            player2.move(false);
        }
    } else {

        if (botCoolDown > 0) {
            botCoolDown--;
            if (botCoolDown == 0) {
                botMoves = 0;
            }
        }

        if (botMoves < 10) {

            if (player2.y + player2.height < ball.y) {
                player2.move(false);
                botMoves++;
            }
            if (player2.y  + player2.height  > ball.y) {
                player2.move(true);
                botMoves++;
            }
            if(botDifficulty == 0)
                botCoolDown = 30;
            elseif(botDifficulty == 1)
                botCoolDown = 20;
            elseif(botDifficulty == 2)
                botCoolDown = 10;
        }
    }
    ball.update(player1, player2);
}

publicvoidactionPerformed(ActionEvent e) {
    if (gameStatus == 2) {
        update();
    }
    renderer.repaint();

}

/**
 * @param args the command line arguments
 */publicstaticvoidmain(String[] args) {
    // TODO code application logic here

    rectAnimation = newRectAnimation();

}

publicvoidkeyTyped(KeyEvent e) {

}

publicvoidkeyPressed(KeyEvent e) {
    intid= e.getKeyCode();

    if (id == KeyEvent.VK_W) {
        w = true;
    } elseif (id == KeyEvent.VK_S) {
        s = true;
    } elseif (id == KeyEvent.VK_UP) {
        up = true;
    } elseif (id == KeyEvent.VK_DOWN) {
        down = true;
    } elseif (id == KeyEvent.VK_ESCAPE && gameStatus == 2) {
        gameStatus = 0;
    } elseif (id == KeyEvent.VK_SHIFT && gameStatus == 0) {
        bot = true;
        start();
    }

    if (id == KeyEvent.VK_SPACE) {
        if (gameStatus == 0) {
            start();
            bot = false;
        } elseif (gameStatus == 1) {
            gameStatus = 2;
        } elseif (gameStatus == 2) {
            gameStatus = 1;
        }
    }
}

publicvoidkeyReleased(KeyEvent e) {

    intid= e.getKeyCode();

    if (id == KeyEvent.VK_W) {
        w = false;
    }
    if (id == KeyEvent.VK_S) {
        s = false;
    }
    if (id == KeyEvent.VK_UP) {
        up = false;
    }
    if (id == KeyEvent.VK_DOWN) {
        down = false;
    }

}

}

Post a Comment for "How To Make Ball Bounce Off Paddle"