这是我思考了一天的游戏,错过了两节课,心痛啊,总用时2个星期,感动!!!
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class SupervisoryControlDesk extends JFrame implements ActionListener, ChangeListener, KeyListener { private static final long serialVersionUID = 1L; protected DataOfMatrix dataOfMatrix; Paint_canvas paint_canvas; GluttonousSnake gluttonousSnake; JButton btn_control, btn_reflesh; JSpinner spinner; public SupervisoryControlDesk() { super("贪吃蛇-(安心)"); dataOfMatrix = new DataOfMatrix(); paint_canvas = new Paint_canvas(dataOfMatrix); gluttonousSnake = new GluttonousSnake(this, dataOfMatrix, paint_canvas); setLayout(new BorderLayout()); plusNorth(); plusCenter(); setBounds(300, 0, 615, 678); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private void plusCenter() { getContentPane().add(paint_canvas); } private void plusNorth() { btn_control = new JButton("Start"); btn_control.addActionListener(this); spinner = new JSpinner(); spinner.setValue(500); spinner.addChangeListener(this); JPanel panel = new JPanel(); getContentPane().add(panel, "North"); panel.add(btn_control); panel.add(spinner); btn_reflesh = new JButton("Reflesh"); panel.add(btn_reflesh); btn_reflesh.addActionListener(this); } public static void main(String[] args) { new SupervisoryControlDesk(); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equalsIgnoreCase("Start")) { btn_control.setText("End"); btn_control.addKeyListener(this); gluttonousSnake.timer.restart(); gluttonousSnake.isStart = true; dataOfMatrix.reflesh(); } if (e.getActionCommand().equalsIgnoreCase("End")) { btn_control.setText("Start"); btn_control.removeKeyListener(this); gluttonousSnake.timer.stop(); gluttonousSnake.isStart = false; } if (e.getSource() == btn_reflesh) { if (dataOfMatrix != null) { dataOfMatrix.reflesh(); paint_canvas.repaint(); btn_control.requestFocus(); } } } @Override public void stateChanged(ChangeEvent e) { try { int size = Integer.parseInt(""+spinner.getValue()); if(size<10||size>1000){ size = 500; } gluttonousSnake.timer.setDelay(size); btn_control.requestFocus(); } catch (NumberFormatException e1) { JOptionPane.showConfirmDialog(this, "输入的值错误,请从新输入!!"); } } @Override public void keyTyped(KeyEvent e) {// 不用 } @Override public void keyPressed(KeyEvent e) {// 目标 gluttonousSnake.snakeMove_Listiner(e); } @Override public void keyReleased(KeyEvent e) {// 不用 } }
public class DataOfMatrix { int foodNumber = 10; int width , height;//行 列 int[][] snakeMatrix; public DataOfMatrix() { set(30,30); decorateFood(); } public void set(int width,int height){ this.width = width; this.height = height; snakeMatrix = new int[width][height]; //测试数据 // for (int i = 0; i < width; i++) { // snakeMatrix[i][0] = 4; // snakeMatrix[i][height-1] = 4; // } // for (int j = 0; j < height; j++) { // snakeMatrix[0][j] = 4; // snakeMatrix[width-1][j] = 4; // } } public void decorateFood() {// decorateFood 布置食物 int[] temp = decorateFood_number(); for (int i = 0; i < temp.length; i++) { int text_i = temp[i] / height; int text_j = temp[i] % height; snakeMatrix[text_i][text_j] = 4;// 食物为4 } } public int[] decorateFood_number() {// 布置指定范围数量的食物个数 int[] mat = new int[foodNumber]; for (int i = 0; i < mat.length; i++) { mat[i] = isExist(mat, getRandomNumber()); } return mat; } public int isExist(int[] mat, int rand) { for (int i = 0; i < mat.length; i++) { if (rand == mat[i]) { rand = getRandomNumber(); rand = isExist(mat, rand); } } return rand; } public int getRandomNumber() { int temp = (int) (Math.random() * 1000) % (width * height); return temp; } public void reflesh(){ for (int i = 0; i < snakeMatrix.length; i++) { for (int j = 0; j < snakeMatrix[i].length; j++) { snakeMatrix[i][j] = 0; } } decorateFood(); System.out.println("刷新"); } }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.Timer; public class GluttonousSnake implements ActionListener { static int countSnake_number = 6; SupervisoryControlDesk supervisoryControlDesk; DataOfMatrix dataOfMatrix; Paint_canvas paint_canvas; SnakePartly[] smallSnake; Timer timer; boolean isStart; public GluttonousSnake(SupervisoryControlDesk supervisoryControlDesk, DataOfMatrix dataOfMatrix, Paint_canvas paint_canvas) { this.supervisoryControlDesk = supervisoryControlDesk; this.dataOfMatrix = dataOfMatrix; this.paint_canvas = paint_canvas; bornFromSnakeBody(); timer = new Timer(100, this); timer.setDelay(500); } private void bornFromSnakeBody() { smallSnake = new SnakePartly[17];// 0号不用 // 1+10+1 头-身体-尾,第一个不用,用来做方向 int[] temp = new int[4]; temp[1] = 1;// 默认向下 for (int i = 0; i < smallSnake.length; i++) { smallSnake[i] = new SnakePartly(15 - i, 8, temp); } smallSnake[1].setShape(1); smallSnake[2].setShape(2); smallSnake[3].setShape(2); smallSnake[4].setShape(2); smallSnake[5].setShape(3); } public void snakeMove_Listiner(KeyEvent e) { smallSnake[0].reflesh(); if (e.getKeyCode() == KeyEvent.VK_UP) { smallSnake[0].directions[0] = -1; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { smallSnake[0].directions[1] = 1; } if (e.getKeyCode() == KeyEvent.VK_LEFT) { smallSnake[0].directions[2] = -1; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { smallSnake[0].directions[3] = 1; } } public void snake_move() { // 擦掉最后一个元素 dataOfMatrix.snakeMatrix[smallSnake[countSnake_number - 1].x][smallSnake[countSnake_number - 1].y] = 0; for (int i = countSnake_number - 1; i > 0; i--) {// i = 5初始不小于5 // 改变方向 for (int j = 0; j < smallSnake[i].directions.length; j++) { smallSnake[i].directions[j] = smallSnake[i - 1].directions[j]; } int text_i = smallSnake[i].x + smallSnake[i].directions[0] + smallSnake[i].directions[1]; int text_j = smallSnake[i].y + smallSnake[i].directions[2] + smallSnake[i].directions[3]; // 撞墙就以顺时针的方向为准 // 0 上 1 下 2 左 3 右 if (text_j < 0) {// 左 text_j = dataOfMatrix.width-1; } if (text_j == dataOfMatrix.width) {// 右 text_j = 0; } if (text_i < 0) {// 上 text_i = dataOfMatrix.height-1; } if (text_i == dataOfMatrix.height) {// 下 text_i = 0; } //从头吃,尾长 if (i == 1 && dataOfMatrix.snakeMatrix[text_i][text_j] == 4) {// 贪吃,只能可能是头吃 countSnake_number++; for (int j = 0; j < 4; j++) { smallSnake[countSnake_number-1].directions[j] = smallSnake[countSnake_number-2].directions[j]; } smallSnake[countSnake_number-1].x = smallSnake[countSnake_number-2].x+ -(smallSnake[countSnake_number-1].directions[0]+ smallSnake[countSnake_number-1].directions[1] ); smallSnake[countSnake_number-1].y = smallSnake[countSnake_number-2].y+ -(smallSnake[countSnake_number-1].directions[2]+ smallSnake[countSnake_number-1].directions[3] ); smallSnake[countSnake_number-2].body_shape = 2; smallSnake[countSnake_number-1].body_shape = 3; } // 改变蛇的位置 smallSnake[i].x = text_i; smallSnake[i].y = text_j; // 将蛇的数据放入数组中 dataOfMatrix.snakeMatrix[text_i][text_j] = smallSnake[i].body_shape; } } @Override public void actionPerformed(ActionEvent e) {// timer 定时调用 if (!isStart) { return; } if (countSnake_number > 15) {//蛇脱皮,也可以吃蛇皮 System.out.println("吃饱了"); countSnake_number = 6; smallSnake[countSnake_number-1].body_shape = 3; isStart = false; dataOfMatrix.snakeMatrix[0][0] = 10; paint_canvas.repaint(); supervisoryControlDesk.btn_control.setText("Start"); return; } snake_move(); paint_canvas.repaint(); } } class SnakePartly { public int x, y; public int[] directions = new int[4]; public int body_shape; public SnakePartly(int x, int y, int[] location) { set(x, y, location, 0);// 默认不画 } public void reflesh() { if (directions != null) { for (int i = 0; i < directions.length; i++) { directions[i] = 0; } } } public void set(int x, int y, int[] directions, int body_shape) { this.x = x; this.y = y; for (int i = 0; i < directions.length; i++) { this.directions[i] = directions[i]; } this.body_shape = body_shape; } public void setShape(int shape) { this.body_shape = shape; } public void setLocation(int[] location) { for (int i = 0; i < location.length; i++) { directions[i] = location[i]; } } public void setLocation(int n) { int temp[] = new int[4]; if (n == 0 || n == 2) { temp[n] = -1; } if (n == 1 || n == 3) { temp[n] = 1; } setLocation(temp); } }
import java.awt.Canvas; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; public class Paint_canvas extends Canvas { private static final long serialVersionUID = 1L; DataOfMatrix dataOfMatrix; boolean isChangColor; public Paint_canvas(DataOfMatrix dataOfMatrix) { this.dataOfMatrix = dataOfMatrix; this.setSize(600, 600); } @Override public void paint(Graphics g) { if (dataOfMatrix != null) { if(dataOfMatrix.snakeMatrix[0][0]==10){ Font fontGameOver = new Font("宋体", Font.BOLD, 50); g.setFont(fontGameOver); g.setColor(Color.red); g.drawString("游戏结束", 120, 180); } for (int i = 0; i < dataOfMatrix.width; i++) { g.setColor(Color.blue); g.drawLine(i * 20, 0, i * 20, 600);// 列 } for (int j = 0; j < dataOfMatrix.height; j++) { g.setColor(Color.blue); g.drawLine(0, j * 20, 600, j * 20);// 行 } for (int i = 0; i < dataOfMatrix.width; i++) { for (int j = 0; j < dataOfMatrix.height; j++) { if (dataOfMatrix.snakeMatrix[i][j] == 4) { if(isChangColor){ g.setColor(Color.blue); }else{ g.setColor(Color.red); } isChangColor = !isChangColor; g.fillOval(j * 20,i * 20, 20, 20);// 行 } if (dataOfMatrix.snakeMatrix[i][j] == 3) { g.setColor(Color.black); g.drawLine(j * 20, i * 20, j * 20 + 20, i * 20);// 上 g.drawLine(j * 20 + 10, i * 20, j * 20, i * 20 + 20);// 中间,左下脚 g.drawLine(j * 20 + 10, i * 20, j * 20 + 20, i * 20 + 20);// 中间,右下脚 } if (dataOfMatrix.snakeMatrix[i][j] == 2) { g.setColor(Color.gray); g.fillRect(j * 20,i * 20, 20, 20); } if (dataOfMatrix.snakeMatrix[i][j] == 1) { g.setColor(Color.black); g.fillOval(j * 20,i * 20, 20, 15); } } } } } }
时间: 2024-10-16 19:49:39