贪吃蛇代码

news2024/9/19 6:59:43

一.准备

1.新建项目

2.放进照片

3.创建两个包放置图片类和入口类

二,游戏界面

package com.snake.view;
 
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;  //图形缓存
	
	
	//图片绘制
	@Override
	public 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);         //然后一次性显示出来
//	    }
	
 
	@Override
	public 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("images/up.png");
		down = new ImageIcon("images/down.png");
		right = new ImageIcon("images/right.png");
		left = new ImageIcon("images/left.png");
		body = new ImageIcon("images/body.png");
		food = new ImageIcon("images/food.png");
		title = new ImageIcon("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() {
			@Override
			public 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});//蛇身1
		localList.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();
		
	}
 
 
}

2.启动

package com.snake.controller;
 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
 
import com.snake.view.SnakeJPanel;
 
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.coloradmin.cn/o/1231478.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

前缀和(c++,超详细,含二维)

前缀和与差分 当给定一段整数序列a1,a2,a3,a4,a5…an; 每次让我们求一段区间的和&#xff0c;正常做法是for循环遍历区间起始点到结束点&#xff0c;进行求和计算&#xff0c;但是当询问次数很多并且区间很长的时候 比如&#xff0c;10^5 个询问和10^6区间长度&#xff0c;相…

[JDK工具-2] javap 类文件解析工具-帮助理解class文件,了解Java编译器机制

文章目录 1. javap -version 版本信息2. javap -verbose 输出附加信息3. javap -l 显示行号和局部变量列表4. javap -c 对代码进行反汇编&#xff08;或叫反编译生成汇编代码&#xff0c;一般说反编译是生成java代码&#xff09;&#xff0c;分解方法代码&#xff0c;也就是显示…

电机应用开发-PID控制器参数整定

PID控制器参数整定 比例调节&#xff1a;调节作用快&#xff0c;系统一出现偏差&#xff0c;调节器立即将偏差放大输出。 积分调节&#xff1a;输出变化和输入偏差的积分成正比。输出不仅取决于偏差大小&#xff0c;还取决于偏差存在的时间。只要有偏差存在&#xff0c;尽管偏差…

【23真题】超难985!做完感觉没学过!

本套试卷难度分析&#xff1a;22年西北工业大学827考研真题&#xff0c;我也发布过&#xff0c;若有需要&#xff0c;戳这里自取&#xff01;本套试题内容有难度&#xff0c;题目考察全为大题&#xff0c;题目不多&#xff01;但是题目都很新颖&#xff0c;状态方程的题目考察较…

MySQL为什么选择了B+树

首先MySQL的数据**&#xff08;索引记录&#xff09;**是存在磁盘里的&#xff0c;磁盘读取非常慢&#xff0c;所以要尽可能减少磁盘操作&#xff0c;因此我们需要更好的利用索引。 首先索引按顺序排列了数据&#xff0c;那么很显然最好的查找方式是二分查找&#xff0c;数组自…

解决证书加密问题:OpenSSL与urllib3的兼容性与优化

在使用客户端证书进行加密通信时&#xff0c;用户可能会遇到一些问题。特别是当客户端证书被加密并需要密码保护时&#xff0c;OpenSSL会要求用户输入密码。这对于包含多个调用的大型会话来说并不方便&#xff0c;因为密码无法在连接的多个调用之间进行缓存和重复使用。用户希望…

改进YOLOv8:结合ConvNeXt V2骨干网络!使用MAE共同设计和扩展ConvNet

🗝️YOLOv8实战宝典--星级指南:从入门到精通,您不可错过的技巧   -- 聚焦于YOLO的 最新版本, 对颈部网络改进、添加局部注意力、增加检测头部,实测涨点 💡 深入浅出YOLOv8:我的专业笔记与技术总结   -- YOLOv8轻松上手, 适用技术小白,文章代码齐全,仅需 …

java桌面程序

目标之一是把打印导出的功能最终用java实现一套&#xff0c;首先选定javafx&#xff0c;因为idea默认创建工程就带的javafx&#xff0c;没找到swing。 创建工程&#xff0c;这里要选1.8&#xff0c;高版本jdk默认不带fx 实现主界面的代码 package sample;import javafx.app…

