Exemple #1
1
  public static void main(String[] args) {
    // String output;
    Ball BigBall = new Ball(5);
    JFrame frame1 = new JFrame();
    frame1.setTitle("Welcome to Ship!");
    frame1.setSize(300, 300);
    frame1.setLocation(200, 100);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox<String> box = new JComboBox<String>(new String[] {"Casey", "Connolly", "Nielson"});
    JPanel panel = new JPanel();
    panel.add(box);
    frame1.add(panel);
    frame1.setVisible(true);

    TV tv1 = new TV();
    tv1.turnOn();
    tv1.setChannel(30);
    tv1.setVolume(3);

    System.out.println(tv1.toString());
    Stack stacker = new Stack();

    for (int i = 0; i < 10; i++) stacker.push(i);
    while (!stacker.empty()) System.out.println(stacker.pop() + " ");

    System.out.println(BigBall.toString());
    System.out.println("Finished");
  }
  public void protocolGameInfo() {
    /* On vérifie que c'est bien la première fois qu'on reçoit les informations (pour cela on regarde
     * si l'ensemble des joueurs est vide) */
    if (pong.setPlayers.isEmpty()) {
      /* On place la balle à la bonne position */
      int positionBallX = Integer.parseInt(tab[1]);
      int positionBallY = Integer.parseInt(tab[2]);
      Ball ball = pong.getBall();
      ball.setPosition(new Point(positionBallX, positionBallY));
      pong.setBall(ball);

      /* On instancie tous les joueurs distants */
      for (int i = 3; i < tab.length; i++) {
        String[] curPlayer = tab[i].split(";");
        PlayerID playerID = PlayerID.valueOf(curPlayer[0]);
        int score = Integer.parseInt(curPlayer[1]);
        int positionRacketX = Integer.parseInt(curPlayer[2]);
        int positionRacketY = Integer.parseInt(curPlayer[3]);
        String host = curPlayer[4];
        int port = Integer.parseInt(curPlayer[5]);

        Player player = new Player(playerID, score, positionRacketX, positionRacketY, host, port);
        pong.setPlayers.add(player);
      }
    }
  }
Exemple #3
1
  /**
   * All of the game's logic (per game iteration) is in this function. Given some initial game
   * state, it computes the next game state.
   */
  private void doGameLogic() {
    float px = mBall.x;
    float py = mBall.y;

    mBall.move();

    // Shake it up if it appears to not be moving vertically
    if (py == mBall.y && mBall.serving() == false) {
      mBall.randomAngle();
    }

    // Do some basic paddle AI
    if (!mRed.player) doAI(mRed, mBlue);
    else mRed.move();

    if (!mBlue.player) doAI(mBlue, mRed);
    else mBlue.move();

    handleBounces(px, py);

    // See if all is lost
    if (mBall.y >= getHeight()) {
      mNewRound = true;
      mBlue.loseLife();

      if (mBlue.living()) playSound(mMissSFX);
      else playSound(mWinSFX);
    } else if (mBall.y <= 0) {
      mNewRound = true;
      mRed.loseLife();
      if (mRed.living()) playSound(mMissSFX);
      else playSound(mWinSFX);
    }
  }
Exemple #4
1
 /** Reset ball to an initial state */
 private void serveBall() {
   mBall.x = getWidth() / 2;
   mBall.y = getHeight() / 2;
   mBall.speed = Ball.SPEED + mBallSpeedModifier;
   mBall.randomAngle();
   mBall.pause();
 }
 public BallsFrame() throws HeadlessException {
   super("Funny balls");
   setLayout(new BorderLayout());
   executorService = Executors.newScheduledThreadPool(1);
   ballsPanel =
       new JPanel() {
         @Override
         protected void paintComponent(Graphics g) {
           super.paintComponent(g);
           if (blueBallVisible) {
             g.setColor(Color.BLUE);
             g.fillOval(blueBall.x, getHeight() / 2, BALL_DIAGONAL, BALL_DIAGONAL);
           }
           g.setColor(Color.RED);
           g.fillOval(redBall.x, getHeight() / 2, BALL_DIAGONAL, BALL_DIAGONAL);
         }
       };
   add(ballsPanel, BorderLayout.CENTER);
   blueBallSwitch = new JCheckBox("Collision", true);
   blueBallSwitch.addItemListener(
       e -> {
         if (blueBallVisible = blueBallSwitch.isSelected()) {
           int x;
           do x = random.nextInt(getWidth() - BALL_DIAGONAL);
           while (redBall.x <= x && x <= redBall.rightX()
               || redBall.x <= x + BALL_DIAGONAL && x + BALL_DIAGONAL <= redBall.rightX());
           blueBall.x = x;
         }
       });
   add(blueBallSwitch, BorderLayout.SOUTH);
   blueBallVisible = true;
   moveToRight = true;
   setSize(500, 500);
 }
Exemple #6
0
  @Override
  public void run() {
    while (!Thread.currentThread().isInterrupted()) {
      try {
        if (ball.getName().equals("reader")) {

          if (ball.isEnteringCs()) {
            readWrite.enterReader();
          } else if (ball.isLeavingCs()) {
            readWrite.exitReader();
          }

        } else if (ball.getName().equals("writer")) {
          if (ball.isEnteringCs()) {
            readWrite.enterWriter();
          } else if (ball.isLeavingCs()) {
            readWrite.exitWriter();
          }
        }
        ball.move();

        Thread.sleep(ball.getSpeed());

      } catch (InterruptedException ex) {
        if (ball.getName().equals("reader")) {
          readWrite.interruptedReader(ball);
        } else if (ball.getName().equals("writer")) {
          readWrite.interruptedWriter(ball);
        }
        Thread.currentThread().interrupt();
      }
    }
  }
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D graphics = (Graphics2D) g;

    graphics.setPaint(Color.GREEN);

    Ellipse2D.Double circle =
        new Ellipse2D.Double(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter());

    // graphics.fillOval(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter());
    graphics.fill(circle);
    if (frame.getWidth() != frameWidth || frame.getHeight() != frameHeight) {
      repaint();
      updateSizes(frame.getWidth(), frame.getHeight());
    }

    try {
      Thread.sleep(7);
    } catch (InterruptedException e) {
    }
    if (begin && !grabBall) {
      moveBall();
    } else if (grabBall) {
      followMouse();
    }
  }
  void the_derive(double time, double y[], double dy[]) {
    int i;
    BallVector balls = decode_balls(y); // new BallVector();//Ball[num_balls];
    BallVector dballs = new BallVector(); // new Ball[num_balls];

    for (i = 0; i < num_balls; i++) {
      Ball p = balls.get2(i);
      Ball d = new Ball();
      d.pos = p.speed;
      d.speed = wall_power(p);
      d.speed.y -= 1; // gravity
      dballs.add(d);
    }

    for (i = 0; i < num_balls; i++)
      for (int j = i + 1; j < num_balls; j++) {
        Ball p1 = balls.get2(i);
        Ball p2 = balls.get2(j);
        if (far_away_fast_calc(p1.pos, p2.pos, radius * 2)) continue;
        double dist = p1.pos.calc_dist(p2.pos);
        // if (dist>radius*2)
        //	continue;

        Vec collide_power = calc_collide_power(p1, p2, dist);
        dballs.get2(i).speed.add_to(collide_power);
        dballs.get2(j).speed.sub_to(collide_power);
      }
    for (i = 0; i < springs.size(); i++) {
      Spring s = springs.get2(i);
      Vec collide_power = calc_spring_power(balls.get2(s.start), balls.get2(s.end));
      dballs.get2(s.start).speed.add_to(collide_power);
      dballs.get2(s.end).speed.sub_to(collide_power);
    }
    encode_balls(dballs, dy);
  };
Exemple #9
0
 @Override
 public void run() {
   for (Ball e : balls) {
     e.update();
   }
   repaint();
 }
Exemple #10
0
 @Test
 public void resetBonusSpeed() {
   ball.increaseBonusSpeed();
   ball.increaseBonusSpeed();
   ball.resetBonusSpeed();
   Assert.assertEquals(0, ball.getBonusSpeed(), 0);
 }
Exemple #11
0
 public boolean isOver(Ball ball) {
   double rX = Math.pow(ball.getX() - x, 2);
   double rY = Math.pow(ball.getY() - y, 2);
   double rR = Math.pow(radius - ball.radius, 2);
   ballOver = (rX + rY) <= rR;
   return ballOver;
 }
  public Breakout() {
    top = new Wall(0, -5, 800, 10);
    left = new Wall(-5, 0, 10, 640);
    right = new Wall(795, 0, 10, 640);
    bottom = new Wall(0, 610, 800, 10);

    keys = new boolean[2];

    score = 0;
    lives = 5;

    ball = new Ball(400, 500);
    ball.setXSpeed(2);
    ball.setYSpeed(-2);

    paddle = new Paddle(400, 595, 100, 10, 5);

    bricks = getBricks("lvlTwo.txt");

    setBackground(Color.WHITE);
    setVisible(true);

    new Thread(this).start();
    addKeyListener(this); // starts the key thread to log key strokes
  }
 public void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2 = (Graphics2D) g;
   for (Ball b : balls) {
     g2.fill(b.getShape());
   }
 }
Exemple #14
0
  private void updateScorePoints() {
    Ball b;
    Barrier barrier;
    long score = 0;

    synchronized (m_newScorePoints) {
      Iterator<ScorePoint> it = m_newScorePoints.iterator();
      ScorePoint sp;
      while (it.hasNext()) {
        sp = it.next();
        sp.reduceAlpha();

        if (sp.getAlpha() <= 0) it.remove();
      }
    }

    synchronized (m_balls) {
      for (int i = 0; i < m_balls.size(); i++) {
        b = m_balls.get(i);
        synchronized (m_barriers) {
          for (int j = 0; j < m_barriers.size(); j++) {
            barrier = m_barriers.get(j);
            if (barrier.isPassed()) continue;
            if (b.center().y() >= barrier.rect().top) {
              addScore(b, m_points);
              barrier.setPassed(true);
              playSound("points");
              break;
            }
          }
        }
      }
    }

    score = m_score + mStartScore;

    // verifica nivel
    if (score > 700 && m_points == 2) {
      // m_sleep = m_sleep + 30;
      ScorePoint.color = Color.YELLOW;
      m_defaultSpeed = m_speed = 2;
      m_points = 3;
    } else if (score > 1400 && m_points == 3) {
      // m_sleep = m_sleep + 20;
      ScorePoint.color = Color.RED;
      m_defaultSpeed = m_speed = 3;
      m_points = 4;
    } else if (score > 2800 && m_points == 4) {
      ScorePoint.color = Color.DKGRAY;
      // m_sleep = m_sleep + 20;
      m_defaultSpeed = m_speed = 4;
      m_points = 5;
    } else if (score > 5600 && m_points == 5) {
      ScorePoint.color = Color.BLACK;
      // m_sleep = m_sleep + 10;
      m_defaultSpeed = m_speed = 5;
      m_points = 6;
    }
  }
Exemple #15
0
 public void updateSizes(int width, int height) {
   frameWidth = width;
   frameHeight = height;
   ball.setX((frameWidth - ball.getDiameter()) / 2);
   ball.setY((frameHeight - ball.getDiameter()) / 2);
   //		System.out.println(this.getWidth() +" " + this.getHeight());
   this.setSize(frameWidth, frameHeight);
 }
Exemple #16
0
 public void followMouse() {
   if (draggedMouseX != -1 && draggedMouseY != -1) {
     ball.setX(draggedMouseX - ball.getDiameter() / 2);
     ball.setY(draggedMouseY - ball.getDiameter() / 2);
     // System.out.println(draggedMouseX + " "+ draggedMouseY);
     repaint();
   }
 }
Exemple #17
0
 private void btnStartActionPerformed(
     java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnStartActionPerformed
   Random r = new Random();
   Ball ball =
       new Ball(canvas, new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256), r.nextInt(256)));
   ball.start();
   balls.add(ball);
 } // GEN-LAST:event_btnStartActionPerformed
 /**
  * Each Ball has a volume. This method returns the total volume of all the Balls in the container.
  *
  * @return the volume of the contents of the container.
  */
 public double getVolume() {
   double volume = 0.0;
   for (Ball b : contents) {
     volume += b.getVolume();
   }
   return volume;
   // throw new RuntimeException("Method not implemented");
 }
Exemple #19
0
  private boolean ballBelow() {

    if (!isMoving()) {
      return ball.getCenterY() > getY() + getHeight();
    } else {
      return ball.getCenterY() > getY();
    }
  }
Exemple #20
0
  private boolean ballAbove() {

    if (!isMoving()) {
      return ball.getCenterY() < getY();
    } else {
      return ball.getCenterY() < getY() + getHeight();
    }
  }
Exemple #21
0
 @Override
 public void paint(Graphics g) {
   super.paint(g);
   g.setColor(Color.BLACK);
   g.fillRect(0, 0, Ballz.FRAME_WIDTH, Ballz.FRAME_HEIGHT);
   for (Ball e : balls) {
     e.paint(g);
   }
 }
 public BallDestiny(
     final Ball b, final int idxJugador, final int iterJugador, final int iterBalon) {
   posBalon = new Position(b.getPosition()).setInsideGameField();
   this.altura = b.getAltura();
   this.idxJugador = idxJugador;
   this.iterJugador = iterJugador;
   this.iterBalon = iterBalon;
   ganaRival = false;
 }
 public void mouseMoved(MouseEvent e) { // called during motion when no buttons are down
   //  if (e.getY ==
   if ((e.getX() < appletsize_x - pad.width() / 2) && (e.getX() > (pad.width() / 2))) {
     pad.changeX(e.getX() - (pad.width() / 2));
     ball.moveWithPaddle(pad.xPos + (pad.width() / 2) - (ball.radius()));
   }
   e.consume();
   repaint();
 }
Exemple #24
0
 public void setPosition() {
   Ball.setVelocity(new Vector2(0, 0)); // Set velocity from ball to 0
   Ball.setPosition(
       new Vector2(
           2040 / 2 - (100 / 2),
           1360 - 190 - 100)); // Set the position from the bal in the middle of the screen
   SpriteHome.setPositionX(250 + 20); // Set the position from the sprite
   SpriteHome.setPositionY(1360 - 200 - 190); // Set the position from the sprite
 }
Exemple #25
0
 private void updateCircles() {
   for (int i = 0; i < ballArray.length; i++) {
     Ball b = ballArray[i];
     Circle c = circleArray[i];
     if (b != null) {
       c.setCenterX(b.getXPos());
       c.setCenterY(b.getYPos());
     }
   }
 }
 private void logic(int delta) {
   ball.update(delta);
   bat.update(delta);
   if (ball.getX() <= bat.getX() + bat.getWidth()
       && ball.getX() >= bat.getX()
       && ball.getY() >= bat.getY()
       && ball.getY() <= bat.getY() + bat.getHeight()) {
     ball.setDX(0.3);
   }
 }
Exemple #27
0
 public boolean attack(Ball ball) {
   int angle = this.calculateAngle(this.getX(), this.getY(), ball.getX(), ball.getY());
   GameInfo.currentMap[this.getY() / Config.slotHeight][this.getX() / Config.slotWidth] =
       this.getMapID() + angle;
   GameManager gameManager = GameManager.getInstance();
   int id = BallCache.generateBallID();
   gameManager.addBall(this.getBulletName(), this.getX(), this.getY(), ball, id);
   SendWrapper.sendBallAction(this, "TOWERATTACK");
   return true;
 }
  public void draw() {

    background(255);

    for (int i = 0; i < balls.size(); i++) {
      Ball ball = (Ball) balls.get(i);
      ball.calc();
      ball.display();
    }
  }
 @Override
 public void applyBallPower(Ball b1) {
   Point p1 = b1.getVelocityVector();
   System.out.println("Vel Vec before slowDown " + p1);
   if (Math.abs(p1.x) > 5) {
     p1.x -= p1.x / slowDown;
     p1.y -= p1.y / slowDown;
     b1.setVelocityVector(p1);
     System.out.println("Vel Vec after slowDown " + p1);
   }
 }
Exemple #30
0
 private void saveBalls(Board board) {
   List<Ball> balls = board.getBalls();
   for (Ball ball : balls) {
     fileOutput.format(
         BALL_FORMAT,
         giveName(ball),
         ball.getX(),
         ball.getY(),
         ball.getXVelocity(),
         ball.getYVelocity());
   }
 }