在总结之前,先给出之前的贪吃蛇小游戏全代码。
package com.duo.snake;
import javax.swing.*;
//游戏的主启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new GamePanel()); //此行代码的位置会影响后续按下空格时游戏的反应(Tab键问题)!!!
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle("贪吃蛇-2023");
frame.setBounds(5, 10, 915, 740);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
package com.duo.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
//游戏的面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//定义蛇的数据结构
int length; //小蛇总长
int[] snakeX = new int[600]; //蛇的X坐标 25*25
int[] snakeY = new int[500]; //蛇的Y坐标 25*25
String direction; //蛇头方向
boolean isStart = false; //默认未开始游戏
boolean isFail = false; //游戏失败状态
Timer timer = new Timer(100, this); //定时器,100ms执行一次
//食物的坐标
int foodX;
int foodY;
Random random = new Random();
//积分面板数据结构
int score;
//构造器
public GamePanel() {
init();
this.setFocusable(true); //获取焦点事件
this.addKeyListener(this); //获取键盘监听事件
timer.start(); //游戏一开始,定时器就启动
}
//初始化方法
public void init() {
length = 3;
snakeX[0] = 100; snakeY[0] = 100; //脑袋的坐标
snakeX[1] = 75; snakeY[1] = 100; //第一节身体的坐标
snakeX[2] = 50; snakeY[2] = 100; //第二节身体的坐标
direction = "R"; //蛇头初始向右
//将食物随机分布于游戏界面
foodX = 25 + 25 * random.nextInt(34);
foodY = 75 + 25 * random.nextInt(24);
score = 0; //定义初始得分
}
//绘制静态面板
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //起到清屏的作用
Data.header.paintIcon(this, g, 25, 11); //将广告栏画入面板
g.fillRect(25, 75, 850, 600); //默认的游戏界面(黑色区域)
this.setBackground(Color.white);
//画出积分面板
g.setColor(Color.white);
g.setFont(new Font("宋体", Font.BOLD, 18));
g.drawString("长度 " + length, 750, 30);
g.drawString("得分 " + score, 750, 55);
//画出食物
Data.food.paintIcon(this, g, foodX, foodY);
//将小蛇(头)画入面板中的游戏区域,且蛇头部分需要判断方向
switch (direction) {
case "R":
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "L":
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "U":
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "D":
Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
}
//画小蛇的其余身体部分
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);
}
//绘制当前游戏状态
if (!isStart) {
g.setColor(Color.white);
g.setFont(new Font("宋体", Font.BOLD, 40));
g.drawString("按下空格开始游戏", 270, 300);
}
if (isFail) {
g.setColor(Color.red);
g.setFont(new Font("宋体", Font.BOLD, 40));
g.drawString("游戏失败,按下空格重新开始", 220, 300);
}
}
//键盘监听事件
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode(); //获取键盘按键
if (keyCode == KeyEvent.VK_SPACE) {
if (isFail) { //重新开始
isFail = false;
init();
} else { //正常开始
isStart = !isStart;
repaint();
}
}
//键盘监听到按键后画上对应方向的蛇头
if (keyCode == KeyEvent.VK_UP && !direction.equals("D")) {
direction = "U";
} else if (keyCode == KeyEvent.VK_DOWN && !direction.equals("U")) {
direction = "D";
} else if (keyCode == KeyEvent.VK_LEFT && !direction.equals("R")) {
direction = "L";
} else if (keyCode == KeyEvent.VK_RIGHT && !direction.equals("L")) {
direction = "R";
}
}
//事件监听需要定时器来实现,即通过固定的时间进行刷新(例如1s=10次)
@Override
public void actionPerformed(ActionEvent e) {
if (isStart && !isFail) {
//小蛇身体右移
for (int i = length - 1; i > 0; i--) { //后一节身体移动至前一节 snakeX[1] = snakeX[0]
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
//判断蛇头是否吃到食物
if (snakeX[0] == foodX && snakeY[0] == foodY) {
length++;
foodX = 25 + 25 * random.nextInt(34);
foodY = 75 + 25 * random.nextInt(24);
score += 10;
}
//判断蛇头走向
switch (direction) {
case "R":
snakeX[0] += 25;
//边界判断
if (snakeX[0] > 850) {
snakeX[0] = 25;
}
break;
case "L":
snakeX[0] -= 25;
//边界判断
if (snakeX[0] < 25) {
snakeX[0] = 850;
}
break;
case "U":
snakeY[0] -= 25;
//边界判断
if (snakeY[0] < 75) {
snakeY[0] = 650;
}
break;
case "D":
snakeY[0] += 25;
//边界判断
if (snakeY[0] > 650) {
snakeY[0] = 75;
}
break;
}
//游戏失败判定
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
isFail = true;
break;
}
}
repaint();
}
timer.start();
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}
游戏的数据中心类:
package com.duo.snake;
import javax.swing.*;
import java.net.URL;
//数据中心
public class Data {
//相对路径 tx.jpg
//绝对路径 /:相当于当前的项目
public static URL headerURL = Data.class.getResource("static/header.png");
public static URL upURL = Data.class.getResource("static/up.png");
public static URL downURL = Data.class.getResource("static/down.png");
public static URL leftURL = Data.class.getResource("static/left.png");
public static URL rightURL = Data.class.getResource("static/right.png");
public static URL bodyURL = Data.class.getResource("static/body.png");
public static URL foodURL = Data.class.getResource("static/food.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static ImageIcon up = new ImageIcon(upURL);
public static ImageIcon down = new ImageIcon(downURL);
public static ImageIcon left = new ImageIcon(leftURL);
public static ImageIcon right = new ImageIcon(rightURL);
public static ImageIcon body = new ImageIcon(bodyURL);
public static ImageIcon food = new ImageIcon(foodURL);
}
游戏运行结果已于前节展示,可实现小蛇移动、吃到食物长大并且分数增加、失败判定等操作。下面将对此阶段GUI编程作一简单总结。
GUI编程: