当前位置: 首页 > news >正文

java“贪吃蛇”小游戏

基于java实现贪吃蛇小游戏,主要通过绘制不同的图片并以一定速度一帧一帧地在窗体上进行展示。

我是在javaSwing项目下创建了一个包 名字叫做:Snakes包 包下有一个启动类和一个设置代码的主界面两个类

代码主界面:

代码主界面主要讲解的是  注册蛇和蛇的身体 还有主要框架 还有蛇吃的豆子 等等

然后是编辑贪吃蛇游戏的主框架  然后再是绘制事物的坐标位置  使用if判断语句来判断是否存在豆子  然后if else语句来判断如果豆子不存在的话就随机生成一个豆子  然后吃到了豆子 就会增加身体的长度  当然也要判断游戏 是否结束  也还是使用if判断语句 判断是否蛇头设否与蛇尾重合  重合就将游戏结束  代码如下所示:

	package Snakes;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class SnakeJPanel extends JPanel implements ActionListener{private boolean start;//当前游戏状态private int speed;//速度private boolean exist;//当前是否存在食物private int foodType;//食物种类private int x;//豆子的横坐标private int y;//豆子的纵坐标private ArrayList<int[]> localList;//蛇public String direction;//方向private String direction2;//引导方向public boolean flag;Random rand = new Random();private ImageIcon up;private ImageIcon down;private ImageIcon right;private ImageIcon left;private ImageIcon body;private ImageIcon food;private ImageIcon title;Timer time;private int score;//当前得分情况private int num;//吃到的食物个数
//    private Image offScreenImage;  //图形缓存//图片绘制@Overridepublic void paint(Graphics g) {direction = direction2;g.setColor(Color.WHITE);g.fillRect(0, 0, 900, 700);//绘制游戏框//标题框g.drawRect(25, 30, 800, 75);title.paintIcon(this, g, 25, 10);//内容框g.setColor(Color.black);g.fillRect(25, 75, 850, 600);//绘制食物的坐标位置if(!exist) {//如果当前不存在豆子,随机绘制一个豆子	if(num % 5 == 0) {foodType = 1;}else {foodType = 0;}boolean isProduce = true;while(isProduce) {isProduce = false;x = rand.nextInt(33) * 25 + 25;		y = rand.nextInt(23) * 25 + 75;			for (int[] arr:localList) {if(x == arr[0] && y == arr[1]) {	isProduce = true;break;	}}}			System.out.println(x + "---" + y);}if(eat()) {exist = false;}else {exist = true;}if(foodType == 0) {//绘制食物g.setColor(Color.blue);
//			g.fillRect(x, y, 25, 25);g.drawImage(food.getImage(),x, y, 25, 25,null);}else {//绘制食物g.setColor(Color.WHITE);g.fillRect(x, y, 25, 25);
//			g.drawImage(food.getImage(),x, y, 25, 25,null);}//绘制头g.setColor(Color.red);
//		g.fillRect(localList.get(0)[0], localList.get(0)[1], 25, 25);	ImageIcon head = null;//判断当前方向if(direction.equals("R")) {head = right;}else if(direction.equals("L")) {head = left;}else if(direction.equals("U")) {head = up;}else if(direction.equals("D")) {head = down;}		g.drawImage(head.getImage(), localList.get(0)[0], localList.get(0)[1], 25, 25,null);head.paintIcon(this, g,localList.get(0)[0], localList.get(0)[1]);//绘制身体g.setColor(Color.white);for (int i = 1; i < localList.size(); i++) {
//			g.fillRect(localList.get(i)[0], localList.get(i)[1], 25, 25);
//			g.drawImage(body.getImage(), localList.get(i)[0], localList.get(i)[1], 25, 25,null);body.paintIcon(this, g, localList.get(i)[0], localList.get(i)[1]);}
//		g.fillRect(localList.get(1)[0], localList.get(1)[1], 25, 25);
//		g.fillRect(localList.get(2)[0], localList.get(2)[1], 25, 25);//绘制分数和长度//长度g.setColor(Color.GREEN);g.setFont(new Font("宋体", Font.BOLD, 18));g.drawString("长度:" + (localList.size() - 1), 25, 30);//分数g.drawString("分数:" + score, 25, 48);if(!start) {//如果游戏未启动,结束移动和重绘g.setColor(Color.white);g.setFont(new Font("宋体", Font.BOLD, 30));g.drawString("暂停/开始(请按任意键开始,空格键暂停)", 150, 300);time.stop();}else {time.start();}//		speed();//移动后进行下一次绘制		
//      move();//移动
//		repaint();//重新绘制		}//	//解决闪烁问题
//	//如果为JFrame 为重量级  程序不会调用update()方法
//	//如果为Frame 为轻量级  重写update()方法 做双缓冲
//	//如果为JPanel 不会闪烁
//	  @Override
//	    public void update(Graphics g)
//	    {
//	    	System.out.println("update");
//	           if(offScreenImage == null)
//	              offScreenImage = this.createImage(900, 700);  //新建一个图像缓存空间,这里图像大小为800*600
//	              Graphics gImage = offScreenImage.getGraphics();  //把它的画笔拿过来,给gImage保存着
//	              paint(gImage);                                   //将要画的东西画到图像缓存空间去
//	              g.drawImage(offScreenImage, 0, 0, null);         //然后一次性显示出来
//	    }@Overridepublic void actionPerformed(ActionEvent e) {	    //移动后进行下一次绘制		move();//移动repaint();//重新绘制		}/*** 绘制速度*/
//	private void speed() {
//		try {//按一定速度进行移动
//			Thread.sleep(speed);//控制移动速度
//		} catch (InterruptedException e) {
//			// TODO 自动生成的 catch 块
//			e.printStackTrace();
//		}
//	}/*** 初始化图片*/private void drawImage() {up = new ImageIcon("D:\\学习资料\\Java\\images\\up.png");down = new ImageIcon("D:\\学习资料\\Java\\images\\down.png");right = new ImageIcon("D:\\学习资料\\Java\\images\\right.png");left = new ImageIcon("D:\\学习资料\\Java\\images\\left.png");body = new ImageIcon("D:\\学习资料\\Java\\images\\body.png");food = new ImageIcon("D:\\学习资料\\Java\\images\\food.png");title = new ImageIcon("D:\\学习资料\\Java\\images\\title.jpg");}private boolean eat() {if(localList.get(0)[0] == x && localList.get(0)[1] == y) {//如果当前蛇头吃到了豆子System.out.println("eat");num++;if(foodType == 0) {score += 10;}else {score += (rand.nextInt(5) * 10 + 10);}int last = localList.size() - 1;//蛇尾			//在蛇尾后面添加一节身体localList.add(new int[] {localList.get(last)[0],localList.get(last)[1]});return true;}return false;}//移动方法public void move() {//判断是否游戏结束if(isbody()) {System.out.println("game over");start = false;//结束游戏移动JOptionPane.showMessageDialog(null,"游戏已结束!");time.stop();init();		}if(flag && localList != null) {//如果长度不为空且游戏未结束				int last = localList.size() - 1;//记录蛇尾for (int i = last; i > 0; i--) {//从蛇尾开始,每节身体移动到前一节身体的位置上localList.set(i,new int[] {localList.get(i - 1)[0],localList.get(i - 1)[1]});}//记录头位置int[] local = localList.get(0);//判断当前方向,并进行模拟移动,判断是否与边界重合if(direction.equals("R")) {if(local[0] >= 850) {local[0] = 25;}else {local[0] += 25;}}else if(direction.equals("L")) {if(local[0] <= 25) {local[0] = 850;}else {local[0] -= 25;}}else if(direction.equals("U")) {if(local[1] <= 75) {local[1] = 650;}else {local[1] -= 25;}}else if(direction.equals("D")) {if(local[1] >= 650) {local[1] = 75;}else {local[1] += 25;}}			//更改头的位置localList.set(0, local);		}	}//判断下一步是否为蛇身private boolean isbody() {// TODO 自动生成的方法存根//记录头位置int x = localList.get(0)[0];int y = localList.get(0)[1];//判断当前方向,并进行模拟移动,判断是否与边界重合if(direction.equals("R")) {x += 25;}else if(direction.equals("L")) {x -= 25;}else if(direction.equals("U")) {y -= 25;}else if(direction.equals("D")) {y += 25;}			for (int i = 1; i < localList.size(); i++) {if(localList.get(i)[0] == x && localList.get(i)[1] == y) {return true;}}return false;}//	//判断下一步是否为边界
//	private boolean isborder() {
//		// TODO 自动生成的方法存根
//		//记录头位置
//		// TODO 自动生成的方法存根
//		//记录头位置
//		int x = localList.get(0)[0];
//		int y = localList.get(0)[1];
//
//		//判断当前方向,并进行模拟移动,判断是否与边界重合
//		if(direction.equals("R")) {
//			x += 25;
//		}else if(direction.equals("L")) {
//			x -= 25;
//		}else if(direction.equals("U")) {
//			y -= 25;
//		}else if(direction.equals("D")) {
//			y += 25;
//		}	
//				
//		if(x < 25 || x > (33 * 25 + 25)) {
//			return true;//当x坐标超出边界,则返回true
//		}
//		if(y < 105 || y > (23 * 25 + 105)) {
//			return true;//当y坐标超出边界,则返回true
//		}
//		return false;//蛇头移动后未超出边界,返回false
//		
//	}/*** Create the frame.*/public SnakeJPanel(int speed) {this.speed = speed; //初始化速度//初始化游戏面板的基本信息this.setSize(900, 700);this.setLocation(0, 30);this.setFocusable(true);init();//初始化界面drawImage();//绘制图片moveByKey();//给界面添加一个键盘监听}/** 键盘监听* 通过键盘输入上下左右来控制当前蛇头移动的方向* 先判断当前蛇头方向,再来改变引导方向* 当进行绘制时再修改蛇的方向* 保证不会因为在短时间内快速变换方向导致蛇头逆向转向*/private void moveByKey() {addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {int key = e.getKeyCode();//边界值判断switch(key) {case 65:case 37:{//向左走if(!direction.equals("R")) {direction2 = "L";}break;}				case 87:case 38:{//向上走if(!direction.equals("D")) {direction2 = "U";}				break;}				case 68:case 39:{//向右走if(!direction.equals("L")) {direction2 = "R";}break;}case 83:case 40:{//向下走if(!direction.equals("U")) {direction2 = "D";}					break;}case KeyEvent.VK_SPACE:{//如果当前键盘输入为空格start = !start;//调整游戏状态System.out.println("暂停/开始");repaint();//重绘}}//任意键开始if(!start && key != KeyEvent.VK_SPACE) {//如果当前状态为暂停状态,且键盘输入不是空格start = true;repaint();//重绘}				}});}/*** 初始化游戏基本信息*/private void init() {start = false;exist = true;direction2 = "U";flag = true;localList = new ArrayList<int[]>();localList.add(0,new int[] {75,125});//蛇头localList.add(1,new int[] {75,150});//蛇身1localList.add(2,new int[] {75,175});//蛇身2//创建第一个食物的位置//通过循环保证当前生成的食物不在身体所在的坐标上boolean isProduce = true;while(isProduce) {//循环生成食物坐标isProduce = false;//结束本次循环x = rand.nextInt(33) * 25 + 25;		y = rand.nextInt(23) * 25 + 75;			for (int[] arr:localList) {//循环遍历蛇头及蛇身的坐标if(x == arr[0] && y == arr[1]) {//如果食物坐标和蛇的某一节坐标重合isProduce = true;//跳转循环状态,继续下一次食物生成break;	}}//蛇身遍历完成,没有重合坐标,结束食物坐标生成					}time = new Timer(speed, this);setLayout(null);score = 0;num = 0;foodType = 0;repaint();	}
}

