更新時(shí)間:2022-07-27 11:51:20 來源:動力節(jié)點(diǎn) 瀏覽1426次
貪吃蛇是一款較老的經(jīng)典電子游戲。它最初是在 70 年代后期創(chuàng)建的。后來它被帶到了PC上。在這個(gè)游戲中,玩家控制一條蛇。目標(biāo)是盡可能多地吃蘋果。蛇每吃一個(gè)蘋果,它的身體就會變大。蛇必須避開墻壁和自己的身體。這個(gè)游戲有時(shí)被稱為Nibbles。
蛇的每個(gè)關(guān)節(jié)的大小是 10 像素。蛇是用光標(biāo)鍵控制的。最初,這條蛇有三個(gè)關(guān)節(jié)。如果游戲結(jié)束,棋盤中央會顯示“游戲結(jié)束”消息。
包 com.zetcode;
導(dǎo)入 java.awt.Color;
導(dǎo)入 java.awt.Dimension;
導(dǎo)入java.awt.Font;
導(dǎo)入 java.awt.FontMetrics;
導(dǎo)入 java.awt.Graphics;
導(dǎo)入 java.awt.Image;
導(dǎo)入 java.awt.Toolkit;
導(dǎo)入 java.awt.event.ActionEvent;
導(dǎo)入 java.awt.event.ActionListener;
導(dǎo)入 java.awt.event.KeyAdapter;
導(dǎo)入 java.awt.event.KeyEvent;
導(dǎo)入 javax.swing.ImageIcon;
導(dǎo)入 javax.swing.JPanel;
導(dǎo)入 javax.swing.Timer;
公共類 Board 擴(kuò)展 JPanel 實(shí)現(xiàn) ActionListener {
私人最終int B_WIDTH = 300;
私人最終int B_HEIGHT = 300;
私人最終 int DOT_SIZE = 10;
私人最終 int ALL_DOTS = 900;
私人最終 int RAND_POS = 29;
私人最終 int 延遲 = 140;
私有最終 int x[] = new int[ALL_DOTS];
私有最終 int y[] = new int[ALL_DOTS];
私有 int 點(diǎn);
私有 int apple_x;
私人 int apple_y;
私人布爾左方向=假;
私有布爾rightDirection = true;
私人布爾向上方向=假;
私人布爾下方向=假;
私人布爾 inGame = true;
私人定時(shí)器定時(shí)器;
私人形象球;
私人形象蘋果;
私有圖像頭;
公共板(){
初始化板();
}
私人無效初始化板(){
addKeyListener(new TAdapter());
設(shè)置背景(顏色。黑色);
可聚焦(真);
setPreferredSize(新維度(B_WIDTH, B_HEIGHT));
加載圖像();
初始化游戲();
}
私人無效加載圖像(){
ImageIcon iid = new ImageIcon("src/resources/dot.png");
球 = iid.getImage();
ImageIcon iia = new ImageIcon("src/resources/apple.png");
蘋果 = iia.getImage();
ImageIcon iih = new ImageIcon("src/resources/head.png");
頭 = iih.getImage();
}
私人無效初始化游戲(){
點(diǎn) = 3;
for (int z = 0; z < 點(diǎn); z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
定位蘋果();
計(jì)時(shí)器 = 新計(jì)時(shí)器(延遲,此);
計(jì)時(shí)器.start();
}
@覆蓋
公共無效paintComponent(圖形g){
super.paintComponent(g);
繪圖(g);
}
私人無效doDrawing(圖形g){
如果(游戲中){
g.drawImage(蘋果,apple_x,apple_y,這個(gè));
for (int z = 0; z < 點(diǎn); z++) {
如果(z == 0){
g.drawImage(頭, x[z], y[z], 這個(gè));
} 別的 {
g.drawImage(球, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} 別的 {
游戲結(jié)束(g);
}
}
私人無效gameOver(圖形g){
String msg = "游戲結(jié)束";
小字體 = new Font("Helvetica", Font.BOLD, 14);
FontMetrics 度量 = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(小);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
私人無效檢查蘋果(){
if ((x[0] == apple_x) && (y[0] == apple_y)) {
點(diǎn)++;
定位蘋果();
}
}
私人無效移動(){
for (int z = 點(diǎn); z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
如果(左方向){
x[0] -= DOT_SIZE;
}
如果(右方向){
x[0] += DOT_SIZE;
}
如果(向上方向){
y[0] -= DOT_SIZE;
}
如果(向下方向){
y[0] += DOT_SIZE;
}
}
私人無效檢查碰撞(){
for (int z = 點(diǎn); z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
游戲內(nèi)=假;
}
}
如果(y[0] >= B_HEIGHT){
游戲內(nèi)=假;
}
如果 (y[0] < 0) {
游戲內(nèi)=假;
}
如果 (x[0] >= B_WIDTH) {
游戲內(nèi)=假;
}
如果 (x[0] < 0) {
游戲內(nèi)=假;
}
如果(!游戲中){
計(jì)時(shí)器.stop();
}
}
私人無效locateApple(){
int r = (int) (Math.random() * RAND_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
apple_y = ((r * DOT_SIZE));
}
@覆蓋
公共無效actionPerformed(ActionEvent e){
如果(游戲中){
檢查蘋果();
檢查碰撞();
移動();
}
重繪();
}
私有類 TAdapter 擴(kuò)展 KeyAdapter {
@覆蓋
公共無效keyPressed(KeyEvent e){
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
左方向=真;
向上方向 = 假;
向下方向 = 假;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
正確方向=真;
向上方向 = 假;
向下方向 = 假;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
向上方向=真;
正確的方向=假;
左方向=假;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
向下方向 = 真;
正確的方向=假;
左方向=假;
}
}
}
}
首先,我們將定義游戲中使用的常量。
私人最終int B_WIDTH = 300;
私人最終int B_HEIGHT = 300;
私人最終 int DOT_SIZE = 10;
私人最終 int ALL_DOTS = 900;
私人最終 int RAND_POS = 29;
私人最終 int 延遲 = 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ù)決定了游戲的速度 。
私有最終 int x[] = new int[ALL_DOTS];
私有最終 int y[] = new int[ALL_DOTS];
這兩個(gè)數(shù)組存儲蛇所有關(guān)節(jié)的 x 和 y 坐標(biāo)。
私人無效加載圖像(){
ImageIcon iid = new ImageIcon("src/resources/dot.png");
球 = iid.getImage();
ImageIcon iia = new ImageIcon("src/resources/apple.png");
蘋果 = iia.getImage();
ImageIcon iih = new ImageIcon("src/resources/head.png");
頭 = iih.getImage();
}
在該loadImages()方法中,我們獲取游戲的圖像。該類ImageIcon用于顯示 PNG 圖像。
私人無效初始化游戲(){
點(diǎn) = 3;
for (int z = 0; z < 點(diǎn); z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
定位蘋果();
計(jì)時(shí)器 = 新計(jì)時(shí)器(延遲,此);
計(jì)時(shí)器.start();
}
在initGame()我們創(chuàng)建蛇的方法中,在板上隨機(jī)定位一個(gè)蘋果,然后啟動計(jì)時(shí)器。
私人無效檢查蘋果(){
if ((x[0] == apple_x) && (y[0] == apple_y)) {
點(diǎn)++;
定位蘋果();
}
}
如果蘋果撞到頭部,我們增加蛇的關(guān)節(jié)數(shù)。我們調(diào)用locateApple()隨機(jī)定位新蘋果對象的方法。
在move()方法中我們有游戲的關(guān)鍵算法。要理解它,看看蛇是如何移動的。我們控制了蛇的頭。我們可以用光標(biāo)鍵改變它的方向。其余關(guān)節(jié)沿鏈條向上移動一個(gè)位置。第二個(gè)關(guān)節(jié)移動到第一個(gè)關(guān)節(jié)的位置,第三個(gè)關(guān)節(jié)移動到第二個(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é)沿鏈向上移動。
如果(左方向){
x[0] -= DOT_SIZE;
}
這條線將頭部向左移動。
在該checkCollision()方法中,我們確定蛇是撞到自己還是撞到了一堵墻。
for (int z = 點(diǎn); z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
游戲內(nèi)=假;
}
}
如果蛇用頭撞到它的一個(gè)關(guān)節(jié),游戲就結(jié)束了。
如果(y[0] >= B_HEIGHT){
游戲內(nèi)=假;
}
如果蛇擊中棋盤底部,則游戲結(jié)束。
包 com.zetcode;
導(dǎo)入 java.awt.EventQueue;
導(dǎo)入 javax.swing.JFrame;
公共類蛇擴(kuò)展 JFrame {
公共蛇(){
初始化界面();
}
私人無效initUI(){
添加(新板());
可調(diào)整大小(假);
盒();
setTitle("蛇");
setLocationRelativeTo(null);
設(shè)置默認(rèn)關(guān)閉操作(JFrame.EXIT_ON_CLOSE);
}
公共靜態(tài)無效主要(字符串[]參數(shù)){
EventQueue.invokeLater(() -> {
JFrame ex = new Snake();
ex.setVisible(true);
});
}
}
這是主要編程。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743