【DevOps】Git 图文详解(五):远程仓库

Git 图文详解&#xff08;五&#xff09;&#xff1a;远程仓库 1.远程用户登录1.1 &#x1f511; 远程用户登录&#xff1a;HTTS1.2 &#x1f511; 远程用户登录&#xff1a;SSH 2.远程仓库指令 &#x1f525;3.推送 push / 拉取 pull4.fetch 与 pull 有什么不同 &#xff1f; …

通过easyexcel实现数据导入功能

上一篇文章通过easyexcel导出数据到excel表格已经实现了简单的数据导出功能&#xff0c;这篇文章也介绍一下怎么通过easyexcel从excel表格中导入数据。 目录 一、前端代码 index.html index.js 二、后端代码 controller service SongServiceImpl 三、功能预览 四、后端…

Run Legends将健身运动游戏化,使用户保持健康并了解Web3游戏

最近&#xff0c;我们有机会采访Talofa Games的首席执行官兼创始人Jenny Xu&#xff0c;一起讨论游戏开发&#xff0c;Talofa Games是Run Legends这款健身游戏的开发工作室。她已经创作了超过一百款游戏&#xff0c;对于推动游戏的可能性并将她的创造力和叙事技巧带入她最喜爱的…

面试题-6

1.精灵图和base64的区别是什么&#xff1f; 精灵图:把多张小图整合到一张大图上,利用定位的一些属性把小图显示在页面上,当访问页面可以减少请求,提高加载速度 base64&#xff1a;传输8bit字节代码的编码方式,把原本二进制形式转为64个字符的单位&#xff0c;最后组成字符串 …

深入解析序列模型:全面阐释 RNN、LSTM 与 Seq2Seq 的秘密

探索序列建模的基础知识和应用。 简介 序列建模是许多领域的一个重要问题&#xff0c;包括自然语言处理 (NLP)、语音识别和语音合成、时间序列预测、音乐生成和「生物信息学」。所有这些任务的共同点是它们需要坚持。接下来的事情的预测是基于历史的。例如&#xff0c;在“哈桑…

漏洞复现--飞企互联FE业务协作平台ShowImageServlet任意文件读取

免责声明&#xff1a; 文章中涉及的漏洞均已修复&#xff0c;敏感信息均已做打码处理&#xff0c;文章仅做经验分享用途&#xff0c;切勿当真&#xff0c;未授权的攻击属于非法行为&#xff01;文章中敏感信息均已做多层打马处理。传播、利用本文章所提供的信息而造成的任何直…

【C/C++】递归算法

信封 某人写了n封信和n个信封&#xff0c;如果所有的信都装错了信封。求所有的信都装错信封共有多少种不同情况 #include <iostream> using namespace std; const int N 30; int n; long f[N];int main() {scanf("%d", &n);f[1] 0, f[2] 1;for (int …

面向开发者的Android

Developerhttps://developer.android.google.cn/?hlzh-cn SDK 平台工具版本说明https://developer.android.google.cn/studio/releases/platform-tools?hlzh-cn#revisions Android SDK Platform-Tools 是 Android SDK 的一个组件。它包含与 Android 平台进行交互的工具…

【Python】给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200

2、问题描述 给定一个长度为n的数列&#xff0c;将这个数列按从小到大的顺序排列。1<n<200 样例输入 5 8 3 6 4 9 样例输出 3 4 6 8 9 n int(input()) a list(map(int,input().split())) a.sort() for i in a:print(i,end ) 运行结果&#xff1a;

OceanBase 4.2.1 LTS 发版 | 一体化数据库首个长期支持版本

在刚刚结束的年度发布会上&#xff0c;OceanBase 沿着“一体化”产品战略思路&#xff0c;发布了一体化数据库的首个长期支持版本 4.2.1 LTS。作为 4.0 系列的第一个 LTS 版本&#xff0c;该版本的定位是支撑客户关键业务稳定长久运行&#xff0c;我们非常认真的打磨了这个版本…

OpenVPN Connect使用连接公网VPN服务器实现内网穿透

安装并运行OpenVPN Connect 点击AGREE 添加配置.OVPN文件 点击连接 连接成功 两个内网主机通过公网VPN穿透