JAVA---GUI---贪吃蛇
写代码的思路
- 定义数据
- 画上去
- 监听事件
- 键盘监听
- 事件监听(定时器刷新)
源码
主程序入口
//游戏的主启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,720);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//正常的游戏应该放在面板上
frame.add(new GamePanel());
frame.setVisible(true);
}
}
读取数据图片的类
public class Data {
//相对路径 header.png
//绝对路径 / 相当于当前的项目
public static URL headerURL = Data.class.getResource("statics/header.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static URL upURL = Data.class.getResource("statics/up.png");
public static ImageIcon up = new ImageIcon(upURL);
public static URL downURL = Data.class.getResource("statics/down.png");
public static ImageIcon down = new ImageIcon(downURL);
public static URL leftURL = Data.class.getResource("statics/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL rightURL = Data.class.getResource("statics/right.png");
public static ImageIcon right = new ImageIcon(rightURL);
public static URL bodyURL = Data.class.getResource("statics/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Data.class.getResource("statics/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
}
游戏面板类
先定义数据结构,然后用画笔画上去,最后事件监听
//游戏的面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//定吃蛇的数据结构
int length; //蛇的长度
int[] sankeX = new int[600];//蛇的X坐标 25*25
int[] sankeY = new int[500];//蛇的Y坐标 25*25
String fx; //方向
//食物的坐标
int foodx;
int foody;
Random random = new Random();
//成绩
int score;
//游戏开始状态:开始,停止
boolean isStart = false;//默认没开始
//游戏默认不失败
boolean isFail = false;
//定时器
Timer timer = new Timer(100,this);//100ms执行一次
//构造器
public GamePanel() {
init();
this.setFocusable(true);//获得焦点事件
this.addKeyListener(this);//获得键盘监听
timer.start();//游戏一开始就启动
}
//初始化
public void init() {
length = 3;
score = 0;
sankeX[0] = 100; sankeY[0] = 100; //头的坐标
sankeX[1] = 75; sankeY[1] = 100; //第一个身体的坐标
sankeX[2] = 50; sankeY[2] = 100; //第二个身体的坐标
fx = "R";
//把食物随机分配在界面上
foodx = 25 + 25*random.nextInt(34);
foody = 75 + 25*random.nextInt(24);
}
//绘制面板,游戏中所有东西都是用这个画笔来画
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);//清屏
this.setBackground(Color.WHITE);
//绘制静态面板
Data.header.paintIcon(this,g,25,11); //头部广告栏画上去
g.fillRect(25,75,850,600); //默认游戏界面
//画积分
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度"+length,750,35);
g.drawString("分数"+score,750,50);
//画食物
Data.food.paintIcon(this,g,foodx,foody);
//把小蛇画上去
if (fx.equals("R")) {
Data.right.paintIcon(this,g,sankeX[0],sankeY[0]);
}else if (fx.equals("L")) {
Data.left.paintIcon(this, g, sankeX[0], sankeY[0]);
}else if (fx.equals("U")) {
Data.up.paintIcon(this, g, sankeX[0], sankeY[0]);
}else if (fx.equals("D")) {
Data.down.paintIcon(this, g, sankeX[0], sankeY[0]);
}
for (int i = 1; i < length ; i++) {
Data.body.paintIcon(this,g,sankeX[i],sankeY[i]);
}
//游戏状态
if (isStart == false) {
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏",300,300);
}
//如果游戏失败
if (isFail){
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("失败!按下空格重新开始游戏",300,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_LEFT){
if(fx.equals("R")){
fx = "R";
}else{
fx = "L";
}
} else if (keyCode == KeyEvent.VK_RIGHT){
if(fx.equals("L")){
fx = "L";
}else{
fx = "R";
}
}if(keyCode == KeyEvent.VK_UP){
if(fx.equals("D")){
fx = "D";
}else{
fx = "U";
}
} else if (keyCode == KeyEvent.VK_DOWN){
if(fx.equals("U")){
fx = "U";
}else{
fx = "D";
}
}
}
//事件监听---需要通过固定事件来刷新,1s=10次 定时器
@Override
public void actionPerformed(ActionEvent e) {
if (isStart && isFail == false){//如果游戏开始,让小蛇动起来
//吃食物
if (sankeX[0] == foodx && sankeY[0] == foody){
length++;
score = score + 10;
//再次随机食物
foodx = 25 + 25*random.nextInt(34);
foody = 75 + 25*random.nextInt(24);
}
//身体移动
for (int i = length - 1; i > 0; i--) {
sankeX[i] = sankeX[i-1];
sankeY[i] = sankeY[i-1];
}
//走向
if (fx.equals("R")){
sankeX[0] = sankeX[0] + 25;
if (sankeX[0] > 850){ sankeX[0] = 25;} //边界判断
}else if (fx.equals("L")){
sankeX[0] = sankeX[0] - 25;
if (sankeX[0] < 25){ sankeX[0] = 850;} //边界判断
}else if (fx.equals("U")){
sankeY[0] = sankeY[0] - 25;
if (sankeY[0] < 75){ sankeY[0] = 650;} //边界判断
}else if (fx.equals("D")){
sankeY[0] = sankeY[0] + 25;
if (sankeY[0] > 650){ sankeY[0] = 75;} //边界判断
}
//判断失败,撞到自己,边界
for (int i = 1; i < length; i++){
if (sankeX[0] == sankeX[i] && sankeY[0]== sankeY[i]){
isFail = true;
}
}
repaint();
}
timer.start();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}