예제 #1
0
  public void drawRedraw(Graphics2D graphics) {

    if (running) {

      this.randomPoint.drawPoint(graphics);

      if (this.snake1.isWithinLimits() && this.snake1.canPlay()) {

        this.snake1.updateMovement();
        this.snake1.drawPoints(graphics);

        if (this.snake1.interact(randomPoint)) {
          randomPoint.setCenter(Point.randomPoint(1, 49, 1, 49));
        }

        this.snake1.crashWithOther(this.snake2.getPointList());

      } else {
        this.snake1.setCanPlay(false);
        this.snake1.setColorAll(graphics, Color.RED);
        // Write Game Over Screen
      }

      if (this.snake2.isWithinLimits() && this.snake2.canPlay()) {

        this.snake2.updateMovement();
        this.snake2.drawPoints(graphics);

        if (this.snake2.interact(randomPoint)) {
          randomPoint.setCenter(Point.randomPoint(1, 49, 1, 49));
        }

        this.snake2.crashWithOther(this.snake1.getPointList());

      } else {
        this.snake2.setCanPlay(false);
        this.snake2.setColorAll(graphics, Color.BLUE);
        // Write Game Over Screen
      }

      if (!this.snake1.canPlay() && !this.snake2.canPlay()) {
        this.running = false;
      }

    } else {

      // graphics.

    }
  }
예제 #2
0
  public SnakeGame() {
    this.gameThread = new Thread();
    this.setPreferredSize(new Dimension(WIDTH - 5, HEIGHT - 5));
    this.setBackground(Color.BLACK);

    try {
      BufferedImage image =
          ImageIO.read(this.getClass().getResourceAsStream("/resources/appleTrans.png"));
      snake1 = new Snake(new Point(14, 14), Color.RED, Color.ORANGE);
      snake2 = new Snake(new Point(70, 70), Color.BLUE, Color.CYAN);
      randomPoint = new CirclePoint(Point.randomPoint(1, 49, 1, 49), image);

    } catch (IOException e) {

      snake1 = new Snake(new Point(14, 14));
      snake2 = new Snake(new Point(70, 70), Color.BLUE, Color.CYAN);
    }

    if (randomPoint == null)
      randomPoint = new CirclePoint(Point.randomPoint(1, 49, 1, 49), Color.MAGENTA);
    this.gameThread.start();
  }