1、JavaAWT实现贪吃蛇游戏import project.MainPanel;/* * 入口函数 * * author Administrator * */public class Main public static void main(String args) new MainPanel(); package project;import java.util.LinkedList;/* * 存放程序运行时数据 * * author Administrator * */public class Config /* * 节点尺寸 */ public static final int LEN =
2、 25; public static final int ROW = 20; public static final int COLUMN = 20; /* * 边距 */ public static final int LEFT_LIMIT = 10; public static final int UPER_LIMIT = 30; public static final int DOWN_LIMIT = LEN * ROW + UPER_LIMIT; public static final int RIGHT_LIMIT = LEN * COLUMN + LEFT_LIMIT; /* *
3、方向常量 */ public static final int UPER = 0, DOWN = 1, LEFT = 2, RIGHT = 3; /* * 画面布局数据存储数组 */ private static int panel = new intROWCOLUMN; /* * 移动方向 初始化向右 */ private static int direct = RIGHT; /* * 移动时间间隔 */ private static long time = 500; /* * 贪吃蛇数据存储容器 */ private static LinkedList track = new Linked
4、List(); /* * 增加移动时间间隔,上限为1000毫秒 */ public static void timeUp() if (time 100) time -= 100; /* * 获取当前时间间隔 * * return time */ public static long getTime() return time; /* * 获取当前界面数据 * * return */ public static int getPanel() return panel; /* * 设置移动方向 * * param direct */ public static void setDirect(int
5、 direct) Config.direct = direct; /* * 获得移动方向 * * return */ public static int getDirect() return direct; /* * 添加蛇头位置,如果与蛇身重复,抛出运行时异常用于结束游戏 * * param x * param y */ public static void add(int x, int y) panelxy = 1; for (int arr : track) if (arr0 = x & arr1 = y) throw new RuntimeException(); track.addF
6、irst(new int x, y ); /* * 移除蛇尾 */ public static void remove() int site = (int) track.removeLast(); panelsite0site1 = 0; package project;import java.awt.Graphics;import javax.swing.JFrame;SuppressWarnings(serial)/* * 界面布局类 * author Administrator * */public class MainPanel extends JFrame JFrame frame;
7、 Police police; public MainPanel() / TODO Auto-generated constructor stub frame = this; frame.setBounds(300, 300, Config.RIGHT_LIMIT, Config.DOWN_LIMIT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setTitle(贪吃蛇); init(); private void init() new Worm(this); pol
8、ice = new Police(); frame.addKeyListener(police); public void paint(Graphics g) super.paint(g); paintWorm(g); private void paintWorm(Graphics g) int panel = Config.getPanel(); for (int i = 0; i Config.ROW; i+) for (int j = 0; j Config.COLUMN; j+) if (panelij = 1) g.fillRect(Config.LEFT_LIMIT + Confi
9、g.LEN * j), (Config.UPER_LIMIT + Config.LEN * i), Config.LEN, Config.LEN); package project;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import static java.awt.event.KeyEvent.*;/* * 键盘监听器类 * * author Administrator * */public class Police implements KeyListener Override public void
10、 keyPressed(KeyEvent e) / TODO Auto-generated method stub char a = e.getKeyChar(); if (a = a) Config.setDirect(Config.LEFT); else if (a = s) Config.setDirect(Config.DOWN); else if (a = d) Config.setDirect(Config.RIGHT); else if (a = w) Config.setDirect(Config.UPER); else if (a = q) Config.timeUp();
11、else if (a = e) Config.timeDown(); int i = e.getKeyCode(); if (i = VK_LEFT) Config.setDirect(Config.LEFT); else if (i = VK_DOWN) Config.setDirect(Config.DOWN); else if (i = VK_RIGHT) Config.setDirect(Config.RIGHT); else if (i = VK_UP) Config.setDirect(Config.UPER); public void keyReleased(KeyEvent e
12、) / TODO Auto-generated method stub Override public void keyTyped(KeyEvent e) / TODO Auto-generated method stub package project;import javax.swing.JFrame;/* * 贪吃蛇构造类 * * author Administrator * */public class Worm implements Runnable int panel; /* * 随机食物位置 */ int food = new int2; /* * 蛇头位置信息 */ int h
13、ead; /* * 当前移动方向 */ int direct; JFrame frame; public Worm(JFrame frame) this.frame = frame; panel = Config.getPanel(); direct = Config.getDirect(); head = new int2; wormInit(); new Thread(this).start(); private void wormInit() for (int i = 1; i = 3; i+) Config.add(7, i); head0 = 7; head1 = 3; public
14、 void run() try foodRandom(); while (true) try Thread.sleep(Config.getTime(); catch (InterruptedException e) e.printStackTrace(); switch (Config.getDirect() case Config.UPER: if (direct = Config.DOWN) Config.setDirect(direct); break; Config.add(head0-, head1); if (head0 = food0 & head1 = food1) / 蛇头
15、移动到食物位置后,调用产生食物方法,并不调用移除蛇尾方法 foodRandom(); break; Config.remove(); break; case Config.DOWN: if (direct = Config.UPER) Config.setDirect(direct); break; Config.add(head0+, head1); if (head0 = food0 & head1 = food1) foodRandom(); break; Config.remove(); break; case Config.LEFT: if (direct = Config.RIGH
16、T) Config.setDirect(direct); break; Config.add(head0, head1-); if (head0 = food0 & head1 = food1) foodRandom(); break; Config.remove(); break; case Config.RIGHT: if (direct = Config.LEFT) Config.setDirect(direct); break; try Config.add(head0, head1+); catch (Exception e) if (head0 = food0 & head1 =
17、food1) foodRandom(); break; Config.remove(); break; direct = Config.getDirect(); frame.paint(frame.getGraphics(); catch (Exception e) frame.setTitle(Game Over); /* * 在随机位置产生食物 */ private void foodRandom() while (true) int x = (int) (Math.random() * Config.ROW); int y = (int) (Math.random() * Config.COLUMN); if (panelxy = 0) panelxy = 1; food0 = x; food1 = y; return;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1