运行效果展示
功能实现
- 程序框架:是利用IDEA的插件“JFormDesigner”来绘制的,里面的棋盘是一个JPanel
- 蛇的功能是由链表实现的,我写的Snake类继承JLabel,Snake有个next的成员变量,表示蛇的某一块连接到的下一块。每次移动,就会从蛇尾开始逐一遍历一直到蛇的头部,将蛇的每一块都移动到下一块的位置上。
- 蛇头的移动方向:Snake类里有一个direction静态成员变量,取值分别为EAST、WEST、NORTH、SOUTH,如果值为EAST,那么在每次移动,蛇头都会向右移动,其他同理。棋盘(JPanel)里添加了一个键盘监听事件,当键盘按下W或者↑,且当前的direction不为SOUTH时,就将direction的值改成NORTH。(这里的EAST、WEST、NORTH、SOUTH也不是什么枚举类型,只是一个int类型的值,是Snake里定义的常量)
- 速度控制:使用Timer(定时调度)
- 是否撞墙:直接判断蛇头的坐标有没有小于0或者大于棋盘的高度、宽度
- 是否吃到食物:需要分情况讨论,如果蛇头坐标是(a,b),食物坐标是(x,y),每个格子的边长是size,如果当前direction为NORTH,当且仅当a == x && y == b + size时,成功吃到食物,其他同理。每次吃到食物,都会将这个食物作为这条蛇的头部,将原来的头部标识成蛇身,并重新随机生成一个食物(使用Random类随机生成一个坐标)
- 是否自己撞自己:和是否吃到食物的判断方法一样
(可能有些地方写得不是很好,欢迎指正!)
代码
/*
* Created by JFormDesigner on Sat Jun 27 23:11:31 CST 2020
*/
//Main.java
package com.domain;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
import javax.swing.*;
import javax.swing.border.*;
/**
* @author 123
*/
public class Main extends JFrame {
public static Main main = null;
public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
main = new Main();
Snake.width = main.game.getWidth();
Snake.height = main.game.getHeight();
//启动检查
if(Snake.width % Snake.size != 0 && Snake.height % Snake.size != 0){
JOptionPane.showMessageDialog(null,"未知错误!","",JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}
}
public Main() throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
Font font = new Font("微软雅黑", Font.PLAIN, 14);
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource)
UIManager.put(key, font);
}
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
initComponents();
game.setFocusable(true); //始终获得焦点
game.requestFocus(false);
}
private int lastChoose = 0;
private int speed = Snake.NORMAL_SPEED;
private void levelItemStateChanged(ItemEvent e) {
// TODO add your code here
if (e.getStateChange() == ItemEvent.SELECTED) {
if (Objects.equals(level.getSelectedItem(), "自定义")) {
String s = null;
while (true) {
s = JOptionPane.showInputDialog(null, "自定义速度(移动一次花费的时间)", speed);
if (s == null) {
level.setSelectedIndex(lastChoose);
break;
} else if (s.matches("\\d+") && Integer.parseInt(s) > 0) {
speed = Integer.parseInt(s);
break;
}else{
JOptionPane.showMessageDialog(null,"请输入大于0的数字!","",JOptionPane.ERROR_MESSAGE);
}
}
} else {
lastChoose = level.getSelectedIndex();
}
}
}
private void levelPropertyChange(PropertyChangeEvent e) {
// TODO add your code here
}
public JPanel getGame() {
return game;
}
public JButton getStart() {
return start;
}
public JLabel getNowScore() {
return nowScore;
}
public JLabel getMaxScore() {
return maxScore;
}
private void startActionPerformed(ActionEvent e) {
int speed = 0;
if(level.getSelectedIndex() == 0){
speed = Snake.NORMAL_SPEED;
}else if(level.getSelectedIndex() == 1){
speed = Snake.A_BIT_FAST_SPEED;
}else if(level.getSelectedIndex() == 2){
speed = Snake.FAST_SPEED;
}else if(level.getSelectedIndex() == 3){
speed = Snake.VERY_FAST_SPEED;
}else if(level.getSelectedIndex() == 4){
speed = Snake.A_BIT_SLOW_SPEED;
}else if(level.getSelectedIndex() == 5){
speed = Snake.SLOW_SPEED;
}else if(level.getSelectedIndex() == 6){
speed = Snake.VERY_SLOW_SPEED;
}else if(level.getSelectedIndex() == 7){
speed = this.speed;
}
// TODO add your code here
if(Snake.isStart){
if(Snake.isStop){
Snake.toContinue();
start.setText("暂停");
Snake.isStop = false;
}else{
Snake.stop();
start.setText("继续");
Snake.isStop = true;
}
}else{
Snake.start(speed);
start.setText("暂停");
Snake.isStart = true;
Snake.isStop = false;
}
}
private void gameKeyPressed(KeyEvent e) {
// TODO add your code here
int code = e.getKeyCode();
if(code == 37 || code == 65){
//左
if(Snake.direction != Snake.EAST){
Snake.direction = Snake.WEST;
}
}else if(code == 38 || code==87){
//上
if(Snake.direction != Snake.SOUTH){
Snake.direction = Snake.NORTH;
}
}else if(code == 39 || code==68){
//右
if(Snake.direction != Snake.WEST){
Snake.direction = Snake.EAST;
}
}else if(code == 40 || code == 83){
//下
if(Snake.direction != Snake.NORTH){
Snake.direction = Snake.SOUTH;
}
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
control = new JPanel();
level = new JComboBox<>();
label1 = new JLabel();
start = new JButton();
label2 = new JLabel();
label3 = new JLabel();
nowScore = new JLabel();
maxScore = new JLabel();
game = new JPanel();
//======== this ========
setTitle("\u8d2a\u5403\u86c7");
setResizable(false);
setBackground(Color.black);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setForeground(new Color(0, 0, 2));
var contentPane = getContentPane();
contentPane.setLayout(null);
//======== control ========
{
control.setBorder(LineBorder.createBlackLineBorder());
control.setLayout(null);
//---- level ----
level.setModel(new DefaultComboBoxModel<>(new String[] {
"\u6b63\u5e38\u901f\u5ea6",
"\u6709\u70b9\u5feb",
"\u5f88\u5feb",
"\u8d85\u7ea7\u5feb",
"\u6709\u70b9\u6162",
"\u5f88\u6162",
"\u8d85\u7ea7\u6162",
"\u81ea\u5b9a\u4e49"
}));
level.setFocusable(false);
level.addItemListener(e -> levelItemStateChanged(e));
level.addPropertyChangeListener(e -> levelPropertyChange(e));
control.add(level);
level.setBounds(85, 10, 115, level.getPreferredSize().height);
//---- label1 ----
label1.setText("\u9009\u62e9\u96be\u5ea6\uff1a");
label1.setFocusable(false);
control.add(label1);
label1.setBounds(new Rectangle(new Point(15, 12), label1.getPreferredSize()));
//---- start ----
start.setText("\u5f00\u59cb");
start.setFocusable(false);
start.addActionListener(e -> startActionPerformed(e));
control.add(start);
start.setBounds(210, 10, 90, start.getPreferredSize().height);
//---- label2 ----
label2.setText("\u5f53\u524d\u5f97\u5206\uff1a");
label2.setFocusable(false);
control.add(label2);
label2.setBounds(new Rectangle(new Point(430, 15), label2.getPreferredSize()));
//---- label3 ----
label3.setText("\u6700\u9ad8\u5f97\u5206\uff1a");
label3.setFocusable(false);
control.add(label3);
label3.setBounds(new Rectangle(new Point(560, 15), label3.getPreferredSize()));
//---- nowScore ----
nowScore.setText("0");
nowScore.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", nowScore.getFont().getStyle() | Font.BOLD, nowScore.getFont().getSize() + 9));
nowScore.setForeground(new Color(0, 51, 255));
nowScore.setFocusable(false);
control.add(nowScore);
nowScore.setBounds(500, 10, 60, nowScore.getPreferredSize().height);
//---- maxScore ----
maxScore.setText("0");
maxScore.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", maxScore.getFont().getStyle() | Font.BOLD, maxScore.getFont().getSize() + 9));
maxScore.setForeground(Color.red);
maxScore.setFocusable(false);
control.add(maxScore);
maxScore.setBounds(630, 10, 60, maxScore.getPreferredSize().height);
}
contentPane.add(control);
control.setBounds(10, 10, 700, 45);
//======== game ========
{
game.setBorder(LineBorder.createBlackLineBorder());
game.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
gameKeyPressed(e);
}
});
game.setLayout(null);
}
contentPane.add(game);
game.setBounds(10, 60, 700, 700);
contentPane.setPreferredSize(new Dimension(725, 770));
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JPanel control;
private JComboBox<String> level;
private JLabel label1;
private JButton start;
private JLabel label2;
private JLabel label3;
private JLabel nowScore;
private JLabel maxScore;
private JPanel game;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
//Snake.java
package com.domain;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Snake extends JLabel {
public static final int NORMAL_SPEED = 25;
public static final int A_BIT_FAST_SPEED = 20;
public static final int FAST_SPEED = 15;
public static final int VERY_FAST_SPEED = 10;
public static final int A_BIT_SLOW_SPEED = 30;
public static final int SLOW_SPEED = 35;
public static final int VERY_SLOW_SPEED = 40;
//---------------------------------------------------------------------------
public static final Color BODY_COLOR = Color.YELLOW;
public static final Color FOOD_COLOR = Color.BLACK;
public static final Color HEAD_COLOR = Color.RED;
//--------------------------------------------------------------------------
public static final int NORTH = 0;
public static final int SOUTH = 1;
public static final int WEST = 2;
public static final int EAST = 3;
//---------------------------------------------------------------------------
public static boolean isStart = false;//是否已经开始游戏
public static boolean isStop = false; //如果isStart为true,则isStop代表是不是在暂停
public static int width; //游戏区域的宽度(在Main类中赋值)
public static int height; //高度
public static int nowScore = 0; //当前分数
public static int maxScore = 0; //最高分数
public static Snake food = null; //食物
public static Snake head = null; //头
public static Snake trail = null; //尾
public static int speed = 30; //移动速度
public static int size = 10; //边长
public static int direction = EAST; //移动方向
public static Timer timer = new Timer(speed, e -> action()); //定时调度
//---------------------------------------------------------------------------
public int x;
public int y;
public Snake next;
//----------------------------------------------------------------------------
public static Snake CREATE_FOOD() {
Snake snake = new Snake();
Random rd = new Random();
int x = rd.nextInt(width / size - 1) * size;
int y = rd.nextInt(height / size - 1) * size;
snake.x = x;
snake.y = y;
snake.setBackground(FOOD_COLOR);
Main.main.getGame().add(snake);
food = snake;
return snake;
}
public static Snake CREATE_HEAD() {
Snake snake = new Snake();
int x = (width / size - 1) / 2 * size;
int y = (height / size - 1) / 2 * size;
snake.x = x;
snake.y = y;
snake.setBackground(HEAD_COLOR);
Main.main.getGame().add(snake);
head = snake;
return snake;
}
public static Snake CREATE_BODY(Snake next) {
Snake snake = new Snake(next);
snake.setBackground(BODY_COLOR);
trail = snake;
Main.main.getGame().add(snake);
return snake;
}
//-----------------------------------------------------------------------------
/**
* 移动
*/
public static void move() {
Snake temp = trail;
while (temp != head) {
temp.x = temp.next.x;
temp.y = temp.next.y;
temp = temp.next;
}
if (direction == EAST) {
//向右
head.x += size;
} else if (direction == WEST) {
//向左
head.x -= size;
} else if (direction == NORTH) {
head.y -= size;
} else if (direction == SOUTH) {
head.y += size;
}
}
//刷新
public static void flush() {
Snake temp = Snake.trail;
while (temp != null) {
temp.setBounds(temp.x, temp.y, size, size);
temp = temp.next;
}
if (food != null)
food.setBounds(food.x, food.y, size, size);
//重绘
Main.main.getGame().repaint();
}
//Timer执行一次
public static void action() {
move();
flush();
//判断是否吃到食物
if(isEatFood()){
++nowScore;//加分
Main.main.getNowScore().setText(Integer.toString(nowScore));//更新分数到控件
//把现在的头的next变成食物
head.next = food;
head.setBackground(BODY_COLOR);
//食物变成新的head
head = food;
head.setBackground(HEAD_COLOR);
//创建新的食物
CREATE_FOOD();
return;
}
boolean isHitWall = isHitWall();
boolean isHitSelf = isHitSelf();
if (isHitWall || isHitSelf) { //TODO 判断是否自己撞自己
if (isHitWall)
JOptionPane.showMessageDialog(null, "撞墙,游戏结束,得分为" + nowScore, "Game Over", JOptionPane.PLAIN_MESSAGE);
else{
JOptionPane.showMessageDialog(null, "自己撞自己,游戏结束,得分为" + nowScore, "Game Over", JOptionPane.PLAIN_MESSAGE);
}
isStart = false;
Main.main.getStart().setText("重新开始");
if (nowScore > maxScore) {
maxScore = nowScore;
Main.main.getMaxScore().setText(Integer.toString(maxScore));
}
timer.stop();
}
}
//开始游戏
public static void start(int speed) {
Main.main.getGame().removeAll();//清除所有组件
Main.main.getGame().repaint();//重绘
food = null;
head = null;
trail = null;
direction = EAST;
Snake s1 = CREATE_HEAD();
Snake s2 = CREATE_BODY(s1);
Snake s3 = CREATE_BODY(s2);
Snake s4 = CREATE_BODY(s3);
Snake s5 = CREATE_BODY(s4);
CREATE_FOOD();
flush();
//重置分数
nowScore = 0;
Main.main.getNowScore().setText("0");
Snake.speed = speed;
timer.setDelay(speed);
timer.start();
}
//暂停游戏
public static void stop() {
timer.stop();
}
//暂停后继续
public static void toContinue() {
timer.start();
}
//是否撞墙
public static boolean isHitWall() {
return head.x >= width || head.x < 0 || head.y >= height || head.y < 0;
}
//是否撞自己
public static boolean isHitSelf() {
Snake temp = trail;
try{
while (temp .next.next.next.next!= null && temp != head) {
if (temp.x == head.x && temp.y == head.y)
return true;
temp = temp.next;
}
}catch (NullPointerException e){
return false;
}
return false;
}
//判断是否吃到食物
public static boolean isEatFood(){
if (direction == EAST) {
//向右
return food.y == head.y && food.x == head.x + size;
} else if (direction == WEST) {
//向左
return food.y == head.y && food.x == head.x - size;
} else if (direction == NORTH) {
return food.x == head.x && food.y == head.y - size;
} else if (direction == SOUTH) {
return food.x == head.x && food.y == head.y + size;
}
return false;
}
//-----------------------------------------------------------------------------
Snake() {
super();
setPreferredSize(new Dimension(size, size));
setBackground(Color.black);
setOpaque(true);
}
Snake(Snake next) {
this();
if (direction == EAST) {
//向右
this.x = next.x - size;
} else if (direction == WEST) {
//向左
this.x = next.x + size;
} else if (direction == NORTH) {
this.y = next.y - size;
} else if (direction == SOUTH) {
this.y = next.y + size;
}
this.next = next;
}
//--------------------------------------------------------------------------
}