最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java GUI編程之貪吃蛇游戲簡單實現(xiàn)方法【附demo源碼下載】

 更新時間:2017年09月19日 11:06:47   作者:送人玫瑰手留余香  
這篇文章主要介紹了Java GUI編程之貪吃蛇游戲簡單實現(xiàn)方法,詳細(xì)分析了貪吃蛇游戲的具體實現(xiàn)步驟與相關(guān)注意事項,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下

本文實例講述了Java GUI編程之貪吃蛇游戲簡單實現(xiàn)方法。分享給大家供大家參考,具體如下:

例子簡單,界面簡陋 請見諒

項目結(jié)構(gòu)如下

Constant.jvava 代碼如下:

package snake;
/**
 *
 * @author hjn
 *
 */
public class Constant {
/**
 * 蛇方移動方向:左邊
 */
public static final int LEFT = 0;
/**
 * 蛇方移動方向:右邊
 */
public static final int RIGHT = 1;
/**
 * 蛇方移動方向:上邊
 */
public static final int UP = 3;
/**
 * 蛇方移動方向:下邊
 */
public static final int DOWN = 4;
/**
 * 界面列數(shù)
 */
public static final int COLS = 30;
/**
 * 界面行數(shù)
 */
public static final int ROWS = 30;
/**
 * 每個格子邊長
 */
public static final int BODER_SIZE = 15;
}

Node.java代碼如下:

package snake;
/**
 * 格子
 *
 * @author hjn
 *
 */
