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

📄 gamecanvas.java

📁 几个不错的手机程序例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
						//再描绘
						repaint();
						//等待描绘的间隔
						Thread.sleep(DRAW_INTERVAL);
					}// end while

					if(isGameOver) {
					//当游戏结束的标志建立时
						//删除指令
						removeCommand(pauseCmd);
						removeCommand(resumeCmd);
						removeCommand(quitCmd);

						//进行再描绘后等待一下
						repaint();
						Thread.sleep(1000);

						//显示游戏结束,在等待一定时间后显示标题
						gameState = GAME_END;
						repaint();
						Thread.sleep(5000);
						doTitle();
					}
				break;
			}
		}catch(Exception e) {}
	}

	/** 结束游戏的方法 */
	private void doGameStop() {
		//停止游戏循环
		isStopped = true;
		//设定最高分数
		setHighScore(score);
	}

	/** 设定最高分数的方法 */
	private void setHighScore(long score) {
		if(score > highScore) {
			highScore = score;
		}
	}

	/**
	 * 向下落下来结合的方法
	 * @return	boolean   当可以向下落下结合的时候就会传回true、
         * 若不能结合的话就传回false。
	 */
	private boolean doDropDown() {
		//显示是否可以落下的标志
		boolean isFallOK = false;
		//显示是否有放置的标志
		boolean isDropDown = false;
		//储存可以落下数的变量
		int fallOKCount = 0;

		int loopCount = (COLUMN_LENGTH -1) * ROW_LENGTH -1;
		for(int i=COLUMN_LENGTH * ROW_LENGTH-1; i > loopCount; i--) {//从右端来向左边
			isFallOK = false;
			fallOKCount = 0;
			for(int j=i; j > -1; j=j-ROW_LENGTH) {//当现在的Index是最上面
				if(pieces[j] != null) {
					//当现在的Index不是null时
					if(isFallOK) {
						//当可以落下时
						isDropDown = true;

						//寻找到成为null为止
						int jj = 0;
						for(jj=j-ROW_LENGTH; jj > -1; jj=jj-ROW_LENGTH) {
							if(pieces[jj] == null) {
								break;
							}
						}

						//落下可以落下的部分
						for(int f=j; f > jj; f=f-ROW_LENGTH) {
							pieces[f +(fallOKCount *ROW_LENGTH)] = pieces[f];
							pieces[f] = null;
						}
						//再度调查相同的纵列
						i++;
					}
				}else {
				//没有方块时
					isFallOK = true;
					fallOKCount++;
				}
			}
		}
		return isDropDown;
	}

	/**
	 * 传回储存了放入凑成同色地方的ndex值的Vector对象的Vector的方法
	 * @return	Vector	放入凑成同色地方的Index值的Vector对象的Vector
	 */
	private Vector doMatchCheck() {
		//准备用来检查用的Vector与Hashtable
		Vector matchVec = new Vector();
		Hashtable matchHash = new Hashtable();

		//用来调查该行是否为null的标志
		boolean isNullHorizon = false;

		//从Piece数组的右下顺序来看
		//为了不要浪费功夫只看上与左
		for(int i=COLUMN_LENGTH * ROW_LENGTH -1; i > -1; i=i-ROW_LENGTH) {//左边则向上检查
			if(isNullHorizon) {
			//横向一行为null时因为上面没有任何东西,因此结束for语法的处理
				break;
			}

			//清除用来调查该行是否为null的标志
			isNullHorizon = true;
			int endInx = i -ROW_LENGTH;
			for(int j=i; j > endInx; j--) {//从右边开始横向查看该行
				if(pieces[j] != null) {
				//现在位置的方块不是null的场合
					//将用来调查该列是否为null的标志设为false
					isNullHorizon = false;

					//上方的检查
					if(i > ROW_LENGTH -1 && pieces[j -ROW_LENGTH] != null) {
						//不是最上方、上方不是null时
						if(pieces[j -ROW_LENGTH].currentColor == pieces[j].currentColor) {
							//目前的方块与上面的方块为同Color时
							pieces[j].isMatchUp = true;
							pieces[j -ROW_LENGTH].isMatchDown = true;

							if(matchHash.get(String.valueOf(j)) == null) {
							//为没有处理的坐标时
							//当储存了凑在一起的Index的Hashtable没有任何值时
								if(matchHash.get(String.valueOf(j -ROW_LENGTH)) == null) {
								//当也没有处理上方的方块时
									//新产生Vector
									Vector tmpVect = new Vector();
									//在所产生的Vector中新增现在坐标、上方坐标
									tmpVect.addElement(new Integer(j));
									tmpVect.addElement(new Integer(j -ROW_LENGTH));
									matchHash.put(String.valueOf(j), tmpVect);
									matchHash.put(String.valueOf(j -ROW_LENGTH), tmpVect);
									//将产生的Vector上新增到Hashtable与Vector
									matchVec.addElement(tmpVect);
								}
							}else {
							//为已经处理过的坐标时
							//上方为未处理时
								Vector tmpVect = (Vector)matchHash.get(String.valueOf(j));
								tmpVect.addElement(new Integer(j -ROW_LENGTH));
								matchHash.put(String.valueOf(j -ROW_LENGTH), tmpVect);
							}
						}else {
						//现在的方块与上方的方块为不同Color时
							pieces[j].isMatchUp = false;
							pieces[j -ROW_LENGTH].isMatchDown = false;
						}
					}else {
					//上方为null时
						pieces[j].isMatchUp = false;
					}

					//左边的检查
					if(j % ROW_LENGTH != 0 && pieces[j -1] != null) {
					//当不是最左边的时候、或左边不是null的时候
						if(pieces[j -1].currentColor == pieces[j].currentColor) {
						//目前的方块与上面的方块为同Color时
							pieces[j].isMatchLeft = true;
							pieces[j -1].isMatchRight = true;

							if(matchHash.get(String.valueOf(j)) == null) {
							//为没有处理过的坐标时
								if(matchHash.get(String.valueOf(j -1)) == null) {
								//左方也为没有处理过时
									//新增Vector
									Vector tmpVect = new Vector();
									//在所产生的Vector中新增现在坐标、上方坐标
									tmpVect.addElement(new Integer(j));
									tmpVect.addElement(new Integer(j -1));
									matchHash.put(String.valueOf(j), tmpVect);
									matchHash.put(String.valueOf(j -1), tmpVect);
									matchVec.addElement(tmpVect);
								}else {
								//左方为处理过时
									Vector tmpVect = (Vector)matchHash.get(String.valueOf(j -1));
									tmpVect.addElement(new Integer(j));
									matchHash.put(String.valueOf(j), tmpVect);
								}
							}else {
							//为已经处理过的坐标时
								if(matchHash.get(String.valueOf(j -1)) == null) {
								//左方为没处理过时
									Vector tmpVect = (Vector)matchHash.get(String.valueOf(j));
									tmpVect.addElement(new Integer(j -1));
									matchHash.put(String.valueOf(j -1), tmpVect);
								}else {
								//当左方为处理过时就结合
									Vector curVect = (Vector)matchHash.get(String.valueOf(j));
									Vector leftVect = (Vector)matchHash.get(String.valueOf(j -1));
									if(! curVect.equals(leftVect)) {
										int curVectSize = curVect.size();
										for(int k=0; k < curVectSize; k++) {
											Integer intge = (Integer)curVect.elementAt(k);
											leftVect.addElement(intge);
											matchHash.put(intge.toString(), leftVect);
										}
										matchVec.removeElement(curVect);
									}
								}
							}
						}else {
						//目前的方块与左方的方块为不同Color时
							pieces[j].isMatchLeft = false;
							pieces[j -1].isMatchRight = false;
						}
					}else {
					//左方为null时
						pieces[j].isMatchLeft = false;
					}
				}else {
				//现在的坐标为null时
					if(i > ROW_LENGTH -1 && pieces[j -ROW_LENGTH] != null) {
					//当不是在最上面时就将上面方块的下方设为false
						pieces[j -ROW_LENGTH].isMatchDown = false;
					}
					if(j % ROW_LENGTH != 0 && pieces[j -1] != null) {
					//当不是在最左边时就将左边方块的右边设为false
						pieces[j -1].isMatchRight = false;
					}
				}
			}
		}
		return matchVec;
	}

	/** 进行暂停或是重新开始的方法 */
	private void doPauseOrResume() {
		if(isPaused) {
		//暂停时
			//重新开始
			isPaused = false;
			isStopped = false;
			doThreadStart();
		}else {
		//不是暂停时
			//暂停
			isPaused = true;
			doGameStop();
		}
		repaint();
	}

	/**
	 * 进行描绘的方法,通常不会从本身类直接调用
	 * @param	g	Graphics对象
	 */
	protected void paint(Graphics g) {
		//将背景以白色清除
		g.setColor(0x00FFFFFF);
		g.fillRect(0, 0, screenWidth, screenHeight);

		String drawSt;
		int drawX, drawY, stWidth;
		int stHeight = defFont.getHeight();

		g.setFont(defFont);

		//根据游戏状态来改变描绘内容
		switch(gameState) {
			case TITLE:			//显示标题时
				//描绘标题图像
				drawX = (screenWidth - titleImg.getWidth()) /2;
				g.drawImage(titleImg, drawX, 0, Graphics.TOP|Graphics.LEFT);

				//显示最高分数
				g.setColor(0x00000000);
				drawSt = "High Score";
				drawX = (screenWidth - defFont.stringWidth(drawSt)) / 2;
				drawY = titleImg.getHeight();
				g.drawString(drawSt, drawX, drawY, Graphics.TOP|Graphics.LEFT);
				drawX = drawX + defFont.stringWidth(drawSt) - defFont.stringWidth(String.valueOf(highScore));
				drawY = drawY + stHeight;
				g.drawString(String.valueOf(highScore), drawX, drawY, Graphics.TOP|Graphics.LEFT);

				//显示着作权
				drawSt = "Copyright 2001";
				drawX = (screenWidth - defFont.stringWidth(drawSt)) / 2;
				drawY = getHeight() - stHeight *2;
				g.drawString(drawSt, drawX, drawY, Graphics.TOP|Graphics.LEFT);
				drawSt = "SkyArts.com";
				drawX = (screenWidth - defFont.stringWidth(drawSt)) / 2;
				drawY = drawY + stHeight;
				g.drawString(drawSt, drawX, drawY, Graphics.TOP|Graphics.LEFT);
			return;

			case GAME_START:	//游戏开始时
				//描绘游戏开始画面
				g.setColor(0x00000000);
				drawSt = "Ready go !";
				stWidth = defFont.stringWidth(drawSt);
				drawX = (screenWidth - stWidth) / 2;
				drawY = (screenHeight - stHeight) / 2;
				g.drawString(drawSt, drawX, drawY, Graphics.TOP|Graphics.LEFT);
			return;

			case GAME_PLAYING:	//游戏中
			case GAME_END:		//游戏结束时
				Piece[] pieces = new Piece[COLUMN_LENGTH * ROW_LENGTH];
				System.arraycopy(this.pieces, 0, pieces, 0, pieces.length);

				g.setColor(0x00000000);
				//将分数描绘在画面左边
				drawSt = "S:" + String.valueOf(score);
				g.drawString(drawSt, 0, 0, Graphics.TOP|Graphics.LEFT);

				//显示边框榞傪昞帵
				int widSize = ROW_LENGTH * Piece.WIDTH;
				int heiSize = COLUMN_LENGTH * Piece.HEIGHT;
				g.setColor(0x000000FF);
				g.fillRect(0, screenHeight -heiSize -3, 3, heiSize +3);				//左
				g.fillRect(0, screenHeight -3, widSize +4, 3);					//下
				g.fillRect(widSize+4, screenHeight -heiSize -3, 3, heiSize +3);			//右
				g.setColor(0x0000FFFF);
				g.drawLine(1, screenHeight -heiSize -3+1, 1, screenHeight -2);			//左 
				g.drawLine(1, screenHeight -2, widSize+4, screenHeight -2);			//下
				g.drawLine(widSize +5, screenHeight -heiSize -3+1, widSize +5, screenHeight -2);//右

				//Next piece
				g.setColor(0x00000000);
				drawSt = "Next";
				stWidth = defFont.stringWidth(drawSt);
				g.drawString(drawSt, widSize + 12, screenHeight -heiSize -3, Graphics.TOP|Graphics.LEFT);

				if(! isPaused) {
				//暂停时不描绘方块
					//描绘方块部分
					int loopCount = COLUMN_LENGTH * ROW_LENGTH;
					for(int i=loopCount-1; i > -1; i=i-ROW_LENGTH) {
					//由右下往上增加列
						drawY = (Piece.HEIGHT * (i / ROW_LENGTH)) + screenHeight -heiSize -3;
						for(int j=i; j > i -ROW_LENGTH; j--) {
						//从右边横对该行横向前进
							if(pieces[j] != null) {
							//不是null时进行描绘
								drawX = (Piece.WIDTH * (j % ROW_LENGTH)) + 3;
								pieces[j].doDraw(g, drawX, drawY);
							}
						}
					}
					//描绘下一个方块
					nextPieces[0].doDraw(g, widSize + Piece.WIDTH, screenHeight -heiSize -3 + stHeight);
					nextPieces[1].doDraw(g, widSize + Piece.WIDTH *2 , screenHeight -heiSize -3 + stHeight);
				}

				//在暂停等画面中显示文字
				switch(gameState) {
					case GAME_PLAYING://游戏中
						if(isPaused) {
						//显示代表游戏暂停的文字
							drawSt = "Pause";
						}else {
							return;
						}
					break;

					case GAME_END://游戏结束时
						//显示代表游戏结束的文字
						drawSt = "Game over";
					break;

					default://在上述以外的状况时为return
					return;
				}

				//设定粗体文字
				stWidth = defFont.stringWidth(drawSt);
				drawX = (screenWidth-stWidth) / 2;
				drawY = (screenHeight - (stHeight)) / 2;

				//描绘文本框
				g.setColor(0x00BBBBBB);
				g.fillRoundRect(drawX-2, drawY+2, stWidth+9, stHeight+2, 5, 5);
				g.setColor(0x00000000);
				g.fillRoundRect(drawX-6, drawY-2, stWidth+11, stHeight+4, 5, 5);
				g.setColor(0x00FFFFFF);
				g.fillRoundRect(drawX-4, drawY, stWidth+7, stHeight, 5, 5);

				//显示消息
				g.setColor(0x00000000);
				g.drawString(drawSt, drawX, drawY, Graphics.TOP|Graphics.LEFT);
			break;
		}
	}
}

⌨️ 快捷键说明

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