更新時(shí)間:2022-03-31 11:11:54 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1673次
貪吃蛇是一款較老的經(jīng)典電子游戲。它最初是在 70 年代后期創(chuàng)建的。后來它被帶到了PC上。在這個(gè)游戲中,玩家控制一條蛇。目標(biāo)是盡可能多地吃蘋果。蛇每吃一個(gè)蘋果,它的身體就會(huì)變大。蛇必須避開墻壁和自己的身體。這個(gè)游戲有時(shí)被稱為Nibbles。
蛇的每個(gè)關(guān)節(jié)的大小是 10 像素。蛇是用光標(biāo)鍵控制的。最初,這條蛇有三個(gè)關(guān)節(jié)。如果游戲結(jié)束,棋盤中央會(huì)顯示“游戲結(jié)束”消息。
package com.zetcode;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private final int B_WIDTH = 300;
private final int B_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 final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public Board() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
loadImages();
initGame();
}
private void loadImages() {
ImageIcon iid = new ImageIcon("src/resources/dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("src/resources/apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("src/resources/head.png");
head = iih.getImage();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
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 (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private 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] >= B_HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= B_WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
private 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));
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
}
}
首先,我們將定義游戲中使用的常量。
private final int B_WIDTH = 300;
private final int B_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;
B_WIDTH和B_HEIGHT常量決定了板子的大小 。DOT_SIZE是蘋果的大小和蛇的圓點(diǎn)。該ALL_DOTS常數(shù)定義了板上可能的最大點(diǎn)數(shù) (900 = (300*300)/(10*10))。該RAND_POS 常數(shù)用于計(jì)算蘋果的隨機(jī)位置。DELAY常數(shù)決定了游戲的速度 。
private final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];
這兩個(gè)數(shù)組存儲(chǔ)蛇所有關(guān)節(jié)的 x 和 y 坐標(biāo)。
private void loadImages() {
ImageIcon iid = new ImageIcon("src/resources/dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("src/resources/apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("src/resources/head.png");
head = iih.getImage();
}
在該loadImages()方法中,我們獲取游戲的圖像。該類ImageIcon用于顯示 PNG 圖像。
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
在initGame()我們創(chuàng)建蛇的方法中,在板上隨機(jī)定位一個(gè)蘋果,然后啟動(dòng)計(jì)時(shí)器。
private void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
如果蘋果撞到頭部,我們?cè)黾由叩年P(guān)節(jié)數(shù)。我們調(diào)用locateApple()隨機(jī)定位新蘋果對(duì)象的方法。
在move()方法中我們有游戲的關(guān)鍵算法。要理解它,看看蛇是如何移動(dòng)的。我們控制了蛇的頭。我們可以用光標(biāo)鍵改變它的方向。其余關(guān)節(jié)沿鏈條向上移動(dòng)一個(gè)位置。第二個(gè)關(guān)節(jié)移動(dòng)到第一個(gè)關(guān)節(jié)的位置,第三個(gè)關(guān)節(jié)移動(dòng)到第二個(gè)關(guān)節(jié)的位置,依此類推。
for (int z = 點(diǎn); z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
此代碼將關(guān)節(jié)沿鏈向上移動(dòng)。
if (leftDirection) {
x[0] -= DOT_SIZE;
}
這條線將頭部向左移動(dòng)。
在該checkCollision()方法中,我們確定蛇是撞到自己還是撞到了一堵墻。
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
如果蛇用頭撞到它的一個(gè)關(guān)節(jié),游戲就結(jié)束了。
if (y[0] >= B_HEIGHT) {
inGame = false;
}
如果蛇擊中棋盤底部,則游戲結(jié)束。
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Snake extends JFrame {
public Snake() {
initUI();
}
private void initUI() {
add(new Board());
setResizable(false);
pack();
setTitle("Snake");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame ex = new Snake();
ex.setVisible(true);
});
}
}
這是主要課程。
setResizable(false);
pack();
該方法會(huì)影響某些平臺(tái)上容器setResizable()的插圖。JFrame因此,在方法之前調(diào)用它很重要 pack()。否則,蛇的頭部與右邊界和下邊界的碰撞可能無法正常工作。
這是 Java 中的 Snake 游戲。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743