3.贪吃蛇
3.1 StartGame类
用于创建出一个静态的窗体,然后启动游戏
package com.Snake;
import javax.swing.*;
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLocation(0,0);
frame.setSize(900,620);
frame.setResizable(false);
frame.add(new GamePanel());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
3.2 Data类
用于存放调用的图片
package com.Snake;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL headerURL=Data.class.getResource("/com/Snake/statics/header.jpg");
public static URL bodyURL=Data.class.getResource("/com/Snake/statics/body.jpg");
public static URL foodURL=Data.class.getResource("/com/Snake/statics/food.jpg");
public static URL upURL=Data.class.getResource("/com/Snake/statics/up.jpg");
public static URL downURL=Data.class.getResource("/com/Snake/statics/down.jpg");
public static URL leftURL=Data.class.getResource("/com/Snake/statics/left.jpg");
public static URL rightURL=Data.class.getResource("/com/Snake/statics/right.jpg");
public static ImageIcon header=new ImageIcon(headerURL);
public static ImageIcon body=new ImageIcon(bodyURL);
public static ImageIcon food=new ImageIcon(foodURL);
public static ImageIcon up=new ImageIcon(upURL);
public static ImageIcon down=new ImageIcon(downURL);
public static ImageIcon left=new ImageIcon(leftURL);
public static