返回介绍

Nibbles in Java SWT

发布于 2025-02-22 22:19:35 字数 11764 浏览 0 评论 0 收藏 0

In this part of the Java SWT tutorial, we create a Nibbles game clone.

Nibbles is an older classic video game. It was first created in late 70s; later it was brought to PCs. In this game, the player controls a snake. The objective is to eat as many apples as possible. Each time the snake eats an apple, its body grows. The snake must avoid the walls and its own body.

Development

The size of each of the joints of a snake is 10 px. The snake is controlled with the cursor keys. Initially, the snake has three joints. The game starts immediately. When the game is finished, the "Game Over" message is shown in the center of the window.

Board.java

package com.zetcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;

public class Board extends Canvas {

  private final int WIDTH = 300;
  private final int HEIGHT = 300;
  private final int DOT_SIZE = 10;
  private final int ALL_DOTS = 900;
  private final int RAND_POS = 29;
  private final int DELAY = 140;

  private int x[] = new int[ALL_DOTS];
  private int y[] = new int[ALL_DOTS];

  private int dots;
  private int apple_x;
  private int apple_y;

  private boolean left = false;
  private boolean right = true;
  private boolean up = false;
  private boolean down = false;
  private boolean inGame = true;

  private Image ball;
  private Image apple;
  private Image head;

  private Display display;
  private Shell shell;
  private Runnable runnable;

  public Board(Shell shell) {
    super(shell, SWT.NULL);

    this.shell = shell;

    initBoard();
  }

  private void initBoard() {

    display = shell.getDisplay();

    addListener(SWT.Paint, event -> doPainting(event));
    addListener(SWT.KeyDown, event -> onKeyDown(event));

    addListener(SWT.Dispose, event -> {

      ball.dispose();
      apple.dispose();
      head.dispose();
    });

    Color col = new Color(shell.getDisplay(), 0, 0, 0);

    setBackground(col);
    col.dispose();

    loadImages();

    initGame();
  }

  private void loadImages() {

    ImageData iib = new ImageData("dot.png");
    ball = new Image(display, iib);

    ImageData iia = new ImageData("apple.png");
    apple = new Image(display, iia);

    ImageData iih = new ImageData("head.png");
    head = new Image(display, iih);
  }

  private void initGame() {

    dots = 3;

    for (int z = 0; z < dots; z++) {
      x[z] = 50 - z * 10;
      y[z] = 50;
    }

    locateApple();

    runnable = new Runnable() {
      @Override
      public void run() {

        if (inGame) {
          checkApple();
          checkCollision();
          move();

        }
        
        display.timerExec(DELAY, this);
        redraw();
      }
    };

    display.timerExec(DELAY, runnable);
  };

  private void doPainting(Event e) {

    GC gc = e.gc;

    Color col = new Color(shell.getDisplay(), 0, 0, 0);
    gc.setBackground(col);
    col.dispose();

    gc.setAntialias(SWT.ON);

    if (inGame) {
      drawObjects(e);
    } else {
      gameOver(e);
    }
  }

  private void drawObjects(Event e) {

    GC gc = e.gc;

    gc.drawImage(apple, apple_x, apple_y);

    for (int z = 0; z < dots; z++) {
      if (z == 0) {
        gc.drawImage(head, x[z], y[z]);
      } else {
        gc.drawImage(ball, x[z], y[z]);
      }
    }
  }

  private void gameOver(Event e) {

    GC gc = e.gc;

    String msg = "Game Over";

    Font font = new Font(e.display, "Helvetica", 12, SWT.NORMAL);
    Color whiteCol = new Color(e.display, 255, 255, 255);

    gc.setForeground(whiteCol);
    gc.setFont(font);

    Point size = gc.textExtent(msg);

    gc.drawText(msg, (WIDTH - size.x) / 2, (HEIGHT - size.y) / 2);

    font.dispose();
    whiteCol.dispose();

    display.timerExec(-1, runnable);
  }

  private void checkApple() {

    if ((x[0] == apple_x) && (y[0] == apple_y)) {
      dots++;
      locateApple();
    }
  }

  private void move() {

    for (int z = dots; z > 0; z--) {
      x[z] = x[(z - 1)];
      y[z] = y[(z - 1)];
    }

    if (left) {
      x[0] -= DOT_SIZE;
    }

    if (right) {
      x[0] += DOT_SIZE;
    }

    if (up) {
      y[0] -= DOT_SIZE;
    }

    if (down) {
      y[0] += DOT_SIZE;
    }
  }

  public void checkCollision() {

    for (int z = dots; z > 0; z--) {

      if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
        inGame = false;
      }
    }

    if (y[0] > HEIGHT - DOT_SIZE) {
      inGame = false;
    }

    if (y[0] < 0) {
      inGame = false;
    }

    if (x[0] > WIDTH - DOT_SIZE) {
      inGame = false;
    }

