⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keybuffer.java

📁 一款基于java 的赛车类游戏 一款基于java 的赛车类游戏
💻 JAVA
字号:
/*
 * Created on 2005-07-29
 *
 * Copyright (c) 2005 nanoGames. All Rights Reserved.
 *
 */
//package com.nano.KangooJumper;


/**
 * @author plumkawka
 * KeyBuffer
 */
public class KeyBuffer
{
	public	final	static int		BUFFER_SIZE = 16;
	public	final	static	int		KEY_INPUT_INTERVAL = 100;
	public	final	static	int		KEY_INPUT_LONGINTERVAL = 1000;
	
	public	final	static	byte	KEY_DELAY = -1;
	public	final	static	byte	KEY_UP = 0;
	public	final	static	byte	KEY_RIGHT = 1;
	public	final	static	byte	KEY_DOWN = 2;
	public	final	static	byte	KEY_LEFT = 3;
	public	final	static	byte	KEY_FIRE = 4;
	public	final	static	byte	KEY_1 = 5;
	public	final	static	byte	KEY_2 = 6;
	public	final	static	byte	KEY_3 = 7;
	public	final	static	byte	KEY_4 = 8;
	public	final	static	byte	KEY_5 = 9;
	public	final	static	byte	KEY_6 = 10;
	public	final	static	byte	KEY_7 = 11;
	public	final	static	byte	KEY_8 = 12;
	public	final	static	byte	KEY_9 = 13;
	public	final	static	byte	KEY_0 = 14;
	public	final	static	byte	KEY_POUND = 15;
	public	final	static	byte	KEY_HASH = 16;
	public	final	static	byte	KEY_MENULEFT = 17;
	public	final	static	byte	KEY_MENURIGHT = 18;
	
	public	boolean	clear;
	private	byte[]	keyBuffer;
	private	long	lastUpdateTime;
	private	long	currentTime;
	
	public KeyBuffer()
	{
		clear = true;
		lastUpdateTime = System.currentTimeMillis();
		keyBuffer = new byte[BUFFER_SIZE];
		for (int i=0; i<BUFFER_SIZE; i++)
			keyBuffer[i] = KEY_DELAY;

	}
	
	public void update(byte key)
	{
		currentTime = System.currentTimeMillis();
		if (currentTime >= (lastUpdateTime + KEY_INPUT_INTERVAL))
		{
			// longer delay between key input?
			if (currentTime >= (lastUpdateTime + KEY_INPUT_INTERVAL + KEY_INPUT_LONGINTERVAL))
			{
				for (int i=(BUFFER_SIZE - 2); i>0; i--)
					keyBuffer[i + 1] = keyBuffer[i];
				
				keyBuffer[1] = KEY_DELAY;
			}
			else
			{
				// normal input
				for (int i=(BUFFER_SIZE - 2); i>-1; i--)
					keyBuffer[i + 1] = keyBuffer[i];
			}
			
			keyBuffer[0] = key;
			lastUpdateTime = System.currentTimeMillis();
		}
		/*String s = "";
		for (int i=0; i<BUFFER_SIZE; i++)
			s += " "+keyBuffer[i];
		Utils.Log(s);*/
	}
	
	public boolean checkKey(byte key)
	{
		for (int i=(BUFFER_SIZE - 1); i>-1; i--)
		{
			if (keyBuffer[i] == key)
			{
				if (clear)
					keyBuffer[i] = KEY_DELAY;
				
				return true;
			}
		}
		
		return false;
	}
	
	public boolean checkKey(byte key1, byte key2)
	{
		for (int i=(BUFFER_SIZE - 1); i>-1; i--)
		{
			if (keyBuffer[i] == key1)
			{
				if (((i - 1) >= 0) && (keyBuffer[i - 1] == key2))
				{
					if (clear)
					{
						keyBuffer[i] = KEY_DELAY;
						keyBuffer[i - 1] = KEY_DELAY;
					}
					
					return true;						
				}
			}
		}
		
		return false;
	}

	public boolean any()
	{
		
		for (int i=(BUFFER_SIZE - 1); i>-1; i--)
		{
			if (keyBuffer[i] != KEY_DELAY)
				return true;
		}
		
		return false;
	}
	
	public	void	clear()
	{
		for (int i=(BUFFER_SIZE - 1); i>-1; i--)
			keyBuffer[ i ] = KEY_DELAY;
		
		lastUpdateTime = 0;
	}
	
}

⌨️ 快捷键说明

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