启动类:

package Snakes;
import javax.swing.JFrame;
import javax.swing.JOptionPane;public class SnakeStart {public static void main(String[] args) {int speed = 0;String showInputDialog = null;//初始化时间//得到速度while(true) {showInputDialog = JOptionPane.showInputDialog("蛇移动速度(1 - 5)","3");if(showInputDialog == null) {showInputDialog = "3";//默认速度break;}if(showInputDialog.length() > 1) {continue;}char[] a = showInputDialog.toCharArray();if(a[0] >= '1' && a[0] <= '5') {break;}}speed = Integer.parseInt(showInputDialog) * 50;SnakeJPanel snakeJPanel = new SnakeJPanel(speed);//创建一个JFrame窗口,将游戏面板添加进行窗口中JFrame jFrame = new JFrame();//设置窗口的某些属性jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jFrame.setSize(920, 750);jFrame.add(snakeJPanel);jFrame.setLocationRelativeTo(null);jFrame.setVisible(true);}}

运行结果如下所示:

 

http://www.lryc.cn/news/232729.html

相关文章:

  • 【面试经典150 | 位运算】数字范围按位与
  • 推介会如何做好媒体宣传
  • 【ROS导航Navigation】五 | 导航相关的消息 | 地图 | 里程计 | 坐标变换 | 定位 | 目标点和路径规划 | 激光雷达 | 相机
  • 什么是脏读、不可重复读、幻读讲解
  • 2018年五一杯数学建模C题江苏省本科教育质量综合评价解题全过程文档及程序
  • 第四代智能井盖传感器:万宾科技助力城市安全
  • [Jenkins] Docker 安装Jenkins及迁移流程
  • 第七篇 基于JSP 技术的网上购书系统——新品上架、推荐产品、在线留言、搜索功能实现(网上商城、仿淘宝、当当、亚马逊)
  • IntelliJ IDE 插件开发 |(一)快速入门
  • 【Ubuntu】Windows远程Ubuntu系统
  • pipeline jenkins流水线
  • 软件工程理论与实践 (吕云翔) 第六章 面向对象分析课后习题及其解析
  • langchain(1):使用LangChain 调用 openai 的 text/chat model
  • rabbitMQ的扇出模式(fanout发布订阅)的生产者与消费者使用案例
  • VSCode打开Json 文件格式化
  • 【C++】:STL——标准模板库介绍 || string类
  • Python小白之PyCharm仍然显示“No module named ‘xlwings‘”
  • 在Uni-app中实现计时器效果
  • Linux脚本shell中将Windos格式字符转换为unix
  • 【分布式】MIT 6.824 Lab 2B实现细节分析
  • MySql 数据库初始化,创建用户,创建数据库,授权
  • 【洛谷算法题】P5712-Apples【入门2分支结构】
  • vue项目中的js文件使用vuex
  • 【Vue3】 computed 完整写法 全选反选 、计算商品总价
  • Mindomo Desktop for Mac(免费思维导图软件)下载
  • Spark资源规划-资源上线评估
  • RT-Thread STM32F407 定时器
  • C#asp.net考试系统+sqlserver
  • mac上配置maven
  • 解决vue-cli node-sass安装不成功问题