java版贪吃蛇
界面:大紅色方塊----蛇頭,綠色----身體,粉色----食物
?
????????????
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(); //調(diào)用初始化地圖的方法 sp.initMap(); //調(diào)用初始化蛇的方法 sp.initSnake(); //調(diào)用生成食物的方法 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); //得到蛇頭的坐標(biāo) 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: //小鍵盤的數(shù)字6 case 54: //大鍵盤的數(shù)字6 break; case KeyEvent.VK_UP: sp.setDirection(SnakePanel.UP); break; case KeyEvent.VK_DOWN: sp.setDirection(SnakePanel.DOWN); break; default: break; } //假定對(duì)應(yīng)的寬度為40列,對(duì)應(yīng)的是橫軸的坐標(biāo)x final int X_WIDTH = 40; //假定對(duì)應(yīng)的高度為30列,對(duì)應(yīng)的是縱軸的坐標(biāo)y final int Y_HEIGHT = 30; //撞墻了,游戲結(jié)束-----如果是 X_WIDTH-1或者 == 0,則蛇會(huì)進(jìn)入墻壁,不允許! 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){ //設(shè)置當(dāng)前窗體可見,默認(rèn)不可見 frame.setVisible(true); //int formWidth = 300; //int formHeight = 200; //設(shè)置當(dāng)前窗體的寬和高 frame.setSize(formWidth+14, formHeight+35); frame.setTitle("我的貪食蛇...."); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); //通過Dimension類的對(duì)象dim可以獲取到屏幕的寬和高 int screenWidth = dim.width; int screenHeight = dim.height; System.out.println("當(dāng)前屏幕的分辨率為:"+screenWidth+"*"+screenHeight); int x = (screenWidth-formWidth)/2; int y = (screenHeight-formHeight)/2;; //設(shè)置當(dāng)前窗體出現(xiàn)在窗口中坐標(biāo)位置,即x軸的坐標(biāo)值和y軸的坐標(biāo)值 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; //假定對(duì)應(yīng)的寬度為40列,對(duì)應(yīng)的是橫軸的坐標(biāo)x public static final int X_WIDTH = 40; //假定對(duì)應(yīng)的高度為30列,對(duì)應(yīng)的是縱軸的坐標(biāo)y public static final int Y_HEIGHT = 30; /* * * 定義一個(gè)30*40二維數(shù)組 ,表示有30行40列 * 其中行對(duì)應(yīng)的是y軸的坐標(biāo)值 * 列對(duì)應(yīng)的是x軸的坐標(biāo)值 */ boolean[][]map = new boolean[Y_HEIGHT][X_WIDTH]; /* * 初始化地圖的方法: * 第一行、最后一行、第一列、最后一列表示游戲中的墻壁 * [1,28]*[1,38]表示的是蛇活動(dòng)的地圖 * 如果是墻壁則賦值為true,否則活動(dòng)區(qū)域?yàn)閒alse * */ 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>(); /* * 繪制蛇以及保存蛇的坐標(biāo)值 * 假定: * 蛇的整個(gè)長(zhǎng)度為3,蛇頭一節(jié),蛇身2節(jié) * 蛇頭水平垂直居中顯示————即橫軸40/2-1,縱軸30/2-1 * * 初始化蛇的坐標(biāo)值的方法并將坐標(biāo)信息保存在LinkedList集合中 */ void initSnake(){ int x = X_WIDTH/2-1; //19 //水平居中的橫軸坐標(biāo)x int y = Y_HEIGHT/2-1; //14 //垂直居中的縱軸坐標(biāo)y for (int i = 0; i < 3; i++) { snake.add(new Point(x-i,y)); } } Point food; //食物的坐標(biāo) /** * 隨機(jī)生成食物的坐標(biāo) * 規(guī)則: * 1.食物不能在墻壁上 * 隨機(jī)生成的食物坐標(biāo)的取值范圍 [1,38] * [1,28] * 也就是x坐標(biāo)值取值為 [1,38]之間 * y坐標(biāo)值取值為[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表示的是二維數(shù)組中的列,y表示的是二維數(shù)組中的行,所以在二維數(shù)組中先傳y在傳x,即先行后列 if (!map[y][x]) { food = new
轉(zhuǎn)載于:https://www.cnblogs.com/tt-t/p/5767668.html
總結(jié)