使用Java实现一个简单的贪吃蛇小游戏

这篇具有很好参考价值的文章主要介绍了使用Java实现一个简单的贪吃蛇小游戏。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

开发工具:eclipse

java工具包:jdk1.8

代码地址:https://gitee.com/jay_musu/games-and-tools.git

一、创建新项目

创建一个新的项目,并命名。创建一个名为images的文件夹用来存放游戏相关图片。然后再在项目的src文件下创建一个com.xxx.view的包用来存放所有的图形界面类,创建一个com.xxx.controller的包用来存放启动的入口类(控制类)。如下所示;

使用Java实现一个简单的贪吃蛇小游戏

二、主要的游戏界面

1.代码

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();
		
	}


}

 三、构造启动类

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);
	}

}

四、游戏测试

设置贪吃蛇运行速度

使用Java实现一个简单的贪吃蛇小游戏

游戏界面

使用Java实现一个简单的贪吃蛇小游戏

使用Java实现一个简单的贪吃蛇小游戏文章来源地址https://www.toymoban.com/news/detail-512518.html

到了这里,关于使用Java实现一个简单的贪吃蛇小游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用TypeScript实现贪吃蛇小游戏(网页版)

    本项目使用webpack+ts所编写 下边是项目的文件目录 /src下边的index.html页面是入口文件 index.ts是引入所有的ts文件 /modules文件夹是用来存放所有类的 index.html index.ts  /style/less /modules/Food.ts /modules/GameControl.ts /modules/ScorePanel.ts /modules/Snake.ts

    2024年02月13日
    浏览(29)
  • 小游戏:人生中写的第一个小游戏——贪吃蛇(C语言)

            小编开了一个关于游戏的专栏,主要是运用easyx图形库进行的。        第一章:人生中写的第一个小游戏——贪吃蛇(C语言)         这个游戏的代码我在gitee上发布了,大家如果不嫌弃,可以进入这个网址进行查看和复制:https://gitee.com/rising-sun-1。      

    2024年02月09日
    浏览(43)
  • ❤️创意网页:贪吃蛇游戏 - 创造一个经典的小游戏

    ✨ 博主: 命运之光   🌸 专栏: Python星辰秘典 🐳 专栏: web开发(简单好用又好看) ❤️ 专栏: Java经典程序设计 ☀️ 博主的其他文章: 点击进入博主的主页 前言: 欢迎踏入我的Web项目专栏,一段神奇而令人陶醉的数字世界! 🌌 在这里,我将带您穿越时空,揭开属于

    2024年02月14日
    浏览(47)
  • 【用python做一个贪吃蛇小游戏】

    引言: 贪吃蛇是一款经典的小游戏,它既简单又有趣。在本文中,我们将使用Python编写一个贪吃蛇小游戏,通过这个项目来学习如何利用Python的基础知识和图形界面库来创建一个简单的游戏。 介绍: 贪吃蛇游戏的目标是控制一条蛇在屏幕上移动并吃掉食物,每吃掉一块食物

    2024年02月08日
    浏览(28)
  • 基于C#制作一个贪吃蛇小游戏

    基于C#制作一个贪吃蛇小游戏,简单耐玩,操作简单。

    2024年02月08日
    浏览(32)
  • 简单的贪吃蛇小游戏的 Python 代码

    简单的贪吃蛇小游戏的 Python 代码:

    2024年02月08日
    浏览(36)
  • python贪吃蛇最简单代码,python代码贪吃蛇小游戏

    大家好,小编来为大家解答以下问题,python贪吃蛇游戏代码详解外加中文,python贪吃蛇最简单代码,现在让我们一起来看看吧! Source code download: 本文相关源码 import pygame import sys import random SCREEN_X=600 SCREEN_Y=600 class Snake(object): def __init__(self): self.direction=pygame.K_RIGHT self.body = []

    2024年02月21日
    浏览(33)
  • JavaScript做一个贪吃蛇小游戏,无需网络直接玩。

    用JavaScript做一个贪吃蛇小游戏,无需网络 = 打开即可玩。 直接按上下左右键就可以玩起来了 欢迎关注我的原创文章:小伙伴们!我是一名热衷于前端开发的作者,致力于分享我的知识和经验,帮助其他学习前端的小伙伴们。在我的文章中,你将会找到大量关于前端开发的精

    2024年03月21日
    浏览(38)
  • Java小游戏—贪吃蛇

    摘 要 近年来,java作为一种新的编程语言,以其简单性、可移植性和平台无关性等优点,得到了各个国家广泛地认可。本文基于java技术和Eclipse开发环境,设计并实现了一个操作简单、界面美观的“贪吃蛇”游戏。“贪吃蛇”游戏是一个经典的游戏,它因操作简单、娱乐性强

    2024年02月03日
    浏览(33)
  • 【C语言】做一个贪吃蛇小游戏,完整代码&附带视频演示

    视频演示: https://www.bilibili.com/video/BV1pt421a7Nu/?spm_id_from=333.999.0.0vd_source=82b65865be0947de29bd55efc8cdb40a 编译环境:linux(Vmware16.2.4 + Ubantu20.04.3); 小蛇🐍只能在固定的范围内活动; 可以利用键盘方向键控制小蛇🐍的前进方向; 活动范围内会随机生成食物; 小蛇🐍吃到食物,身

    2024年02月21日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包