java中使用双向链表实现贪吃蛇程序源码分享
程序员文章站
2024-03-02 11:21:16
使用双向链表实现贪吃蛇程序
1.链表节点定义:
package snake;
public class snakenode {
private int...
使用双向链表实现贪吃蛇程序
1.链表节点定义:
package snake; public class snakenode { private int x; private int y; private snakenode next; private snakenode ahead; public snakenode() { } public snakenode(int x, int y) { super(); this.x = x; this.y = y; } public int getx() { return x; } public void setx(int x) { this.x = x; } public int gety() { return y; } public void sety(int y) { this.y = y; } public snakenode getnext() { return next; } public void setnext(snakenode next) { this.next = next; } public snakenode getahead() { return ahead; } public void setahead(snakenode ahead) { this.ahead = ahead; } }
主程序:
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; /** * created by hackcoder on 15-3-11. */ public class snake extends jframe { private static final int rows = 60; private static final int columns = 80; // 方向 private static final int up = 1; private static final int right = 2; private static final int down = 3; private static final int left = 4; private static int driection_now = right; private static boolean iseat = false; private static int tailx; private static int taily; private static snakenode snakeheader = new snakenode(); private static snakenode snaketailer = snakeheader; private static snakenode food = new snakenode(); private static jlabel[] images = new jlabel[rows * columns]; public static void main(string args[]) { snakeheader.setx(new random().nextint(rows - 1)); snakeheader.sety(new random().nextint(columns - 1)); snake snake = new snake(); food = getfood(); while (true) { try { next(); // 吃到了食物 if (food.getx() == snakeheader.getx() && food.gety() == snakeheader.gety()) { addtail(); iseat = true; } //吃到食物,重新生成一个食物 if (iseat) { food = getfood(); } // 判断是否结束游戏 if (judgeend()) { joptionpane.showmessagedialog(null, "游戏结束!", "游戏结束!", joptionpane.error_message); break; } snakenode pnow = snakeheader; while (pnow != null) { images[columns * pnow.getx() + pnow.gety()] .seticon(new imageicon("image/black.jpg", "")); pnow = pnow.getnext(); } images[columns * food.getx() + food.gety()] .seticon(new imageicon("image/black.jpg", "")); thread.sleep(100); // 清理 pnow = snakeheader; while (pnow != null) { images[columns * pnow.getx() + pnow.gety()] .seticon(new imageicon("image/white.jpg", "")); pnow = pnow.getnext(); } images[columns * food.getx() + food.gety()] .seticon(new imageicon("image/white.jpg", "")); iseat = false; } catch (interruptedexception e) { e.printstacktrace(); } } } public snake() { init(); this.setbounds(80, 80, 400, 400); this.setvisible(true); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); // 添加全局键盘监听 keyboardfocusmanager manager = keyboardfocusmanager .getcurrentkeyboardfocusmanager(); manager.addkeyeventpostprocessor((keyeventpostprocessor) this .getmykeyeventhandler()); } /** * 初始化地图 */ public void init() { jpanel p = new jpanel(new gridlayout(rows, columns, 1, 1)); setlayout(new borderlayout()); for (int x = 0; x < rows; x++) { for (int y = 0; y < columns; y++) { imageicon imageicon; if (x == 0 || x == rows - 1 || y == 0 || y == columns - 1) { imageicon = new imageicon("image/red.jpg", ""); } else { imageicon = new imageicon("image/white.jpg", ""); } images[columns * x + y] = new jlabel(imageicon); p.add(images[columns * x + y]); } } getcontentpane().add(p, borderlayout.center); } /** * 键盘监听 * * @return */ public keyeventpostprocessor getmykeyeventhandler() { return new keyeventpostprocessor() { public boolean postprocesskeyevent(keyevent e) { if (e.getid() != keyevent.key_pressed) { return false; } int keycode = e.getkeycode(); if (keycode == keyevent.vk_up) { if (snakeheader.getnext() != null) { // 判断方向是否可转 int x1 = snakeheader.getx(); int y1 = snakeheader.gety(); int x2 = snakeheader.getnext().getx(); 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; } } driection_now = right; } else if (keycode == keyevent.vk_down) { if (snakeheader.getnext() != null) { int x1 = snakeheader.getx(); int y1 = snakeheader.gety(); int x2 = snakeheader.getnext().getx(); int y2 = snakeheader.getnext().gety(); if (y1 == y2 && x2 - x1 == 1) { return true; } } driection_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; } } driection_now = left; } return true; } }; } /** * 计算贪吃蛇的方向及位移 * * @param header */ 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 (driection_now == right) { snakeheader.sety(snakeheader.gety() + 1); } else if (driection_now == left) { snakeheader.sety(snakeheader.gety() - 1); } else if (driection_now == up) { snakeheader.setx(snakeheader.getx() - 1); } else if (driection_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(); boolean flag = true; while (true) { int x = new random().nextint(rows); int y = new random().nextint(columns); if (x == 0 || x == rows - 1 || y == 0 || y == columns - 1) { continue; } snakenode pnow = snakeheader; while (pnow != null) { if (x == pnow.getx() && y == pnow.gety()) { flag = false; } pnow = pnow.getnext(); } if (flag) { food = new snakenode(x, y); break; } } return food; } public static boolean judgeend() { //碰墙判断 if (snakeheader.getx() == 0 || snakeheader.getx() == rows - 1 || snakeheader.gety() == 0 || snakeheader.gety() == columns - 1) { return true; } //碰身体判断 snakenode pnow = snakeheader.getnext(); while (pnow != null) { if (snakeheader.getx() == pnow.getx() && snakeheader.gety() == pnow.gety()) { system.out.println("=========碰到身体==========="); return true; } pnow = pnow.getnext(); } return false; } }
上一篇: 详解JAVA的封装