bubblemanager.java

来自「本代码为java编写的泡泡龙游戏」· Java 代码 · 共 85 行

JAVA
85
字号

import java.awt.*;
import java.util.Random;

//泡泡管理器
public class BubbleManager
{
	int bubblesLeft;//目前总共泡泡的个数
	Image[] bubbles;//所有泡泡对应的图像
	int[] countBubbles;//每个泡泡对应的个数
	
	public BubbleManager(Image[] bubbles)
	{	
		this.bubbles = bubbles;
		this.countBubbles = new int[bubbles.length];
		this.bubblesLeft = 0;
	}
	
	public void addBubble(Image bubble)
	{
		countBubbles[findBubble(bubble)]++;
		bubblesLeft++;
	}
	
	public void removeBubble(Image bubble)
	{
		countBubbles[findBubble(bubble)]--;
		bubblesLeft--;
	}
	
	public int countBubbles()
	{
		return bubblesLeft;
	}
	
	//得到下一个泡泡,并且每次尽量找已存的一个泡泡
	public int nextBubbleIndex(Random rand)
	{
		int select = rand.nextInt() % bubbles.length;
		
		if (select < 0)
		{
			select = -select;
		}

		int count = -1;
		int position = -1;
		
		while (count != select)
		{
			position++;
			
			if (position == bubbles.length)
			{
				position = 0;
			}
			
			if (countBubbles[position] != 0)
			{
				count++;
			}

		}

		return position;
	}
	
	public Image nextBubble(Random rand)
	{
		return bubbles[nextBubbleIndex(rand)];
	}
	
	private int findBubble(Image bubble)
	{
		for (int i=0 ; i<bubbles.length ; i++)
		{
			if (bubbles[i] == bubble)
			{
				return i;
			}
		}
		
		return -1;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?