界面:大紅色方塊----蛇頭,綠色----身體,粉色----食物
package com.snake; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.LinkedList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SnakeGame { public SnakeGame() { } public static void main(String[] args) { JFrame jf = new JFrame(); SnakeGame sg = new SnakeGame(); sg.init(jf, 800, 600); final SnakePanel sp = new SnakePanel(); //調用初始化地圖的方法 sp.initMap(); //調用初始化蛇的方法 sp.initSnake(); //調用生成食物的方法 sp.createFood(); sp.move(); jf.add(sp); jf.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); char ch = e.getKeyChar(); System.out.println("keyCode="+keyCode+",keyChar="+ch); //得到蛇頭的坐標 Point snakeHead = sp.snake.getFirst(); switch (keyCode) { case KeyEvent.VK_LEFT: //sp.snake.addFirst(new Point(snakeHead.x-1,snakeHead.y)); sp.setDirection(SnakePanel.LEFT); break; case KeyEvent.VK_RIGHT: sp.setDirection(SnakePanel.RIGHT); case 102: //小鍵盤的數字6 case 54: //大鍵盤的數字6 break; case KeyEvent.VK_UP: sp.setDirection(SnakePanel.UP); break; case KeyEvent.VK_DOWN: sp.setDirection(SnakePanel.DOWN); break; default: break; } //假定對應的寬度為40列,對應的是橫軸的坐標x final int X_WIDTH = 40; //假定對應的高度為30列,對應的是縱軸的坐標y final int Y_HEIGHT = 30; //撞牆了,游戲結束-----如果是 X_WIDTH-1或者 == 0,則蛇會進入牆壁,不允許! if(snakeHead.x == X_WIDTH-2|| snakeHead.x == 1|| snakeHead.y == Y_HEIGHT-2||snakeHead.y == 1) { String message = "GameOver!"; JOptionPane.showMessageDialog(sp, message); System.exit(0); } else{ sp.move(); } } }); } public void init(JFrame frame,int formWidth,int formHeight){ //設置當前窗體可見,默認不可見 frame.setVisible(true); //int formWidth = 300; //int formHeight = 200; //設置當前窗體的寬和高 frame.setSize(formWidth+14, formHeight+35); frame.setTitle("我的貪食蛇...."); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); //通過Dimension類的對象dim可以獲取到屏幕的寬和高 int screenWidth = dim.width; int screenHeight = dim.height; System.out.println("當前屏幕的分辨率為:"+screenWidth+"*"+screenHeight); int x = (screenWidth-formWidth)/2; int y = (screenHeight-formHeight)/2;; //設置當前窗體出現在窗口中坐標位置,即x軸的坐標值和y軸的坐標值 frame.setLocation(x, y); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class SnakePanel extends JPanel{ private static final long serialVersionUID = 1L; int x_width = 40; int y_height = 30; //假定對應的寬度為40列,對應的是橫軸的坐標x public static final int X_WIDTH = 40; //假定對應的高度為30列,對應的是縱軸的坐標y public static final int Y_HEIGHT = 30; /* * * 定義一個30*40二維數組 ,表示有30行40列 * 其中行對應的是y軸的坐標值 * 列對應的是x軸的坐標值 */ boolean[][]map = new boolean[Y_HEIGHT][X_WIDTH]; /* * 初始化地圖的方法: * 第一行、最后一行、第一列、最后一列表示游戲中的牆壁 * [1,28]*[1,38]表示的是蛇活動的地圖 * 如果是牆壁則賦值為true,否則活動區域為false * */ void initMap(){ for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { if(i==0 || i==map.length-1 || j==0 || j == map[i].length-1) map[i][j] = true; //else // map[i][j] = false; } } } LinkedList<Point> snake = new LinkedList<Point>(); /* * 繪制蛇以及保存蛇的坐標值 * 假定: * 蛇的整個長度為3,蛇頭一節,蛇身2節 * 蛇頭水平垂直居中顯示————即橫軸40/2-1,縱軸30/2-1 * * 初始化蛇的坐標值的方法並將坐標信息保存在LinkedList集合中 */ void initSnake(){ int x = X_WIDTH/2-1; //19 //水平居中的橫軸坐標x int y = Y_HEIGHT/2-1; //14 //垂直居中的縱軸坐標y for (int i = 0; i < 3; i++) { snake.add(new Point(x-i,y)); } } Point food; //食物的坐標 /** * 隨機生成食物的坐標 * 規則: * 1.食物不能在牆壁上 * 隨機生成的食物坐標的取值范圍 [1,38] * [1,28] * 也就是x坐標值取值為 [1,38]之間 * y坐標值取值為[1,28]之間 * * Math.ramdom() * 或者 * Random random = new Random(); * random.nextInt(int value) * */ public void createFood(){ Random random = new Random(); while (true) { int x = random.nextInt(X_WIDTH); int y = random.nextInt(Y_HEIGHT); System.out.println("x:"+x+",y:"+y); //x表示的是二維數組中的列,y表示的是二維數組中的行,所以在二維數組中先傳y在傳x,即先行后列 if (!map[y][x]) { food = new Point(x, y); break; } } }