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

📄 maingame.java

📁 QQ游戏大厅
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				else
				{
					startOver = false;
					peaceOver = false;
					lostOver = false;
					cursor = 0;
					if (!playChess)
					{
						setDefaultCursor();
					}
					mp.repaint();
				}
			}
		});
	}
	//----------游戏中的计时,每人有10分钟下棋时间,谁时间到就谁输----------//
	public void timer()
	{
		timerLeft = new Timer(1000, new ActionListener()
		{
			public void actionPerformed(ActionEvent ev)
			{		
				if (sec == 0)
				{
					sec = 9;
					if (dSec == 0)
					{
						dSec = 5;
						if (min == 0)
						{
							if (dMin != 0)
							{
								min = 9;
								dMin--;
							}
							else
							{
								if (way.equals(MyProtocol.LEFT_TABLE))
								{
									JOptionPane.showMessageDialog(mp , "时间到,你输了" , "输了" ,
																JOptionPane.INFORMATION_MESSAGE);
								}
								else
								{
									JOptionPane.showMessageDialog(mp , "恭喜!!对方时间到,你赢了" , "胜利" ,
																JOptionPane.INFORMATION_MESSAGE);
								}
								winCountR++;
								reset();
							}
						}
						else
						{
							min--;
						}
					}
					else
					{
						dSec--;
					}
				}	
				else
				{
					sec--;
				}
				mp.repaint();			
			}
		});

		timerRight = new Timer(1000, new ActionListener()
		{
			public void actionPerformed(ActionEvent ev)
			{		
				if (secR == 0)
				{
					secR = 9;
					if (dSecR == 0)
					{
						dSecR = 5;
						if (minR == 0)
						{
							if (dMinR != 0)
							{
								minR = 9;
								dMinR--;
							}
							else
							{
								if (way.equals(MyProtocol.LEFT_TABLE))
								{
									JOptionPane.showMessageDialog(mp , "恭喜!!对方时间到,你赢了" , "胜利" ,
																JOptionPane.INFORMATION_MESSAGE);
								}
								else
								{
									JOptionPane.showMessageDialog(mp , "时间到,你输了" , "输了" ,
																JOptionPane.INFORMATION_MESSAGE);
								}
								winCountL++;
								reset();
							}
						}
						else
						{
							minR--;
						}
					}
					else
					{
						dSecR--;
					}
				}	
				else
				{
					secR--;
				}
				mp.repaint();			
			}
		});
	}

	//改变光标为手掌
	public void setCursor()
	{
		this.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}
	//恢复光标
	public void setDefaultCursor()
	{
		this.setCursor(Cursor.getDefaultCursor());
	}
	
	public void setSeat(String way , String name , String sex , String size)
	{
		this.otherWay = way;
		this.name = name;
		this.sex = sex;
		this.size = size;
		mp.repaint();
	}

	class MyPanel extends JPanel
	{
		public String setName(Graphics g, String name, int width)
		{
			FontMetrics fm = g.getFontMetrics();
			int w  = fm.stringWidth(name); //得到字符串的像素长度
			if (w <= width)
			{
				return name;
			}
			else if (w > width)
			{
				for (int i = name.length() - 1; i >= 0; i--)
				{
					String str = name.substring(0, i) + "...";
					if (fm.stringWidth(str) < width)
					{
						return str;
					}
				}
			}
			return name;
		}
		public void paint(Graphics g)
		{
			g.drawImage(gameImage , 0 , 0 ,null);
			g.setFont(new Font("" , Font.BOLD , 13));
			g.setColor(Color.WHITE); 
			
			if (isOtherReady)
			{
				if (way.equals(MyProtocol.LEFT_TABLE))
				{
					g.drawImage(whiteDisk , 655 , 173 , null);
				}
				else
				{
					g.drawImage(blackDisk , 24 , 173 , null);
				}
			}
			if (isReady)
			{
				if (way.equals(MyProtocol.LEFT_TABLE))
				{
					g.drawImage(blackDisk , 24 , 173 , null);
				}
				else
				{
					g.drawImage(whiteDisk , 655 , 173 , null);
				}
			}
			if (isReady && !playChess)
			{
				g.drawImage(ready , 93 , 631 , null);
			}
			if (isBegin && playChess)
			{
				g.drawImage(play , 93 , 630 , null);
			}
			
			String strName = setName(g, infoSave.getUserName(), (int)leftName.getWidth());
			FontMetrics fm = g.getFontMetrics();
			int width  = fm.stringWidth(strName); //得到字符串的像素长度
			int height = fm.getHeight();		  //得到字符串的像素高度
			if (way.equals(MyProtocol.LEFT_TABLE))
			{
				if (infoSave.getSex().equals("男"))
				{
					g.drawImage(maleSeatFull , 7 , 219 , null);
				}
				else
				{	
					g.drawImage(femaleSeatFull , 7 , 219 , null);
				}			
				g.drawString(strName , (int)leftName.getX() + (int)leftName.getWidth() / 2 - width / 2 ,
							 (int)leftName.getY() + height);
			}
			else
			{
				if (infoSave.getSex().equals("男"))
				{
					g.drawImage(maleSeatFull , 638 , 219 , null);
				}
				else
				{	
					g.drawImage(femaleSeatFull , 638 , 219 , null);
				}			
				g.drawString(strName , (int)rightName.getX() + (int)rightName.getWidth() / 2 - width / 2 ,
							 (int)rightName.getY() + height);
			}
			if (size.equals("2"))
			{
				String strN = setName(g, name , (int)leftName.getWidth());
				int w  = fm.stringWidth(strN); 
				int h = fm.getHeight();		  
				if (otherWay.equals(MyProtocol.LEFT_TABLE))
				{
					if (sex.equals("男"))
					{
						g.drawImage(maleSeatFull , 7 , 219 , null);
					}
					else
					{	
						g.drawImage(femaleSeatFull , 7 , 219 , null);
					}			
					g.drawString(strN , (int)leftName.getX() + (int)leftName.getWidth() / 2 - w / 2 ,
							 (int)leftName.getY() + h);
				}
				else
				{
					if (sex.equals("男"))
					{
						g.drawImage(maleSeatFull , 638 , 219 , null);
					}
					else
					{	
						g.drawImage(femaleSeatFull , 638 , 219 , null);
					}			
					g.drawString(strN , (int)rightName.getX() + (int)rightName.getWidth() / 2 - w / 2 ,
							 (int)rightName.getY() + h);
				}
			}
			if (startOver)
			{
				g.drawImage(start , 94 , 629 , null);
			}
			if (peaceOver)
			{
				g.drawImage(peace , 94 , 630 , null);
			}
			if (lostOver)
			{
				g.drawImage(lost , 94 , 630 , null);
			}

			g.drawString("总用时:" , 15 , 400);
			g.drawString("总用时:" , 645 , 400);
			g.drawString("10 : 00" , 25 , 415);
			g.drawString("10 : 00" , 655 , 415);
			g.drawString("倒计时:" , 15 , 430);		
			g.drawString("倒计时:" , 645 , 430);
			g.drawString("" + dMin + min + " : " + dSec + sec , 25 , 445);
			g.drawString("" + dMinR + minR + " : " + dSecR + secR , 655 , 445);
			g.drawString("比分:" + winCountL , 15 , 465);
			g.drawString("比分:" + winCountR , 645 , 465);
			
			for (int i = 0; i < 15 ; i++ )
			{
				for (int j = 0; j < 15 ; j++ )
				{
					if (chess[i][j] == 1)
					{
						g.drawImage(black , i * 35 + 97 , j * 35 + 91 , null);
					}
					if (chess[i][j] == 2)
					{
						g.drawImage(white , i * 35 + 97 , j * 35 + 91 , null);
					}
					if (curChess[i][j] == 1)
					{
						g.drawImage(curBlack , i * 35 + 97 , j * 35 + 91 , null);
					}
					if (curChess[i][j] == 2)
					{
						g.drawImage(curWhite , i * 35 + 97 , j * 35 + 91 , null);
					}
					if (select[i][j] == 1)
					{
						g.drawImage(selected, i*35 + 97, j*35 + 91 , null);
					}
				}
			}
		}
	}

	public void setSize(String size)
	{
		this.size = size;
		this.repaint();
	}
	public String getPlayerSize()
	{
		return this.size;
	}
	//----------根据对方的方位和坐标在自己这边画出对方下的棋----------//
	public void setXY(String way , int X , int Y)
	{
		if (way.equals(MyProtocol.LEFT_TABLE))
		{
			chess[X][Y] = 1;
			curChess[X][Y] = 1;
		}
		else
		{
			chess[X][Y] = 2;
			curChess[X][Y] = 2;
		}
		this.otherX = X;
		this.otherY = Y;
		if (step != 0)
		{
			curChess[preX][preY] = 0;
		}
		mp.repaint();
		win();
		if (isBegin)
		{
			setPlayChess();
		}	
	}
	//对方玩家准备好了
	public void setReady()
	{
		isOtherReady = true;
		if (isReady)
		{
			isBegin = true;
		}
		mp.repaint();
	}
	//对方玩家退出游戏了
	public void noReady()
	{
		isOtherReady = false;
		if (isBegin)
		{
			timerLeft.stop();
			timerRight.stop();
			JOptionPane.showMessageDialog(mp , "对手强行退出了!!" , "警告" ,JOptionPane.INFORMATION_MESSAGE);
			reset();
		}
		winCountL = 0;
		winCountR = 0;
		mp.repaint();
	}

	public void setPlayChess()
	{
		playChess = true;
		if (way.equals(MyProtocol.LEFT_TABLE))
		{
			timerLeft.start();
			timerRight.stop();
		}
		else
		{
			timerRight.start();
			timerLeft.stop();
		}
	}

	public void setBegin()
	{
		isBegin = true;
		mp.repaint();
	}

	public boolean isBegin()
	{
		return isBegin;
	}

	public void showPeace()
	{
		timerLeft.stop();
		timerRight.stop();
		int result = JOptionPane.showConfirmDialog(mp, "对手向你求和,是否同意?", "求和", 
													JOptionPane.YES_NO_OPTION );
		if (result == 0)
		{		
			reset();
			ps.println(MyProtocol.PEACE_AGREE + tableNumber + ":" + way + MyProtocol.PEACE_AGREE);
		}
		else
		{
			ps.println(MyProtocol.PEACE_REJECT + tableNumber + ":" + way + MyProtocol.PEACE_REJECT);
			if (way.equals(MyProtocol.LEFT_TABLE))
			{
				timerRight.start();
			}
			else
			{
				timerLeft.start();
			}
		}
	}

	public void showRejectPeace()
	{
		setPlayChess();
		JOptionPane.showMessageDialog(mp , "对方拒绝了你的求和要求!!" , "不同意和棋" ,
										JOptionPane.INFORMATION_MESSAGE);
	}

	public void showAgree()
	{
		//timerLeft.stop();
		//timerRight.stop();
		JOptionPane.showMessageDialog(mp , "对方同意了你的和棋要求,本局打平!!" , "同意和棋" ,
										JOptionPane.INFORMATION_MESSAGE);
		reset();
	}
	
	public void showLost(String way)
	{
		if (way.equals(MyProtocol.LEFT_TABLE))
		{
			timerLeft.stop();
			JOptionPane.showMessageDialog(mp , "黑棋认输了,恭喜你获胜!!" , "胜利" ,
											JOptionPane.INFORMATION_MESSAGE);
			winCountR++;
		}
		else
		{
			timerRight.stop();
			JOptionPane.showMessageDialog(mp , "白棋认输了,恭喜你获胜!!" , "胜利" ,
											JOptionPane.INFORMATION_MESSAGE);
			winCountL++;
		}
		reset();
	}

	public String getWay()
	{
		return way;
	}

	public void setTimerLeft()
	{
		timerLeft.start();
	}
}

⌨️ 快捷键说明

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