Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game.pong;
- import java.awt.*;
- public class Ball {
- private int ballSize = 15;
- private int x = Game.WIDTH / 2;
- private int y = Game.HEIGHT / 2;
- private int xVelocity = -2;
- private int yVelocity = -2;
- public void update() {
- x = x + xVelocity;
- y = y + yVelocity;
- if(x < 0){
- xVelocity = 3;
- }else if(x + ballSize + 5 > 600){
- xVelocity = -3;
- }
- if(y < 0){
- yVelocity = 3;
- }else if(y + ballSize * 2 + 10 > 400){
- yVelocity = -3;
- }
- }
- public void paint(Graphics g){
- g.setColor(Color.WHITE);
- g.fillOval(x, y, ballSize, ballSize);
- }
- private void reverseVelocity(){
- xVelocity = -xVelocity + 2;
- }
- public void hasCollided(Player player){
- if(this.x > player.getX() && this.x < player.getX() + player.getWidth()){
- if(this.y > player.getY() && this.y < player.getY() + player.getHeight()){
- reverseVelocity();
- }
- }
- }
- public void hasCollided(Computer computer){
- if(this.x > computer.getX() && this.x < computer.getX() + computer.getWidth()){
- if(this.y > computer.getY() && this.y < computer.getY() + computer.getHeight()){
- reverseVelocity();
- }
- }
- }
- public int getY() {
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement