《Java小游戏》:球球大作战


首先灰常开森在2020年下半年这学期收获了很多知识,自己的意志也得到了锻炼,尤其是在verilog学习中 摸爬滚打 爬爬爬爬的无数个日日夜夜最令我难忘。
Java课大作业消息出来的时候我脑子一片空白,连Hello World都不会输出,让用Java写个小游戏,七八个星期以来老师上课给我们讲的内容我一点也没印象,莫名有种给你一盒擦炮去炸碉堡的感觉哦。
ddl最后那三天,着实难受>_<…。
往事不可追,寒假决定把自己一点点磨出来的东西传上来,供大家参考和指正,谢谢。

游戏规则

球球大作战时一款多玩家实时在线对抗策略类游戏。在这款游戏中,玩家操控一个具有一定初始体积的小球在地图中移动。为了使自己的小球的体积增大,玩家可以规划自己的路线并吃掉沿途上的“微粒”,“微粒”具有一个很小的体积,吃掉微粒后,小球可以增加相应的体积,随着体积不断变大,如果当玩家控制小球的体积在直径上明显大于另一名玩家的小球时,如果玩家成功地使自己的小球将对方小球的超过二分之一体积覆盖,那么判定成功吃掉对方的小球,玩家控制的小球体积随之增加,对方的小球被吃掉,需要恢复初始体积重新开始游戏。

主要实现方法

在编写项目前,首先对需要实现的功能进行分析,制作了如下的分析图。
项目待实现的功能分析
项目中较为关键的是Ball类。

public class Ball {
    
    
	private double x;			//绘制横坐标
	private double y;			//绘制纵坐标
	private double d;			//直径
	private double real_x;		//中心横坐标
	private double real_y;		//中心纵坐标
	private double speed;		//速度
	private double degree;		//角度
	private double m;			//质量
	private String name;		//昵称
	private boolean alive;		//存活
	private Color owncolor;		//颜色
	private BufferedImage flag;	//国家国旗

	public Ball(double x, double y, double d) {
    
    
		this.setX(x);
		this.setY(y);
		this.setD(d);
		this.setOwncolor(randomcolor());
		this.real_x = x + d / 2;
		this.real_y = y + d / 2;
		this.alive = true;
	}

	public Ball(double x, double y, double speed, double degree, double m) {
    
    
		this.setX(x);
		this.setY(y);
		this.speed = speed;
		this.degree = degree;
		this.m = m;
		FreshD();
		this.alive = true;
	}

	public Ball(double x, double y, double speed, double degree, double m, String name) {
    
    
		super();
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.degree = degree;
		this.m = m;
		this.name = name;
		this.setOwncolor(randomcolor());
		FreshD();
		this.alive = true;
	}
	
	public BufferedImage getFlag() {
    
    
		return flag;
	}

	public void setFlag(BufferedImage flag) {
    
    
		this.flag = flag;
	}

	public boolean isAlive() {
    
    
		return alive;
	}

	public void setAlive(boolean alive) {
    
    
		this.alive = alive;
	}

	public double getSpeed() {
    
    
		return speed;
	}

	public void setSpeed(double speed) {
    
    
		this.speed = speed;
	}

	public double getDegree() {
    
    
		return degree;
	}

	public void setDegree(double degree) {
    
    
		this.degree = degree;
	}

	public double getM() {
    
    
		return m;
	}

	public void setM(double m) {
    
    
		this.m = m;
	}

	public String getName() {
    
    
		return name;
	}

	public void setName(String name) {
    
    
		this.name = name;
	}

	public double getX() {
    
    
		return x;
	}

	public void setX(double x) {
    
    
		this.x = x;
	}

	public double getY() {
    
    
		return y;
	}

	public void setY(double y) {
    
    
		this.y = y;
	}

	public double getD() {
    
    
		return d;
	}

	public void setD(double d) {
    
    
		this.d = d;
	}

	public double getReal_x() {
    
    
		return real_x;
	}

	public void setReal_x(double real_x) {
    
    
		this.real_x = real_x;
	}

	public double getReal_y() {
    
    
		return real_y;
	}

	public void setReal_y(double real_y) {
    
    
		this.real_y = real_y;
	}

	public Color getOwncolor() {
    
    
		return owncolor;
	}

	public void setOwncolor(Color owncolor) {
    
    
		this.owncolor = owncolor;
	}

	public void draw(Graphics g) {
    
    
		Color c = g.getColor();
		g.setColor(Color.cyan);
		g.fillOval((int) x, (int) y, (int) d, (int) d);
		g.setColor(c);
	}

	public void draw(Graphics g, int windowx, int windowy) {
    
    	//绘制自身
		Color c = g.getColor();
		g.setColor(Color.cyan);
		g.fillOval((int) (x - windowx), (int) (y - windowy), (int) d, (int) d);
		g.setColor(c);
	}

	public void move() {
    
    					//移动小球
		if (x > ballfight.Width || x < 0)
			degree = Math.PI - degree;
		if (y > ballfight.Height || y < 0)
			degree = -degree;
		while (degree < 0)
			degree += 2 * Math.PI;
		while (degree > 2 * Math.PI)
			degree -= 2 * Math.PI;
		if (speed > 0) {
    
    
			x += speed * Math.cos(degree);
			y += speed * Math.sin(degree);
		}
	}

	public void eat(Ball b) {
    
    				//吃掉别的Ball类
		m += b.m;
		FreshD();
	}

	public void FreshD() {
    
    					//刷新小球中心位置
		d = 30 + Math.sqrt(m) * 4;
		real_x = x + d / 2;
		real_y = y + d / 2;
	}

	public Color randomcolor() {
    
    			
		Random rand = new Random();
		float r = rand.nextFloat();
		float s = rand.nextFloat();
		float t = rand.nextFloat();
		return (new Color(r, s, t));
	}
}

Player(玩家)类继承Ball类,但是需要重写draw()方法、move()方法,并添加spore()方法和my_spore_move()方法,具体如下。

public void move(double mx, double my) {
    
    		//移动的边界处理和普通Ball不同
		double dis = Math.sqrt((mx - screenx) * (mx - screenx) + (my - screeny) * (my - screeny));
		double Degree;
		Degree = Math.acos((mx - screenx) / dis);
		if (my - screeny < 0) {
    
    
			Degree = -Degree;
		}
		setDegree(Degree);
		if (dis > 2) {
    
    
			if (getSpeed() > 0) {
    
    
				if ((getX() + getSpeed() * Math.cos(getDegree()) < 0
						|| getX() + getSpeed() * Math.cos(getDegree()) > ballfight.Width)
						&& (getY() + getSpeed() * Math.sin(getDegree()) < 0
								|| getY() + getSpeed() * Math.sin(getDegree()) > ballfight.Height)) {
    
    
				} else if (getX() + getSpeed() * Math.cos(getDegree()) < 0
						|| getX() + getSpeed() * Math.cos(getDegree()) > ballfight.Width) {
    
    
					setY(getY() + getSpeed() * Math.sin(getDegree()));
					FreshD();
				} else if (getY() + getSpeed() * Math.sin(getDegree()) < 0
						|| getY() + getSpeed() * Math.sin(getDegree()) > ballfight.Height) {
    
    
					setX(getX() + getSpeed() * Math.cos(getDegree()));
					FreshD();
				} else {
    
    
					setX(getX() + getSpeed() * Math.cos(getDegree()));
					setY(getY() + getSpeed() * Math.sin(getDegree()));
					FreshD();
				}
			}
		}
	}

	public double getsize(double weight) {
    
    
		return 10 + Math.sqrt(weight) * 4;
	}

	public void spore(double speed) {
    
    		//吐出孢子
		System.out.println(getM());
		if (resttime < ballfight.timeperspore / ballfight.breaktime)
			resttime++;
		else {
    
    
			resttime = 0;
			if (getM() > 2 * ballfight.sporeweight) {
    
    
				double rx, ry;
				Spore s = new Spore(
						getReal_x() + getD() * 0.5 * Math.cos(getDegree()) - 0.5 * getsize(ballfight.sporeweight),
						getReal_y() + getD() * 0.5 * Math.sin(getDegree()) - 0.5 * getsize(ballfight.sporeweight),
						speed, this.getDegree(), ballfight.sporeweight);
				s.FreshD();
				spores.add(s);
				rx = getReal_x();
				ry = getReal_y();
				double weight = getM();
				weight -= s.getM();
				setM(weight);
				FreshD();
				setX(rx - getD() * 0.5);
				setY(ry - getD() * 0.5);
				FreshD();
			}
		}
	}

	public void my_spore_move() {
    
    		//所有已吐出孢子运动
		for (int i = 0; i < spores.size(); i++) {
    
    
			Spore s = spores.get(i);
			if (s.isAlive()) {
    
    
				s.move();
				s.FreshD();
			}
		}
	}

完成基本的类的编写,下面是页面以及线程的编写。
主类ballfight继承JPanel,在主类的实例化方法中初始化JFrame作为游戏页面的框架。

public class ballfight extends JPanel
public ballfight(String name) {
    
    
		LoadImgs();
		setBackground(Color.black);
		InitialImages();
		frame = new JFrame(name);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.add(this);
		frame.setBackground(Color.black);
		frame.setBounds(this.X, this.Y, ballfight.windowWidth, ballfight.windowHeight);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

接下来为frame添加KeyListener键盘监听。

keylistener = new KeyListener() {
    
    
			/**************/

			@Override
			public void keyTyped(KeyEvent e) {
    
    
				// TODO Auto-generated method stub
				/*********/
			}

			@Override
			public void keyReleased(KeyEvent e) {
    
    
				// TODO Auto-generated method stub
				/*********/
			}

			@Override
			public void keyPressed(KeyEvent e) {
    
    
				// TODO Auto-generated method stub
				/*********/
			}

		};
frame.addKeyListener(keylistener);

为ballfight类中的实例化对象添加了MouseMotionListener和MouseListener监听,用来获取用户在面板上的鼠标移动和鼠标点击。

game.mouse_adapter = new MouseAdapter() {
    
    

			@Override
			public void mouseClicked(MouseEvent e) {
    
    
				// TODO Auto-generated method stub
			}

			@Override
			public void mousePressed(MouseEvent e) {
    
    
				// TODO Auto-generated method stub
				/*********/
			}

		};

		game.addMouseListener(mouse_adapter);
		game.addMouseMotionListener(new MouseMotionListener() {
    
    

			@Override
			public void mouseMoved(MouseEvent e) {
    
    
				// TODO Auto-generated method stub
				mx = e.getX();
				my = e.getY();
			}

			@Override
			public void mouseDragged(MouseEvent e) {
    
    
				// TODO Auto-generated method stub

			}
		});

在游戏进行中需要在地图中随机生成微粒,通过Runnable接口定义一个ParticleMaker类,将这个线程加入到主类中即可实现随机生成的任务。

public class ParticleMaker implements Runnable {
    
    

		@Override
		public void run() {
    
    
			// TODO Auto-generated method stub
			while (player.isAlive()) {
    
    
				synchronized (this) {
    
    
					for (int i = 0; i < particlepertime; i++) {
    
    
						MakeParticle();
					}
				}
				try {
    
    
					Thread.sleep(particletime);
				} catch (Exception e) {
    
    
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}
	}

为了增强游戏的趣味性,添加了和用户体重相关的排行榜功能。通过定义一个ranking类,实现对场上所有存活玩家和敌人的体重排序并生成一个实时更新的排行榜,供玩家参考自己的体重在当前场上的排名情况。

public class ranking {
    
    
		Ball[] M = new Ball[10000];
		Color Gold = new Color(255, 215, 0);
		Color Silver = new Color(192, 192, 192);
		Color Copper = new Color(244, 164, 96);
		Color MediumOrchid = new Color(186, 85, 211);
		Color Lavender = new Color(230, 230, 250);
		Color CornflowerBlue = new Color(100, 149, 237);

		public Ball[] getM() {
    
    
			return M;
		}

		public void setM(Ball[] m) {
    
    
			M = m;
		}

		public ranking() {
    
    
			int j = 0;
			for (int i = 0; i < enemy.size(); i++) {
    
    
				Enemy e = enemy.get(i);
				if (e != null && e.isAlive()) {
    
    
					M[j] = e;
					j++;
				}
			}
			M[j] = player;
			for (int i = 0; i < ranknum; i++) {
    
    
				int x = i;
				for (int ii = i + 1; ii <= j; ii++) {
    
    
					if (M[ii].getM() > M[x].getM()) {
    
    
						x = ii;
					}
				}
				Ball y = M[i];
				M[i] = M[x];
				M[x] = y;
			}
		}

		public void UpdateRankingList() {
    
    
			int j = 0;
			for (int i = 0; i < enemy.size(); i++) {
    
    
				Enemy e = enemy.get(i);
				if (e != null && e.isAlive()) {
    
    
					M[j] = e;
					j++;
				}
			}
			M[j] = player;
			for (int i = 0; i < ranknum; i++) {
    
    
				int x = i;
				for (int ii = i + 1; ii <= j; ii++) {
    
    
					if (M[ii].getM() > M[x].getM()) {
    
    
						x = ii;
					}
				}
				Ball y = M[i];
				M[i] = M[x];
				M[x] = y;
			}
		}

		public void DrawRankingList(Graphics g) {
    
    
			UpdateRankingList();
			g.setColor(MediumOrchid);
			g.setFont(new Font("Comic Sans MS", Font.LAYOUT_LEFT_TO_RIGHT, 21));
			g.drawString("---Ranking---", windowWidth - 200, 20);
			g.setFont(new Font("楷体", Font.LAYOUT_LEFT_TO_RIGHT, 18));
			g.drawString("排名", windowWidth - 200, 45);
			g.drawString("昵称", windowWidth - 140, 45);
			g.drawString("地区", windowWidth - 90, 45);

			g.setColor(Gold);
			g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));
			g.drawString(1 + ": ", windowWidth - 200, 70 + fontlineheight * 0);
			g.setColor(Color.white);
			g.drawString(M[0].getName(), windowWidth - 160, 70 + fontlineheight * 0);
			g.drawImage(M[0].getFlag(), windowWidth - 85, 55 + fontlineheight * 0, 27, 18, null);

			g.setColor(Silver);
			g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));
			g.drawString(2 + ": ", windowWidth - 200, 70 + fontlineheight * 1);
			g.setColor(Color.white);
			g.drawString(M[1].getName(), windowWidth - 160, 70 + fontlineheight * 1);
			g.drawImage(M[1].getFlag(), windowWidth - 85, 55 + fontlineheight * 1, 27, 18, null);

			g.setColor(Copper);
			g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));
			g.drawString(3 + ": ", windowWidth - 200, 70 + fontlineheight * 2);
			g.setColor(Color.white);
			g.drawString(M[2].getName(), windowWidth - 160, 70 + fontlineheight * 2);
			g.drawImage(M[2].getFlag(), windowWidth - 85, 55 + fontlineheight * 2, 27, 18, null);

			for (int i = 3; i < ranknum; i++) {
    
    
				g.setColor(CornflowerBlue);
				g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));
				g.drawString(i + 1 + ": ", windowWidth - 200, 70 + fontlineheight * i);
				g.setColor(Color.white);
				g.drawString(M[i].getName(), windowWidth - 160, 70 + fontlineheight * i);
				g.drawImage(M[i].getFlag(), windowWidth - 85, 55 + fontlineheight * i, 27, 18, null);
			}
		}
	}

游戏流程展示

1. 开始游戏页面

在这里插入图片描述

2. 加载中页面

在这里插入图片描述

3. 选择地图页面

在这里插入图片描述

4. 自定义昵称页面

在这里插入图片描述

5. 运行页面

在这里插入图片描述

死亡页面

在这里插入图片描述

发展方向

这次项目学习收获很大,在初期因为空指针错误耽误了很长时间去琢磨Java中的ArrayList和List及其方法,但是当问题经过广泛地排查得到解决后对知识的理解也更进一步。这个项目现在虽然能够较流畅地运行,但是其发展空间仍然巨大。正如前面在分析图中介绍的,我希望能够为所有的敌人类分阶段地设计编写一种智能的算法,让程序控制的小球能够在某种情境下以超过玩家的速度发育,可能要用到贪心算法、最短路径等理论。在真正的手机端游戏中,小球可以分裂成两个、四个甚至更多,因为本项目未实现玩家视野大小的动态调整,所以很遗憾没能实现分裂功能。这些都为以后这个项目的继续发展提供了可能。

(前面都不感兴趣,快进到)原码

链接:https://pan.baidu.com/s/1oLecTmwYWkvFDwVWu9KFYQ
提取码:y0v1

最后

我是第一次用Java写游戏,所以如果有错误和愚蠢的地方请大家指出,也请同样小白的同学对我的代码不要依赖可能有错555 ,欢迎大家在评论区交流学习!\\^ _ ^//!

猜你喜欢

转载自blog.csdn.net/pandas_dream/article/details/113086334