午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Java實現(xiàn)貪吃蛇游戲

瀏覽:125日期:2022-08-28 09:04:56

最近JAVA和JSwing上手練習(xí)了一下貪吃蛇,供大家參考,具體內(nèi)容如下

Java實現(xiàn)貪吃蛇游戲

歡迎交流和加入新的內(nèi)容

用到了JSwing,下面是一些具體的思路

實現(xiàn)

* 蛇:

采用單鏈表記錄首尾,整個蛇被分為lattice格子,放在map里

* 移動:

我在實現(xiàn)的過程中發(fā)現(xiàn)最難得反而是蛇的定義和實現(xiàn)。一直想著怎么樣用單獨(dú)的方法表示出蛇來,但是如果將蛇單獨(dú)實現(xiàn),總有些細(xì)節(jié)實現(xiàn)起來特別麻煩

其實蛇移動并非牽一發(fā)而動全身,其實身子是沒有發(fā)生變化的,關(guān)鍵是兩點(diǎn):

a.頭的移動b.尾巴的移動

實現(xiàn):

直接把蛇實現(xiàn)在地圖的小格子里,不再單獨(dú)設(shè)置子類或者ArrayList等,Map里加上蛇頭的坐標(biāo),從而使得Map可以根據(jù)蛇頭改變蛇的坐標(biāo)(類似于變量交換)。為頭部單獨(dú)設(shè)置x,y,作為移動的方向(也可以作為靜態(tài)變量x和y,不過沒什么區(qū)別),為身子設(shè)置next指針,只要next.next不是尾巴,那么保持不變。如果next是尾巴,就把自己的設(shè)置為尾巴,并且改變next,使之成為普通地圖塊。(refresh方法)

* 控制方向:

使用鍵盤事件,目前僅設(shè)置了wasd四個

* 窗口設(shè)計:

view extends JPanel,控制顯示,然后在Lattice里調(diào)用Graphics.draw(...)實現(xiàn)對每個格子的顯示

下面是核心的map部分代碼(包括自動移動,檢測食物,增加長度等等)

import codes.myGame.snake.cell.Lattice; import java.util.Random; public class Smap { private boolean getFood = false;//如果得到食物,該指針設(shè)為true,并且在隨后的autoChange里增加蛇的長度 private boolean gameOver = false; private boolean directionChange = false;//這里標(biāo)志的作用是保證在一次運(yùn)動期間只會進(jìn)行一次轉(zhuǎn)向,使游戲更流暢 private int MAP_SIZE; private Lattice[][] map; private int directionX = 0;//下一次頭在當(dāng)前位置的哪個方向上 private int directionY = 1;//下一次頭在當(dāng)前位置的哪個方向上 private int[] head = new int[2];//記錄當(dāng)前頭的位置 private int[] food = new int[2];//記錄當(dāng)前食物的位置 public Smap(int size) { MAP_SIZE = size; map = new Lattice[MAP_SIZE][MAP_SIZE]; for(int i=0;i<size;i++){ for (int j = 0 ;j<size;j++){ map[i][j] = new Lattice(); } } map[MAP_SIZE/2][MAP_SIZE/2].setHead(true,map[MAP_SIZE/2][MAP_SIZE/2-1]);//初始化設(shè)置一個頭結(jié)點(diǎn),以及他的尾節(jié)點(diǎn) head[0] = MAP_SIZE/2; head[1] = MAP_SIZE/2; map[MAP_SIZE/2][MAP_SIZE/2-1].setRear(true,null); this.randFood(); } //模擬蛇的自動移動 public void autoChange(){ this.setHead(); if(food[0]==head[0] && food[1]==head[1]){//如果新的頭部碰觸到了食物,那么尾部增長 getFood = true; } if(!gameOver)this.setRear(); if(getFood)this.randFood(); directionChange = false; } //根據(jù)鍵盤事件,改變頭的下一次移動方向,注意 該移動方向是僅針對頭部的 //setDirection和setHead兩個方法需要互斥進(jìn)行,這里單線程,用synchronized即可 //(否則,如果當(dāng)前頭部在邊界位置,連續(xù)變幻方向可能導(dǎo)致在setHead里發(fā)生溢出) public synchronized void setDirection(int x,int y){ if(directionY!=y && directionX!=x &&!directionChange){ directionX = x; directionY = y; directionChange = true; } } public boolean gameOver(){ return gameOver;//頭碰到身子,證明gameOver } private synchronized void setHead(){ int i = head[0]; int j = head[1]; head[0] = ( head[0] + directionX + MAP_SIZE)%MAP_SIZE; head[1] = ( head[1] + directionY + MAP_SIZE )%MAP_SIZE; if(map[head[0]][head[1]].isBody())gameOver = true; map[head[0]][head[1]].setHead(true,map[i][j]); map[i][j].setBody(true,null); map[i][j].setHead(false,null); //傳入null表示不改變當(dāng)前指向 } //設(shè)置尾巴由于沒法像頭部那樣直接設(shè)置,這里只能采用鏈表遍歷的方式獲取尾巴 private void setRear(){ if(!getFood){ Lattice temp = map[head[0]][head[1]]; while (!temp.next.isRear())temp = temp.next; temp.next().setRear(false,null); temp.setRear(true,null); temp.setBody(false,null); } } private void randFood(){ getFood = false; map[food[0]][food[1]].setFood(false);//先把當(dāng)前的食物取消掉 boolean flag = false;//設(shè)置下一個食物 Random random = new Random(); int x = random.nextInt(MAP_SIZE); int y = random.nextInt(MAP_SIZE); while (!flag){ x = random.nextInt(MAP_SIZE); y = random.nextInt(MAP_SIZE); if(!map[x][y].isHead() && !map[x][y].isRear() &&!map[x][y].isBody())flag = true; } map[x][y].setFood(true); food[0] = x; food[1] = y; } public Lattice get(int row, int col){ return map[row][col]; } public int getMAP_SIZE() { return MAP_SIZE; }}

下面是顯示部分的代碼

顯示分為兩部分,一塊是利用Graphics.draw()方法實現(xiàn)單個單元格的繪制,另一塊設(shè)置view類繼承自JPanel。負(fù)責(zé)繪制圖畫顯示

public class Lattice { private boolean isBody = false; private boolean isHead = false; private boolean isFood = false; private boolean isRear = false; public Lattice next = null; public void setHead(boolean bool,Lattice next){ isHead = bool; if(next!=null)this.next = next; } public void setBody(boolean bool,Lattice next){ isBody = bool; if(next!=null)this.next = next; //傳入?yún)?shù)為null時,不改變當(dāng)前的next } public void setRear(boolean bool,Lattice next){ isRear = bool; this.next = next; } public void setFood(boolean bool){ isFood = bool; } public Lattice next(){ return next; } public boolean isHead(){ return isHead; } public boolean isFood(){ return isFood; } public boolean isRear(){ return isRear; } public boolean isBody(){ return isBody; } public void refresh(){ if(isHead){ isBody = true; isHead = false;// 怎么設(shè)置下一個頭呢?(考慮把DirectionX,Y放到Smap里,而不是這里) }else if(isBody){ if(next.isRear){ next.isRear = false; isRear = true; isBody = false; } } }// 在這里設(shè)置細(xì)胞可見 public void draw(Graphics g, int x, int y, int size) { g.setColor(black); g.drawRect(x, y, size, size); if ( isHead ) { g.setColor( red); g.fillRect(x, y, size, size); }else if ( isBody || isRear) { g.setColor(black); g.fillRect(x, y, size, size); }else if(isFood){ g.setColor( blue); g.fillRect(x, y, size, size); } }}

view部分:

import codes.myGame.snake.cell.Lattice;import javax.swing.*;import java.awt.*; public class View extends JPanel { private static final long serialVersionUID = -5258995676212660595L; private static final int GRID_SIZE = 32; //填充的像素數(shù)量 private Smap thisMap; public View(Smap map) { thisMap = map; } @Override public void paint(Graphics g) { super.paint(g); int size = thisMap.getMAP_SIZE(); for (int row = 0; row< size; row++ ) { for (int col = 0; col< size; col++ ) { Lattice lattice = thisMap.get(row, col); if ( lattice != null ) { lattice.draw(g, col*GRID_SIZE, row*GRID_SIZE, GRID_SIZE);//對應(yīng)的格子的顯示 } } } } @Override public Dimension getPreferredSize() {//創(chuàng)建該div大小 return new Dimension(thisMap.getMAP_SIZE()*GRID_SIZE+1, thisMap.getMAP_SIZE()*GRID_SIZE+1); }}

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

javascript經(jīng)典小游戲匯總

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 精品一区二区在线播放 | 国产欧美日韩视频 | 91热在线| 婷婷丁香花五月天 | 日韩av免费在线 | 日韩一区二区三区视频在线观看 | 青娱乐av| 欧美日韩国产色 | 亚洲一区无| 91国内揄拍国内精品对白 | 一区视频 | 在线观看国产成人 | 找个毛片看看 | 一级特黄aa大片欧美 | 51精品| 日日夜夜伊人 | 成人免费三级 | 欧美无限看| 国产视频久久久久久久 | 天堂中文字幕在线 | xxxxxx国产| 香蕉视频网页版 | 97成人免费视频 | 亚洲欧洲日韩av | 欧美日韩aaa | 欧美精品免费在线 | 麻豆视频在线观看免费网站黄 | 成人亚洲网站 | 欧美一级片免费观看 | 日本久热| 中文字幕在线日韩 | 在线不卡中文字幕 | 欧美一级精品 | 蜜桃精品在线 | 69色视频 | 美女张开腿流出白浆 | 老女人性淫交视频 | a国产视频| 亚欧在线视频 | 日本黄页免费 | 亚洲福利视频网 |