Java贪吃蛇游戏(简单粗暴版)
Java编程贪吃蛇小游戏(简单粗暴版)
2021/7/5 15:15:40
这是自己大一时通过老师讲的Java课程编写的第一个小游戏,从构思到游戏的实现耗时一天,虽然时间不长,但确实从里面学到很多东西,对我的Java编程能力和编程思想是一个很好的提升。
(之所以称为简单粗暴版,是因为里面部分功能没有实现,只是简单的实现功能效果,并没有小蛇死亡和吃食物计数功能,仅供初学者参考吧!)
游戏的主类(启动类)
import javax.swing.*; //游戏的主启动类 public class StartGame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setBounds(10,10,900,720); frame.setTitle("新科贪吃蛇"); //设置窗口大小不可变 frame.setResizable(false); /* 游戏页面写在面板上 */ //将面板添加到窗体中 frame.add(new GamePanel()); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
游戏的功能实现类
游戏的数据存储类
import javax.swing.*; import java.net.URL; /* 数据中心 */ public class Data { //数据头,头部文件 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 URL downURL= Data.class.getResource("statics\down.png"); public static URL leftURL= Data.class.getResource("statics\left.png"); public static URL rightURL= Data.class.getResource("statics\right.png"); 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 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 static URL foodURL= Data.class.getResource("statics\food.png"); public static ImageIcon food = new ImageIcon(foodURL); }