⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat.java

📁 贪吃蛇游戏,游戏中移动的长方形代表蛇
💻 JAVA
字号:
package e;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GameSnake {
static Disktop d;

public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setBounds(100, 100, 500, 500);

      JPanel content = (JPanel) frame.getContentPane();

      d = new Disktop(400, 400);
      content.add(d);
      frame.setTitle("贪食蛇");
      frame.setVisible(true);

      frame.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
        System.exit(0);// 窗口关闭时自动关闭
       }
      });

      frame.addKeyListener(new KeyAdapter() {
       public void keyPressed(KeyEvent e) {
        int keycode = e.getKeyCode();
        Snake snake = d.getSnake();
        if (keycode == 38) {
         if (snake.direction == snake.DOWN)
          return;
         snake.direction = Snake.TOP;
        } else if (keycode == 40) {
         if (snake.direction == snake.TOP)
          return;
         snake.direction = Snake.DOWN;
        } else if (keycode == 37) {
         if (snake.direction == snake.RIGHT)
          return;
         snake.direction = Snake.LEFT;
        } else if (keycode == 39) {
         if (snake.direction == snake.LEFT)
          return;
         snake.direction = Snake.RIGHT;
        }
       }
      });

      d.startGame();// 启动游戏
}
}

/**
* 表示蛇的一段
*/
class Section {
int x;

int y;

int width = 10;

int height = 10;

public Section(int width, int height, int x, int y) {
      this.width = width;
      this.height = height;
      this.x = x;
      this.y = y;
}

// 绘制本块
public void draw(Graphics e) {
      e.drawRect(x, y, width, height);
      e.setColor(Color.black);
      e.fillRect(x, y, width, height);
}
}

/**
* 表示和绘制一条蛇
*/
class Snake {
// 方向常量
public static final int TOP = 1;

public static final int DOWN = 2;

public static final int LEFT = 3;

public static final int RIGHT = 4;

public static int WIDTH = 20;

public static int HEIGHT = 20;

LinkedList<Section> sectionLink;// body chain

public int direction = 2;// 方向

public Snake(int seNum, int x, int y, int direction) {
      sectionLink = new LinkedList<Section>();
      this.direction = direction;

      // 将默认身体块加入链表中
      Section s = null;
      int tx = x, ty = y;

      for (int i = 0; i < seNum; i++) {
       s = new Section(WIDTH, HEIGHT, tx, ty);
       sectionLink.add(s);

       // 根据方向来计算下一个方块位置
       switch (direction) {
       case TOP:
        ty += HEIGHT;
        break;
       case DOWN:
        ty -= HEIGHT;
        break;
       case LEFT:
        tx += WIDTH;
        break;
       case RIGHT:
        tx -= WIDTH;
        break;
       }
      }
}

// 绘制整条蛇
public void draw(Graphics e) {
      Section temp = null;

      ListIterator iterator = sectionLink.listIterator();
      while (iterator.hasNext()) {
       temp = (Section) iterator.next();
       temp.draw(e);
      }

}

// 增加一个身体块
public void add() {
      Section last = sectionLink.getLast();

      int tx = last.x, ty = last.y;
      // 根据方向来计算下一个方块位置
      switch (direction) {
      case TOP:
       ty += HEIGHT;
       break;
      case DOWN:
       ty -= HEIGHT;
       break;
      case LEFT:
       tx += WIDTH;
       break;
      case RIGHT:
       tx -= WIDTH;
       break;
      }

      Section s = new Section(WIDTH, HEIGHT, tx, ty);
      sectionLink.add(s);
}

// 获得蛇头
public Section getHead() {
      return (Section) sectionLink.getFirst();
}

// 向前走一步
public void go() {
      Section temp = null;

      for (int i = sectionLink.size() - 1; i > 0; i--) {
       temp = sectionLink.get(i);
       temp.x = sectionLink.get(i - 1).x;
       temp.y = sectionLink.get(i - 1).y;
      }
      Section head = sectionLink.getFirst();

      switch (direction) {
      case TOP:
       head.y -= HEIGHT;
       break;
      case DOWN:
       head.y += HEIGHT;
       break;
      case LEFT:
       head.x -= WIDTH;
       break;
      case RIGHT:
       head.x += WIDTH;
       break;
      }
}
}

/**
* 食物
*/
class Food {
int x;

int y;

public Food(int x, int y) {
      this.x = x;
      this.y = y;
}

// 绘制
public void draw(Graphics e) {
      e.setColor(Color.blue);
      e.drawOval(x, y, Snake.WIDTH, Snake.HEIGHT);
      e.setColor(Color.black);
}
}

/**
* 游戏桌面
*/
class Disktop extends JPanel {
int width;

int height;

Snake snake;

ArrayList<Food> foodList;

int score = 0;

public Disktop(int width, int height) {
      this.width = width;
      this.height = height;
      snake = new Snake(5, Snake.WIDTH * 5, Snake.RIGHT * 5, Snake.RIGHT);// 构造一条蛇
      foodList = new ArrayList<Food>();

}

// 开始游戏
public void startGame() {
      // 初始化食物
      int raint = new Random().nextInt(19);
      Food f = new Food(raint * Snake.WIDTH, raint * Snake.HEIGHT);
      foodList.add(f);
      if (!gameing()) {
       JOptionPane.showMessageDialog(this, "游戏结束");
      }
}

// 游戏实现部分
private boolean gameing() {
      while (true) {
       snake.go();
       // 比较 ,如果碰到自己则结束游戏
       Section temp = null;
       Section head = snake.getHead();

       ListIterator iterator = snake.sectionLink.listIterator();
       if (iterator.hasNext())
        iterator.next();

       while (iterator.hasNext()) {

        temp = (Section) iterator.next();
        if (head.x == temp.x && head.y == temp.y) {
         return false;
        }
       }
       // 检测是否走出边界
       if (head.x <= (0 - Snake.WIDTH) || head.y <= (0 - Snake.HEIGHT)
         || head.x >= width || head.y == height) {
        return false;
       }

       // 比较是否吃到食物
       for (int i = 0; i < foodList.size(); i++) {
        Food food = foodList.get(i);

        if (head.x == food.x && head.y == food.y) {
         score++;

         food.x = new Random().nextInt(19) * Snake.WIDTH;
         food.y = new Random().nextInt(19) * Snake.HEIGHT;
         snake.add();
        }
       }
       this.updateUI();
       try {
        Thread.sleep(100);
       } catch (InterruptedException e) {
        // TODO 自动生成 catch 块
        e.printStackTrace();
       }
      }
}

public void paintComponent(Graphics e) {// 刷新绘制图形
      e.drawRect(0, 0, width, height);
      e.clearRect(1, 1, width - 1, height - 1);
      e.clearRect(402, 80, 120, 50);
      e.drawString("你目前拥有" + score + "分", 402, 100);

      snake.draw(e);

      for (int i = 0; i < foodList.size(); i++) {
       Food food = foodList.get(i);
       food.draw(e);
      }
}

public Snake getSnake() {
      return snake;
}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -