欢迎光临
我们一直在努力

JAVA贪吃蛇小游戏源代码系列

欢迎关注公众号:

JAVA贪吃蛇小游戏源代码系列

获取贪吃蛇小游戏的源代码。

贪吃蛇小游戏运行结果如下:
启动界面:
JAVA贪吃蛇小游戏源代码系列
运行界面:
JAVA贪吃蛇小游戏源代码系列
重启界面:
JAVA贪吃蛇小游戏源代码系列
源代码框架如下:
JAVA贪吃蛇小游戏源代码系列

注:在运行程序的时候,得重新设计窗体的大小,以适合自己的电脑,其次,图片类和音乐类都保存在我自己电脑的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); //设置透明与否 } /*设置按钮的监听器事件 * 进入按钮的监听器事件的主要功能是当点击按钮以后,程序关掉启动界面,并转入运行界面。 * 主要实现原理是定义一个新界面的类,作为运行界面,然后定义一个关掉启动界面的方法,然后在监听器事件中, * 调用关掉界面的方法,实例化运行界面 */ @Override public 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; } } 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《JAVA贪吃蛇小游戏源代码系列》
文章链接:https://www.456zj.com/37441.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址