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

📄 gamecanvas.java

📁 几个不错的手机程序例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					if( pieces[centerPoint -1] == null &&
						pieces[centerPoint  +ROW_LENGTH -1] == null)
					{
					//在左下与左方没有任何东西时向左移动
						pieces[centerPoint -1] = pieces[centerPoint +ROW_LENGTH];
						pieces[centerPoint +ROW_LENGTH] = null;

						//将方向变为向左
						currentDirection = DIRECTION_LEFT;
					}
				}
			break;

			case DIRECTION_LEFT:	//方向为向左时
				//因为要作顺时钟旋转,所以要检查上方
				if(centerPoint > ROW_LENGTH -1) {
				//当不在最上面的时候
					if( pieces[centerPoint -ROW_LENGTH] == null &&
						pieces[centerPoint -ROW_LENGTH -1] == null)
					{
					//在左上与上方都没有东西时向上移动
						pieces[centerPoint -ROW_LENGTH] = pieces[centerPoint -1];
						pieces[centerPoint -1] = null;

						//将方向设为向上
						currentDirection = DIRECTION_UP;
					}
				}
			break;
		}
	}

	/** 按下左按键后被调用出来的方法 */
	private void doLeft() {
		switch(currentDirection) {
			case DIRECTION_UP:		//方向向上时
				if(centerPoint % ROW_LENGTH != 0) {
				//不是在最左边的时候
					//检查左方与左上
					if( pieces[centerPoint -1] == null &&
						pieces[centerPoint -ROW_LENGTH -1] == null)
					{
					//当左与左上都是空的时候
						//向左移动一格
						pieces[centerPoint -1] = pieces[centerPoint];
						pieces[centerPoint -ROW_LENGTH -1] = pieces[centerPoint -ROW_LENGTH];
						pieces[centerPoint] = null;
						pieces[centerPoint -ROW_LENGTH] = null;
						//移动中心位置
						centerPoint--;
					}
				}
			break;

			case DIRECTION_RIGHT:	//方向向右时
				//检查左方
				if(centerPoint % ROW_LENGTH != 0) {
				//不是在最左边的时候
					if( pieces[centerPoint -1] == null ) {
					//当左与左上都是空的时候
					//向左移动一格
						pieces[centerPoint -1] = pieces[centerPoint];
						pieces[centerPoint] = pieces[centerPoint +1];
						pieces[centerPoint +1] = null;
						//移动中心位置
						centerPoint--;
					}
				}
			break;

			case DIRECTION_DOWN:	//方向向下时
				//检查左方与左下
				if(centerPoint % ROW_LENGTH != 0) {
				//不是在最左边的时候
					if( pieces[centerPoint -1] == null &&
						pieces[centerPoint +ROW_LENGTH -1] == null ) {
					//当左与左下都是空的时候
						//向左移动一格
						pieces[centerPoint -1] = pieces[centerPoint];
						pieces[centerPoint +ROW_LENGTH -1] = pieces[centerPoint +ROW_LENGTH];
						pieces[centerPoint] = null;
						pieces[centerPoint +ROW_LENGTH] = null;
						//移动中心位置
						centerPoint--;
					}
				}
			break;

			case DIRECTION_LEFT:	//方向向下时
				//检查左方
				if((centerPoint -1)% ROW_LENGTH != 0) {
				//不是在最左边的时候
					if( pieces[centerPoint -1 -1] == null ) {
					//当附在左方的方块不在最左边、且左方为空的时候
						//向左移动一格
						pieces[centerPoint -1 -1] = pieces[centerPoint -1];
						pieces[centerPoint -1] = pieces[centerPoint];
						pieces[centerPoint] = null;
						//移动中心位置
						centerPoint--;
					}
				}
			break;
		}
	}

	/** 按下右按键时被调用出来的方法 */
	private void doRight() {
		switch(currentDirection) {
			case DIRECTION_UP:		//方向向上时
			if(centerPoint % ROW_LENGTH != ROW_LENGTH-1) {
			//不是在最右边的时候
				//检查右与右上
				if( pieces[centerPoint +1] == null &&
					pieces[centerPoint -ROW_LENGTH +1] == null ) {
				//当右与右上为空白的时候
					//向右移动一格
					pieces[centerPoint +1] = pieces[centerPoint];
					pieces[centerPoint -ROW_LENGTH +1] = pieces[centerPoint -ROW_LENGTH];
					pieces[centerPoint] = null;
					pieces[centerPoint -ROW_LENGTH] = null;
					//移动中心位置
					centerPoint++;
				}
			}
			break;

			case DIRECTION_RIGHT:	//方向向右时
				if((centerPoint +1) % ROW_LENGTH != ROW_LENGTH-1) {
				//不是在最右边的时候
					//检查右边
					if( pieces[centerPoint +1 +1] == null ) {
					//当附在右方的方块不在最右边、且右方为空白的时候
						//向右移动一格
						pieces[centerPoint +1 +1] = pieces[centerPoint +1];
						pieces[centerPoint +1] = pieces[centerPoint];
						pieces[centerPoint] = null;
						//移动中心位置
						centerPoint++;
					}
				}
			break;

			case DIRECTION_DOWN:	//方向向下时
				if(centerPoint % ROW_LENGTH != ROW_LENGTH-1) {
				//不是在最右边的时候
					//检查右与右下
					if( pieces[centerPoint +1] == null &&
						pieces[centerPoint +ROW_LENGTH +1] == null ) {
					//当右与右下为空白的时候
						//向右移动一格
						pieces[centerPoint +1] = pieces[centerPoint];
						pieces[centerPoint +ROW_LENGTH +1] = pieces[centerPoint +ROW_LENGTH];
						pieces[centerPoint] = null;
						pieces[centerPoint +ROW_LENGTH] = null;
						//移动中心位置
						centerPoint++;
					}
				}
			break;

			case DIRECTION_LEFT:	//方向向左时
				if(centerPoint % ROW_LENGTH != ROW_LENGTH-1) {
				//不是在最右边的时候
					//检查右边
					if( pieces[centerPoint +1] == null ) {
					//当右方为空白的时候
						//向右移动一格
						pieces[centerPoint +1] = pieces[centerPoint];
						pieces[centerPoint] = pieces[centerPoint -1];
						pieces[centerPoint -1] = null;
						//移动中心位置
						centerPoint++;
					}
				}
			break;
		}
	}

	/**
	 * 按下下按键或是向下落下时被调用的方法
	 * @return	boolean	当可以向下落下时就会传回true、不能落下时就会传回false
	 */
	private boolean doFall() {
		switch(currentDirection) {
			case DIRECTION_UP:		//方向为向上时
				//检查正下方
				if(centerPoint < (COLUMN_LENGTH -1) * ROW_LENGTH) {
					//当不是最下方的时候
					if( pieces[centerPoint +ROW_LENGTH] == null ) {
						//当正下方为空白时
						//就往下一格
						pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
						pieces[centerPoint] = pieces[centerPoint -ROW_LENGTH];
						pieces[centerPoint -ROW_LENGTH] = null;
						//移动中心位置
						centerPoint = centerPoint + ROW_LENGTH;
						return true;
					}
				}
			break;

			case DIRECTION_RIGHT:	//方向为向右时
			//检查正下方与右下
				if(centerPoint < (COLUMN_LENGTH -1) * ROW_LENGTH) {
				//当不是最下方的时候
					if( pieces[centerPoint +ROW_LENGTH]  == null &&
						pieces[centerPoint +1 +ROW_LENGTH] == null ) {
					//当正下方与右下为空白时
						//就往下一格
						pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
						pieces[centerPoint +1 +ROW_LENGTH] = pieces[centerPoint +1];
						pieces[centerPoint] = null;
						pieces[centerPoint +1] = null;
						//移动中心位置
						centerPoint = centerPoint +ROW_LENGTH;
						return true;
					}
				}
			break;

			case DIRECTION_DOWN:	//方向为向下时
			//检查正下方
				if(centerPoint +ROW_LENGTH < (COLUMN_LENGTH -1) * ROW_LENGTH) {
				//当附属的方块不是最下方的时候
					if( pieces[centerPoint +ROW_LENGTH +ROW_LENGTH]  == null) {
						//当附属在下面的方块不是在最下面,而正下方为空白时						//往下移动一格
						pieces[centerPoint +ROW_LENGTH +ROW_LENGTH] = pieces[centerPoint +ROW_LENGTH];
						pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
						pieces[centerPoint] = null;
						//移动中心位置
						centerPoint = centerPoint + ROW_LENGTH;
						return true;
					}
				}
			break;

			case DIRECTION_LEFT:	//当方向为向左时
			//调查正下方与左下
				if(centerPoint < (COLUMN_LENGTH -1) * ROW_LENGTH) {
					if( pieces[centerPoint +ROW_LENGTH]  == null &&
					pieces[centerPoint -1 +ROW_LENGTH] == null ) {
					//正下方与左下为空白时
						//往下移动一格
						pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
						pieces[centerPoint -1 +ROW_LENGTH] = pieces[centerPoint -1];
						pieces[centerPoint] = null;
						pieces[centerPoint -1] = null;
						//移动中心位置
						centerPoint = centerPoint +ROW_LENGTH;
						return true;
					}
				}
			break;
		}
		return false;
	}


	/** 启动描绘线程的方法 */
	private void doThreadStart() {
		//启动线程
		new Thread(this).start();
	}

	/** 线程的运作部分 */
	public void run() {
		try {
			switch(gameState) {
				case GAME_START://游戏开始时
					//先将标题的指令删除
					removeCommand(startCmd);
					removeCommand(exitCmd);
					//显示游戏开始画面
					repaint();
					Thread.sleep(1500);
					//新增指令
					addCommand(quitCmd);
					if(! isPaused) {
						addCommand(pauseCmd);
					}
					//将状态设为游戏中,然后就此转移到游戏中的处理
					gameState = GAME_PLAYING;

				case GAME_PLAYING://游戏中
					repaint();
					//游戏结束标志
					boolean isGameOver = false;
					//开始游戏循环
					while(! isStopped) {//在Stop前持续循环
						if(isNextFall) {
						//落下下一个时
							if(	pieces[FALL_START_INDEX1] != null ||
								pieces[FALL_START_INDEX2] != null)
							{
							//无法落下下一个时就算游戏结束
								isGameOver = true;
								//将此循环结束
								doGameStop();
								pieces[FALL_START_INDEX1] = nextPieces[0];
								pieces[FALL_START_INDEX2] = nextPieces[1];
								break;
							}else {
							//让下一个落下时
								pieces[FALL_START_INDEX1] = nextPieces[0];
								pieces[FALL_START_INDEX2] = nextPieces[1];
								currentDirection = DIRECTION_DEFAULT;
								centerPoint = FALL_START_INDEX1;
								isNextFall = false;
								//准备下一个方块组
								nextPieces = getNextPieces();

								//加速落下速度
								fallCount++;
								if(fallCount > 4) {
									fallCount = 0;
									if(tickCount4Speed > 2) {
										tickCount4Speed--;
									}
								}
								isKeyEnable = true;
							}
						}else {
						//已经落下时
							tickCount4Fall++;
							if(tickCount4Fall == tickCount4Speed) {
							//为落下时机时
								tickCount4Fall = 0;

								if(! doFall()) {
								//不让其落下后
									repaint();
									//使按键无效
									isKeyEnable = false;
									if(doDropDown()) {
									//不能若下全体方块组、而能够落下方块时
										//暂停后进行下一个处理
										repaint();
										Thread.sleep(500);
									}

									//连锁回数
									int chainCount = 1;
									while(true) {
										//检查是否凑成同色
										//取得传回放有凑成同色之Index之Vector的Vector
										Vector matchVect = doMatchCheck();
										int matchVectSize = matchVect.size();
										if(matchVectSize > 0) {
										//当有凑成同色的地方时
											repaint();
											Thread.sleep(200);

											boolean isDropDown = false;
											//检查凑成同色的地方
											for(int i=0; i < matchVectSize; i++) {
												Vector inxVect = (Vector)matchVect.elementAt(i);
												int matchInxLeng = inxVect.size();
												if(matchInxLeng >= 4) {
												//当凑成4个以上
													//加上分数
													int tmpScore = DEFAULT_SCORE_VALUE;
													tmpScore = tmpScore + ((matchInxLeng-4) * DEFAULT_SCORE_VALUE);
													tmpScore = tmpScore * chainCount;
													score = score + tmpScore;

													isDropDown = true;
													for(int j=0; j < matchInxLeng; j++) {
													//清除各方块
														Integer inxInteg = (Integer)inxVect.elementAt(j);
														pieces[inxInteg.intValue()] = null;
													}
												}
											}
											repaint();
											Thread.sleep(200);

											if(isDropDown) {
											//要删除时就在暂停后进行下一个处理
												doDropDown();
												repaint();
												Thread.sleep(500);
											}else {
											//都没有结合的部分就拨除while
												break;
											}
										}else {
										//当凑成同色的部分不见时就拨除while
											break;
										}
										chainCount++;
									}
									//落下下一个方块组
									isNextFall = true;
								}
							}
						}

⌨️ 快捷键说明

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