|
| 1 | +package pong; |
| 2 | + |
| 3 | +import static pong.Game.SCALE; |
| 4 | + |
| 5 | +import java.awt.Color; |
| 6 | +import java.awt.Graphics; |
| 7 | +import java.awt.Rectangle; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +public class Ball { |
| 11 | + |
| 12 | + public double x, y; |
| 13 | + public static final int WIDTH_B = 10; |
| 14 | + public static final int HEIGHT_B = 10; |
| 15 | + |
| 16 | + public double dx, dy; |
| 17 | + public double speed = 2; |
| 18 | + |
| 19 | + public Ball(int x, int y) { |
| 20 | + this.x = x; |
| 21 | + this.y = y; |
| 22 | + |
| 23 | + int angle = new Random().nextInt((360 - 1)) + 1; //minimo de 1 e maximo de 360 graus |
| 24 | + while ((angle >= 1 && angle <= 30) || |
| 25 | + (angle >= 70 && angle <= 130) || |
| 26 | + (angle >= 170 && angle <= 210) || |
| 27 | + (angle >= 260 && angle <= 300) || |
| 28 | + (angle >= 340)){ |
| 29 | + angle = new Random().nextInt((360 - 1)) + 1; |
| 30 | + } |
| 31 | + dx = Math.cos(Math.toRadians(angle)); |
| 32 | + dy = Math.sin(Math.toRadians(angle)); |
| 33 | + |
| 34 | + //dx = new Random().nextGaussian(); |
| 35 | + //dy = new Random().nextGaussian(); |
| 36 | + /*dx = randInt(-1,1); |
| 37 | + dy = randInt(-1,1); |
| 38 | + if(dx == 0){ |
| 39 | + dx = 1; |
| 40 | + } |
| 41 | + if(dy==0){ |
| 42 | + dy = 1; |
| 43 | + }*/ |
| 44 | + } |
| 45 | + |
| 46 | + public void tick() { |
| 47 | + x += dx * speed; |
| 48 | + y += dy * speed; |
| 49 | + |
| 50 | + //colisao com paredes |
| 51 | + if (((y + (dy * speed)) + (HEIGHT_B/SCALE)) >= Game.HEIGHT) { |
| 52 | + dy *= -1; |
| 53 | + }else if( (y+(dy*speed)) < 0 ){ |
| 54 | + dy *= -1; |
| 55 | + } |
| 56 | + |
| 57 | + //marcando pontos |
| 58 | + if(x >= Game.WIDTH){ |
| 59 | + //ponto do player |
| 60 | + System.out.println("Ponto do player"); |
| 61 | + new Game(); |
| 62 | + return; |
| 63 | + }else if(x <= 0){ |
| 64 | + //ponto do inimigo |
| 65 | + System.out.println("Ponto do inimigo"); |
| 66 | + new Game(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + //colisao entre bola e player e inimigos |
| 71 | + Rectangle bounds = new Rectangle((int)(x+(dx*speed)),(int)(y+(dy*speed)),WIDTH_B/SCALE,HEIGHT_B/SCALE); |
| 72 | + Rectangle boundsPlayer = new Rectangle(Game.player.x,Game.player.y,(int)(Game.player.WIDTH_P/SCALE),(int)(Game.player.HEIGHT_P/SCALE)); |
| 73 | + Rectangle boundsEnemy = new Rectangle((int)Game.enemy.x,(int)Game.enemy.y,(int)(Game.enemy.WIDTH_E/SCALE),(int)(Game.enemy.HEIGHT_E/SCALE)); |
| 74 | + if(bounds.intersects(boundsPlayer)){ |
| 75 | + int angle = new Random().nextInt((360 - 1)) + 1; //minimo de 1 e maximo de 360 graus |
| 76 | + while (angle >= 91 && angle <= 270){ |
| 77 | + angle = new Random().nextInt((360 - 1)) + 1; |
| 78 | + } |
| 79 | + dx = Math.cos(Math.toRadians(angle)); |
| 80 | + dy = Math.sin(Math.toRadians(angle)); |
| 81 | + if(dx < 0) |
| 82 | + dx*=-1; |
| 83 | + }else if(bounds.intersects(boundsEnemy)){ |
| 84 | + int angle = new Random().nextInt((360 - 1)) + 1; //minimo de 1 e maximo de 360 graus |
| 85 | + while ((angle >= 1 && angle <= 90) || |
| 86 | + (angle >= 271 && angle <= 360)){ |
| 87 | + angle = new Random().nextInt((360 - 1)) + 1; |
| 88 | + } |
| 89 | + dx = Math.cos(Math.toRadians(angle)); |
| 90 | + dy = Math.sin(Math.toRadians(angle)); |
| 91 | + if(dx > 0) |
| 92 | + dx*=-1; |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + public void render(Graphics g) { |
| 98 | + g.setColor(Color.WHITE); |
| 99 | + g.fillRect((int) x, (int) y, WIDTH_B / SCALE, HEIGHT_B / SCALE); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + public static int randInt(int min, int max) { |
| 104 | + |
| 105 | + // NOTE: This will (intentionally) not run as written so that folks |
| 106 | + // copy-pasting have to think about how to initialize their |
| 107 | + // Random instance. Initialization of the Random instance is outside |
| 108 | + // the main scope of the question, but some decent options are to have |
| 109 | + // a field that is initialized once and then re-used as needed or to |
| 110 | + // use ThreadLocalRandom (if using at least Java 1.7). |
| 111 | + // |
| 112 | + // In particular, do NOT do 'Random rand = new Random()' here or you |
| 113 | + // will get not very good / not very random results. |
| 114 | + Random rand = new Random(); |
| 115 | + |
| 116 | + // nextInt is normally exclusive of the top value, |
| 117 | + // so add 1 to make it inclusive |
| 118 | + int randomNum = rand.nextInt((max - min) + 1) + min; |
| 119 | + |
| 120 | + return randomNum; |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments