使用双向链表实现贪吃蛇程序
1. : :
package Snake; public class snakenode {private int x; private int y; private sakenode suivant; private sakenode avant; public snakenode () {} public snakenode (int x, int y) {super (); this.x = x; .y = y;} public int getX () {return x;} public void setx (int x) {this.x = x;} public int gety () {return y;} public void sey (int y) {this .y = y;} public snakenode getNext () {return next;} public void setNext (snakenode next) {this.next = next;} public snakenode getahead () {return avant;} public void seahead (snakenode Ahead) {this this this .ahead = avant;}}
:
Package Snake; import java.awt.borderlayout; import java.awt.gridlayout; import java.awt.keyeventpostprocessor; import java.awt.keyboardfocusmanager; import java.awt.event.keyevent; import java.util.random; import javax. swing.imageicon; import javax.swing.jframe; import javax.swing.jLabel; import javax.swing.joptionpane; import javax.swing.jpanel; / ** * créé par hackcoder le 15-3-11. * / classe publique Snake étend JFrame {private static final int. ; private static final int Left = 4; private static int drisection_now = droite; private static boolean iseeat = false; private static int tailx; private static int taily; private static snakenode snakeheader = new sakenode (); private static snakenode snaKetailler = Snakeheader; Food SnaKenode statique privé = new snakenode (); jlabel statique privé [] images = new JLabel [lignes * colonnes]; public static void main (String args []) {SnakeHeader.setx (new random (). NextInt (lignes - 1 ); if (food.getx () == SnakeHeader.getX () && food.gety () == SnakeHeader.gety ()) {addtail (); iseeat = true;} // 吃到食物 , 重新生成一个食物 if ( iSeAT) {food = getFood ();} // 判断是否结束游戏 if (jugeDend ()) {joptionpane.showMessagealiog (null, "游戏结束!", "游戏结束!", joptionpane.error_message); break;} snakenode pNow = SnakeHeader; while (pnow! = null) {images [colonnes * pnow.getx () + pnow.gety ()]. seticon (new ImageIcon ("image / noir.jpg", "")); pnow = pnow .GetNext ();} images [colonnes * food.getx () + food.gety ()]. seticon (new ImageIcon ("image / noir.jpg", "")); thread.sleep (100); //清理 pnow = SnakeHeader; while (pNow! = Null) {images [colonnes * pnow.getx () + pnow.gety ()]. Seticon (new ImageIcon ("image / white.jpg", "")); pnow = pnow.getNext ();} images [colonnes * food.getx () + food.gety ()]. seticon (new ImageIcon ("image / white.jpg", "")); iseeat = false;} catch (interruptedException e) {e.printStackTrace ();}}} public Snake () {init (); this.setBounds (80, 80, 400, 400); this.setVisible (true); setDefaultCloseOperation (javax.swing.windowconstants.exit_on_closeOPeration (javax.swing.windowconstants.exit_on_close ); // 添加全局键盘监听 KeyboardFocusManager Manager = KeyboardFocusManager.getCurrentKeyBoardFocusManager (); Manager.AddkeyEventPostProcessor ((KeyEventPostProcessor) this.getMyKeyEventHandler ());} / ** * 初始化地图 * / public vide init () {jpanel p = new Jpanel (new GridLayout (lignes, colonnes, 1, 1)); setLayout (new BorderLayout ()); pour (int x = 0; x <lignes; x ++) {pour (int y = 0; y <colonnes; y ++) {imageicon ImageIcon; if (x == 0 || x == lignes - 1 || y == 0 || y == colonnes - 1) { ImageIcon = new ImageIcon ("Image / Red.jpg", "");} else {imageIcon = new ImageIcon ("Image / White.jpg", "");} Images [colonnes * x + y] = new JLabel ( ImageIcon); p.add (images [colonnes * x + y]);}} getContentPane (). Add (p, borderLayout.Center);} / ** * 键盘监听 * * @return * / public keyEventPostProcessor getMyKeyEventHandler () {return new KeyEventPostProcessor () {public boolean postprocessKeyEvent (keyEvent e) {if (e.getId ()! = keyEvent.key_preresse ) ot ); int y2 = snakeHeader.getNext (). gety (); if (y1 == y2 && x1 - x2 == 1) {return true;}} driection_now = up;} else if (keycode == keyevent.vk_right) {if (snakeHeader.getNext ()! = null) {int x1 = SnakeHeader.getx (); int y1 = SnakeHeader.gety (); int x2 = SnakeHeader.getNext (). getX (); int y2 = SnakeHeader.getNext () .gety (); if (x1 == x2 && y2 - y1 == 1) {return true;}} drisection_now = right;} else if (keycode == keyevent.vk_down) {if (snakeheder.getNext () ! = null) {int x1 = SnakeHeader.getX (); int y1 = SnakeHeader.gety (); int x2 = SnakeHeader.getNext (). GetX (); int y2 = SnakeHeader.getNext (). Gety (); (y1 == y2 && x2 - x1 == 1) {return true;}} drisection_now = down;} else if (keycode == keyevent.vk_left) {if (SnakeHeader.getNext ()! = null) {int x1 = SnakeHeader.getX (); int y1 = SnakeHeader.gety (); int x2 = SnakeHeader.getNext (). getX (); int y2 = SnakeHeader.getNext (). Gety (); if (x1 == x2 && y1 - y2 == 1) {return true;}} drisection_now = Left;} return true;}};} / ** * 计算贪吃蛇的方向及位移 * * @param henter * / public static void Next () {if (SnakeHeader == null) return; tailx = snaKetailer.getx (); taily = snaKetailer.gety (); snakenode pnow = snaKetailer; while (pnow! = null) {if (pnow == SnakeHeader) {break;} pnow. setX (pnow.getaHead (). getX ()); pnow.sety (pnow.getaHead (). gety ()); pnow = pnow.getaHead ();} if (drisection_now == droite) {SnakeHeader.Sety (Snakeheader .gety () + 1);} else if (drisection_now == gauche) {SnakeHeader.Sety (SnakeHeader.gety () - 1);} else if (drisection_now == up) {SnakeHeader.setx (SnakeHeader.getx () - 1);} else if (drisection_now == down) {SnakeHeader.setx (SnakeHeader.getX () + 1);}} public static void addtail () {snakenode tail = new Snakenode (tailx, taily); snaKetailer.setnext (tail); tail.setaHead (snaKetailer); snaKetailer = snaKetailer.getNext ();} public static snakenode getFood () {snakenode food = new snakenode (); booléen drapeau = true; while (true) {int x = new random () .Nextint (lignes); int y = new random (). nextInt (colonnes); if (x == 0 || x == lignes - 1 || y == 0 || y == Colonnes - 1) {continue;} snakenode pnow = SnakeHeader; while (pnow! = null) {if (x == pnow.getx () && y == pnow.gety ()) {flag = false;} pnow = pnow.getNext ();} if (drapeau) {aliments = new snakenode (x, y); pause;}} return aliments;} public static booléen jugerend () {// 碰墙判断 if (snakeheader.getx ( ) == 0 || pNow = SnakeHeader.getNext (); while (pNow! = null) {if (SnakeHeader.getX () == pnow.getx () && SnakeHeader.gety () == pnow.gety ()) {System.out.printlnnnn ("========= 碰到身体 =========="); return true;} pnow = pnow.getNext ();} return false;}}