欢迎关注公众号:
获取贪吃蛇小游戏的源代码。
贪吃蛇小游戏运行结果如下:
启动界面:
运行界面:
重启界面:
源代码框架如下:
注:在运行程序的时候,得重新设计窗体的大小,以适合自己的电脑,其次,图片类和音乐类都保存在我自己电脑的F盘的相应路径下,在运行程序的时候需要将图片类和音乐类保存到自己的本地磁盘路径中,然后在程序中改变路径。
package snakeGame;/** @project project* @author liyongping* @creed: just do it* @ date 2021/12/21 17:43* @ version 1.0*///设置类,定义游戏中的各个参数,public class Setting {//图片路径String background="src/snakeGame/image/timg.jpg";String restartPicture="src/snakeGame/image/restartPicture.jpg";String background1="src/snakeGame/image/background1.jpg";String up="src/snakeGame/image/up.png";String down="src/snakeGame/image/down.png";String left="src/snakeGame/image/left.png";String right="src/snakeGame/image/right.png";String food="src/snakeGame/image/food.png";String body="src/snakeGame/image/body.png";//音乐路径String applauseMusic="src/snakeGame/music/applauseMusic.wav";String eatFoodMusic="src/snakeGame/music/eatFoodMusic.wav";String DeadMusic="src/snakeGame/music/DeadMusic.wav";String pushButtonMusic="src/snakeGame/music/pushButtonMusic.wav";String encouragMusic="src/snakeGame/music/encouragMusic.wav";String backgroundMusic="src/snakeGame/music/backgroundMusic.wav";}
package snakeGame;/* Test类的主要任务是设计程序运行后的界面,包括 程序启动的界面和游戏运行界面。* 程序启动的界面包括背景图片和进入运行界面的Button,点击按钮之后程序关闭启动界面进入到运行界面,* 运行界面设置在SnakeGame类中,Test类大体设置了运行界面的大小可见与否等。*/import com.sun.deploy.panel.RuleSetViewerDialog;import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class Start extends JFrame implements ActionListener {public static RuleSetViewerDialog frame1;static JFrame frame = new JFrame( );Setting setting =new Setting();public Start(){ //设置启动界面addFrame();//添加框架AddButton();//添加按钮AddPicture();//添加图片}public void addFrame(){frame.setUndecorated(true); //用于取消边框背景frame.setLayout (null);frame.setSize(1000,600);//定义游戏边框大小frame.setLocation(100, 200);//定义游戏边框位置frame.setLocationRelativeTo (null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}//定义进入游戏按钮public void AddButton() {RButton enterButton =new RButton("进入游戏");enterButton.setFont(new Font("华文行楷", Font.BOLD, 35));//定义字体enterButton.setForeground(Color.red);enterButton.setBounds (450, 450 , 200, 100);enterButton.setBackground(Color.white);frame.add(enterButton);enterButton.addActionListener(this);//添加按键响应事件}//加入背景图片public void AddPicture() {ImageIcon img = new ImageIcon(setting.background);JLabel Label= new JLabel(img);Label.setBounds(0,0,1000,600); //设置大小frame.getLayeredPane().add(Label,new Integer(Integer.MIN_VALUE)); //设置图片底层和按钮在容器中的顺序JPanel jp =(JPanel)frame.getContentPane();jp.setOpaque(false); //设置透明与否}/*设置按钮的监听器事件* 进入按钮的监听器事件的主要功能是当点击按钮以后,程序关掉启动界面,并转入运行界面。* 主要实现原理是定义一个新界面的类,作为运行界面,然后定义一个关掉启动界面的方法,然后在监听器事件中,* 调用关掉界面的方法,实例化运行界面*/@Overridepublic void actionPerformed(ActionEvent e) {new music(setting.pushButtonMusic);// TODO 自动生成的方法存根closeThis(); //关掉当前界面new snakeGameFrame ();//实例化运行界面}private void closeThis() {// TODO 自动生成的方法存根frame.dispose();frame.setVisible(false);}
}
package snakeGame;
/** 定义一个类,用来描述贪吃蛇游戏中的蛇,蛇身上的每一个点,通过建立snakeNode的对象,指定不同的X轴和Y轴的值,就能组成一个蛇身。* 同时可以获得蛇身上的x和y点坐标,和颜色*/
import java.awt.Color;public class SnakeNode { //定义蛇身集合中的各个元素点,x,y。以及颜色三个蛇的关键组成private int x;private int y;private Color color;//定义颜色public int setX=20;//定义蛇身每个点的大小public int setY=20;//定义蛇身每个点的大小public SnakeNode() {super();}public SnakeNode(int x, int y, Color color) {super();this.x = x;this.y = y;this.color = color;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public Color getColor() {return color;}public void setColor(Color color) {this.color = color;}
}