    if (x[0] < 0) {
      inGame = false;
    }
  }

  public void locateApple() {

    int r = (int) (Math.random() * RAND_POS);
    apple_x = ((r * DOT_SIZE));
    r = (int) (Math.random() * RAND_POS);
    apple_y = ((r * DOT_SIZE));
  }

  private void onKeyDown(Event e) {

    int key = e.keyCode;

    if ((key == SWT.ARROW_LEFT) && (!right)) {
      left = true;
      up = false;
      down = false;
    }

    if ((key == SWT.ARROW_RIGHT) && (!left)) {
      right = true;
      up = false;
      down = false;
    }

    if ((key == SWT.ARROW_UP) && (!down)) {
      up = true;
      right = false;
      left = false;
    }

    if ((key == SWT.ARROW_DOWN) && (!up)) {
      down = true;
      right = false;
      left = false;
    }
  }
}

First we will define some globals used in our game.

The WIDTH and HEIGHT constants determine the size of the Board. The DOT_SIZE is the size of the apple and the dot of the snake. The ALL_DOTS constant defines the maximum number of possible dots on the Board. The RAND_POS constant is used to calculate a random position of an apple. The DELAY constant determines the speed of the game.

private int x[] = new int[ALL_DOTS];
private int y[] = new int[ALL_DOTS];

These two arrays store x and y coordinates of all possible joints of a snake.

The initGame() method initialises variables, loads images, and starts a timeout function.

runnable = new Runnable() {
  @Override
  public void run() {

    if (inGame) {
      checkApple();
      checkCollision();
      move();

    }
    
    display.timerExec(DELAY, this);
    redraw();
  }
};

Every DELAY ms, the run() method is called. If we are in the game, we call three methods that build the logic of the game.

if (inGame) {
  drawObjects(e);
} else {
  gameOver(e);
}

Inside the doPainting() method, we check the inGame variable. If it is true, we draw our objects—the apple and the snake joints. Otherwise we display "Game over" text.

private void drawObjects(Event e) {

  GC gc = e.gc;

  gc.drawImage(apple, apple_x, apple_y);

  for (int z = 0; z < dots; z++) {
    if (z == 0) {
      gc.drawImage(head, x[z], y[z]);
    } else {
      gc.drawImage(ball, x[z], y[z]);
    }
  }
}

The drawObjects() method draws the apple and the joints of the snake. The first joint of a snake is its head, which is represented by a red circle.

public void checkApple() {

  if ((x[0] == apple_x) && (y[0] == apple_y)) {
    dots++;
    locateApple();
  }
}

The checkApple() method checks if the snake has hit the apple object. If so, we add another snake joint and call the locateApple() method, which randomly places a new apple object.

In the move() method we have the key algorithm of the game. To understand it, look at how the snake is moving. You control the head of the snake. You can change its direction with the cursor keys. The rest of the joints move one position up the chain. The second joint moves where the first was, the third joint where the second was etc.

for (int z = dots; z > 0; z--) {
  x[z] = x[(z - 1)];
  y[z] = y[(z - 1)];
}

This code moves the joints up the chain.

if (left) {
  x[0] -= DOT_SIZE;
}

Move the head to the left.

In the checkCollision() method, we determine if the snake has hit itself or one of the walls.

for (int z = dots; z > 0; z--) {

  if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
  inGame = false;
  }
}

We finish the game if the snake hits one of its joints with its head.

if (y[0] > HEIGHT - DOT_SIZE) {
  inGame = false;
}

We finish the game if the snake hits the bottom of the Board.

The locateApple() method locates an apple randomly on the board.

int r = (int) (Math.random() * RAND_POS);

We get a random number from 0 to RAND_POS - 1.

apple_x = ((r * DOT_SIZE));
...
apple_y = ((r * DOT_SIZE));

These line set the x and y coordinates of the apple object.

In the onKeyDown() method, we determine the keys that were pressed.

if ((key == SWT.ARROW_LEFT) && (!right)) {
  left = true;
  up = false;
  down = false;
}

If we hit the left cursor key, we set left variable to true. This variable is used in the move() method to change the coordinates of the snake object. Notice also that when the snake is heading to the right, we cannot turn immediately to the left.

Nibbles.java

package com.zetcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * In this code example, we create
 * a Nibbles game clone
 *
 * Author: Jan Bodnar
 * Website: zetcode.com
 * Last modified: June 2015
 */

public class Nibbles {
  
  private final int WIDTH = 300;
  private final int HEIGHT = 300;

  public Nibbles(Display display) {
    
    initUI(display);
  }
  
  @SuppressWarnings("unused")
  private void initUI(Display display) {

    Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
    
    FillLayout layout = new FillLayout();
    shell.setLayout(layout);

    Board board = new Board(shell);    

    shell.setText("Nibbles");
    int borW = shell.getSize().x - shell.getClientArea().width;
    int borH = shell.getSize().y - shell.getClientArea().height;
    shell.setSize(WIDTH + borW, HEIGHT + borH);
    
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
      display.sleep();
      }
    }
  }

  @SuppressWarnings("unused")
  public static void main(String[] args) {
    
    Display display = new Display();
    Nibbles ex = new Nibbles(display);
    display.dispose();
  }
}

In this class, we set up the Nibbles game.

int borW = shell.getSize().x - shell.getClientArea().width;
int borH = shell.getSize().y - shell.getClientArea().height;
shell.setSize(WIDTH + borW, HEIGHT + borH);

When setting the size of the shell, we need to take the window decorations into account too.

Nibbles
Figure: Nibbles

This was the Nibbles computer game programmed with the SWT library and the Java programming language.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文