贪吃蛇小游戏所需要的图片:
package com.massimo.snake;
import javax.swing.*;
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);
}
}
2.2、GamePanel类
package com.massimo.snake;
import javax.swing.*;
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);
}
}
2.3、Data类
package com.massimo.snake;
import javax.swing.*;
import java.net.URL;
//数据中心
public class Data {
//相对路径
//绝对路径 / 相当于当前项目
public static URL headerURL = Data.class.getResource("statics/header.png");
public static URL rightURL = Data.class.getResource("statics/right.png");
public static URL bodyURL = Data.class.getResource("statics/body.png");
public static URL downURL = Data.class.getResource("statics/down.png");
public static URL foodURL = Data.class.getResource("statics/food.png");
public static URL leftURL = Data.class.getResource("statics/left.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static ImageIcon body = new ImageIcon(bodyURL);
public static ImageIcon right = new ImageIcon(rightURL);
public static ImageIcon down = new ImageIcon(downURL);
public static ImageIcon food = new ImageIcon(foodURL);
public static ImageIcon left = new ImageIcon(leftURL);
public static URL upURL = Data.class.getResource("statics/up.png");
public static ImageIcon up = new ImageIcon(upURL);
}
三、项目结构
四、运行效果