public class Node {
/**
 * 所在行數(shù)
 */
private int row;
/**
 * 所在列數(shù)
 */
private int col;
public Node() {
};
public Node(int row, int col) {
this.row = row;
this.col = col;
};
/**
 * 蛇將要移動一格時頭部格子將所到格子
 *
 * @param dir
 *      蛇前進(jìn)方向
 * @param node
 *      蛇頭所在的格子
 */
public Node(int dir, Node node) {
if (dir == Constant.LEFT) {
this.col = node.getCol() - 1;
this.row = node.getRow();
} else if (dir == Constant.RIGHT) {
this.col = node.getCol() + 1;
this.row = node.getRow();
} else if (dir == Constant.UP) {
this.row = node.getRow() - 1;
this.col = node.getCol();
} else {
this.row = node.getRow() + 1;
this.col = node.getCol();
}
}
/**
 * 重寫equals方法
 */
public boolean equals(Object obj) {
if (obj instanceof Node) {
Node node = (Node) obj;
if (this.col == node.col && this.row == node.row) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public String toString() {
return "col:" + this.col + " row:" + this.row;
}
}

Egg.java代碼如下:

package snake;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
 * 蛋,蛇的食物
 *
 * @author Nan
 *
 */
public class Egg extends Node {
/**
 * 蛋的顏色
 */
Color color;
/**
 * 隨機(jī)函數(shù)
 */
public static Random random = new Random();
/**
 * 構(gòu)造函數(shù) 蛋出現(xiàn)在固定位置
 *
 * @param row
 *      所在第幾行數(shù)
 * @param col
 *      所在第幾列數(shù)
 */
public Egg(int row, int col) {
super(row, col);
this.color = Color.green;
}
/**
 * 構(gòu)造函數(shù) 蛋隨機(jī)出現(xiàn)
 *
 */
public Egg() {
super();
int col = random.nextInt(Constant.COLS - 4) + 2;
int row = random.nextInt(Constant.ROWS - 4) + 2;
this.setCol(col);
this.setRow(row);
}
/**
 * 畫蛋
 * @param g 畫筆
 */
void draw(Graphics g) {
if (this.color == Color.green) {
this.color = Color.red;
} else {
this.color = Color.green;
}
g.setColor(this.color);
int boderSize = Constant.BODER_SIZE;
g.fillOval(this.getCol() * boderSize, this.getRow() * boderSize,
boderSize, boderSize);
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}

Snake.java代碼如下:

package snake;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
/**
 * 蛇
 *
 * @author hjn
 *
 */
public class Snake {
/**
 * 前進(jìn)的方向
 */
int dir;
/**
 * 蛇的身體,由一個格子Node集合組成
 */
List<Node> nodeList = new ArrayList<Node>();
/**
 * 是否越界
 */
boolean isOverstep = false;
/**
 * 構(gòu)造方法默認(rèn)開始方向向左 ,蛇身有3個格子 ,位置在20行,15列
 */
public Snake() {
this.dir = Constant.LEFT;
for (int i = 0; i < 3; i++) {
Node node = new Node(20, 15 + i);
this.nodeList.add(node);
}
}
/**
 * 蛇前進(jìn)
 */
void forward() {
addNode();
nodeList.remove(nodeList.size() - 1);
}
/**
 * 蛇前進(jìn)的時候頭部增加格子,私有方法
 */
private void addNode() {
Node node = nodeList.get(0);
node = new Node(dir, node);
nodeList.add(0, node);
}
/**
 * 是否吃到蛋,蛇身是否有格子跟蛋重疊,所以重寫了Node的equals方法
 *
 * @param egg蛋
 * @return boolean
 */
boolean eatEgg(Egg egg) {
if (nodeList.contains(egg)) {
addNode();
return true;
} else {
return false;
}
}
/**
 * 畫自己
 *
 * @param g畫筆
 */
void draw(Graphics g) {
g.setColor(Color.black);
for (int i = 0; i < this.nodeList.size(); i++) {
Node node = this.nodeList.get(i);
if (node.getCol() > (Constant.COLS - 2) || node.getCol() < 2
|| node.getRow() > (Constant.ROWS - 2) || node.getRow() < 2) {
this.isOverstep = true;
}
g.fillRect(node.getCol() * Constant.BODER_SIZE, node.getRow()
* Constant.BODER_SIZE, Constant.BODER_SIZE,
Constant.BODER_SIZE);
}
forward();
}
/**
 * 鍵盤事件,來確定前進(jìn)方向,有左右上下4個方向
 *
 * @param e鍵盤監(jiān)聽事件
 */
void keyPress(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
if (this.dir != Constant.LEFT)
this.dir = Constant.LEFT;
break;
case KeyEvent.VK_RIGHT:
if (this.dir != Constant.RIGHT)
this.dir = Constant.RIGHT;
break;
case KeyEvent.VK_UP:
if (this.dir != Constant.UP)
this.dir = Constant.UP;
break;
case KeyEvent.VK_DOWN:
if (this.dir != Constant.DOWN)
this.dir = Constant.DOWN;
break;
default:
break;
}
}
public int getDir() {
return dir;
}
public void setDir(int dir) {
this.dir = dir;
}
public List<Node> getNodeList() {
return nodeList;
}
public void setNodeList(List<Node> nodeList) {
this.nodeList = nodeList;
}
public boolean isOverstep() {
return isOverstep;
}
public void setOverstep(boolean isOverstep) {
this.isOverstep = isOverstep;
}
}

主界面MainFrame.java代碼如下:

package snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
 * 貪吃蛇展示頁面
 *
 * @author hjn
 *
 */
public class MainFrame extends Frame {
/**
 * 版本
 */
private static final long serialVersionUID = -5227266702753583633L;
/**
 * 背景顏色
 */
Color color = Color.gray;
/**
 * 蛋
 */
static Egg egg = new Egg();
/**
 * 蛇
 */
Snake snake = new Snake();
/**
 * 游戲是否失敗
 */
boolean gameOver = false;
/**
 * 給畫筆起一個線程
 */
PaintThread paintThread = new PaintThread();
/**
 * 構(gòu)造方法
 */
public MainFrame() {
init();
}
/**
 * 界面初始化
 */
void init() {
this.setBounds(200, 200, Constant.COLS * Constant.BODER_SIZE,
Constant.ROWS * Constant.BODER_SIZE);
this.setResizable(true);
this.repaint();
/**
 * 窗口關(guān)閉監(jiān)聽事件
 */
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/**
 * 添加鍵盤監(jiān)聽事件
 */
this.addKeyListener(new KeyMomiter());
/**
 * 畫筆線程啟動
 */
new Thread(paintThread).start();
}
/**
 * 畫筆畫界面
 */
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(0, 0, Constant.COLS * Constant.BODER_SIZE, Constant.ROWS
* Constant.BODER_SIZE);
g.setColor(Color.DARK_GRAY);
for (int i = 0; i < Constant.ROWS; i++) {
g.drawLine(0, i * Constant.BODER_SIZE, Constant.COLS
* Constant.BODER_SIZE, i * Constant.BODER_SIZE);
}
for (int i = 0; i < Constant.COLS; i++) {
g.drawLine(i * Constant.BODER_SIZE, 0, i * Constant.BODER_SIZE,
Constant.ROWS * Constant.BODER_SIZE);
}
g.setColor(Color.yellow);
g.setFont(new Font("宋體", Font.BOLD, 20));
g.drawString("score:" + getScore(), 10, 60);
if (gameOver) {
g.setColor(Color.red);
g.drawString("GAME OVER", 100, 60);
this.paintThread.pause = true;
}
g.setColor(c);
if (snake.eatEgg(egg)) {
egg = new Egg();
}
snake.draw(g);
egg.draw(g);
}
/**
 * 獲取分?jǐn)?shù)
 *
 * @return int 分?jǐn)?shù)
 */
int getScore() {
return snake.getNodeList().size();
}
/**
 * 畫筆的線程
 *
 * @author hjn
 */
class PaintThread implements Runnable {
private boolean isRun = true;
private boolean pause = false;
@Override
public void run() {
while (isRun) {
if (pause) {
continue;
} else {
if (snake.isOverstep == true) {
gameOver = true;
}
repaint();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
 * 暫停
 */
public void pause() {
this.pause = true;
}
/**
 * 重新開始
 */
public void restart() {
this.pause = true;
snake = new Snake();
}
/**
 * 游戲結(jié)束
 */
public void gameOver() {
isRun = false;
}
}
/**
 * 停止
 */
void stop() {
gameOver = true;
}
/**
 * 鍵盤監(jiān)聽器
 *
 * @author hjn
 *
 */
class KeyMomiter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int key = e.getKeyCode();
if (key == KeyEvent.VK_F2) {
paintThread.restart();
} else {
snake.keyPress(e);
}
}
}
/**
 * 啟動程序入口
 *
 * @param args
 */
@SuppressWarnings("deprecation")
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
mainFrame.show();
}
}

運(yùn)行效果:

附:完整實例代碼點擊此處本站下載。

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • Springboot Websocket Stomp 消息訂閱推送

    Springboot Websocket Stomp 消息訂閱推送

    本文主要介紹了Springboot Websocket Stomp 消息訂閱推送,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • 三分鐘讀懂mybatis中resultMap和resultType區(qū)別

    三分鐘讀懂mybatis中resultMap和resultType區(qū)別

    這篇文章主要給大家介紹了mybatis中resultMap和resultType區(qū)別的相關(guān)資料,resultType和resultMap都是mybatis進(jìn)行數(shù)據(jù)庫連接操作處理返回結(jié)果的,需要的朋友可以參考下
    2023-07-07
  • Spring?Boot開發(fā)時Java對象和Json對象之間的轉(zhuǎn)換

    Spring?Boot開發(fā)時Java對象和Json對象之間的轉(zhuǎn)換

    在Spring?Boot開發(fā)中,我們經(jīng)常需要處理Java對象和Json對象之間的轉(zhuǎn)換,本文將介紹如何在Spring?Boot項目中實現(xiàn)Java對象和Json對象之間的轉(zhuǎn)換,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • RabbitMQ 的七種隊列模式和應(yīng)用場景

    RabbitMQ 的七種隊列模式和應(yīng)用場景

    最近學(xué)習(xí)RabbitMQ,本文就記錄一下RabbitMQ 的七種隊列模式和應(yīng)用場景,方便以后使用,也方便和大家共享,相互交流
    2021-05-05
  • Java、C++中子類對父類函數(shù)覆蓋的可訪問性縮小的區(qū)別介紹

    Java、C++中子類對父類函數(shù)覆蓋的可訪問性縮小的區(qū)別介紹

    這篇文章主要給大家介紹了關(guān)于Java、C++中子類對父類函數(shù)覆蓋的可訪問性縮小的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • JavaCV實現(xiàn)獲取視頻每幀并保存

    JavaCV實現(xiàn)獲取視頻每幀并保存

    這篇文章主要為大家詳細(xì)介紹了JavaCV實現(xiàn)獲取視頻每幀并保存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • spring?項目實現(xiàn)限流方法示例

    spring?項目實現(xiàn)限流方法示例

    這篇文章主要為大家介紹了spring項目實現(xiàn)限流的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • SpringBoot快速過濾出一次請求的所有日志的示例代碼

    SpringBoot快速過濾出一次請求的所有日志的示例代碼

    在現(xiàn)網(wǎng)出現(xiàn)故障時,我們經(jīng)常需要獲取一次請求流程里的所有日志進(jìn)行定位,本文給大家介紹了SpringBoot如何快速過濾出一次請求的所有日志,文中有相關(guān)的代碼和示例供大家參考,需要的朋友可以參考下
    2024-03-03
  • java8學(xué)習(xí)教程之lambda表達(dá)式的使用方法

    java8學(xué)習(xí)教程之lambda表達(dá)式的使用方法

    Java8最值得學(xué)習(xí)的特性就是Lambda表達(dá)式,下面這篇文章主要給大家介紹了關(guān)于java8學(xué)習(xí)教程之lambda表達(dá)式使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • JDK8 HashMap擴(kuò)容機(jī)制分析詳解

    JDK8 HashMap擴(kuò)容機(jī)制分析詳解

    這篇文章主要為大家介紹了JDK8 HashMap擴(kuò)容機(jī)制分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評論

肇州县| 郓城县| 阿拉善左旗| 奉化市| 宜宾市| 宁夏| 登封市| 冷水江市| 庆元县| 鹤峰县| 宽甸| 乌兰察布市| 汉寿县| 长汀县| 开封县| 济阳县| 乌兰察布市| 太湖县| 白沙| 大城县| 虹口区| 城步| 濮阳县| 彰化市| 航空| 建始县| 专栏| 乌鲁木齐县| 邹城市| 泽州县| 屏山县| 泰兴市| 杂多县| 湖北省| 安泽县| 康定县| 开原市| 康马县| 太和县| 金坛市| 梧州市|