?? monopolygame.java
字號:
if(i==0||i==7)
{
square[i]=new PublicSquare(squareName[i],500,5000);
}
else
square[i]=new PrivateSquare(squareName[i],100,1000);
square[i].setBounds(x1*81, 0, 81, 60);
getContentPane().add(square[i]);
}
if(i>7&&i<=12)
{
y2+=1;
if(i==10)
{
square[i]=new PublicSquare(squareName[i],500,5000);
}
else
square[i]=new PrivateSquare(squareName[i],150,1500);
square[i].setBounds(7*81, y2*60, 81, 60);
getContentPane().add(square[i]);
}
if(i>12&&i<=20)
{
x2-=1;
if(i==13||i==20)
{
square[i]=new PublicSquare(squareName[i],500,5000);
}
else
square[i]=new PrivateSquare(squareName[i],100,1000);
square[i].setBounds(x2*81, 6*60, 81, 60);
getContentPane().add(square[i]);
}
if(i>20&&i<26)
{
y1-=1;
if(i==23)
{
square[i]=new PublicSquare(squareName[i],500,5000);
}
else
square[i]=new PrivateSquare(squareName[i],200,2000);
square[i].setBounds(0, y1*60, 81, 60);
getContentPane().add(square[i]);
}
}
//初始化當前玩家信息
playinfo=new JLabel();
playinfo.setText("當前玩家為: "+player[(j)%playnum].name+" "
+"現有現金:"+player[(j)%playnum].cash+" 存款:"+player[(j)%playnum].saving);
playinfo.setBounds(300,450,400,20);//設置字體顯示屬性
Font playfont=new Font(null,Font.BOLD,15);
playinfo.setFont(playfont);
playinfo.setForeground(Color.RED);
getContentPane().add(playinfo);
setResizable(false);
}
//移動玩家
private class moveperson extends Thread
{
int dicetotal,i;
public moveperson(int dicetotal,int i)
{
this.dicetotal=dicetotal;
this.i=i;
}
public void run()
{
for(int k=0;k<dicetotal;k++)
{
player[j].preposition=(++player[j].preposition)%26;
if((player[j].preposition)%26>=0&&(player[j].preposition)%26<=7)
{
person[i].setBounds(player[j].preposition*81,0,81,60);
}
else if((player[j].preposition)%26>=8&&(player[j].preposition)%26<=12)
{
person[i].setBounds(7*81,(player[j].preposition-7)*60,81,60);
}
else if((player[j].preposition)%26>=13&&(player[j].preposition)%26<=20)
{
person[i].setBounds((20-player[j].preposition)*81,6*60,81,60);
}
else if((player[j].preposition)%26>=21&&(player[j].preposition)%26<=25)
{
person[i].setBounds(0,(26-player[j].preposition)*60,81,60);
}
if(square[player[i].preposition].name.equals("Bank"))
{
int action=JOptionPane.showConfirmDialog(null, "銀行門口,要進行銀行業務嗎?",
"提示",JOptionPane.YES_NO_OPTION );
if(action==0)//辦理業務
{
String num=JOptionPane.showInputDialog(null, "選擇操作:0取款,1存款!", "Deal Saving", JOptionPane.YES_NO_CANCEL_OPTION);
int op=Integer.parseInt(num);
{
if(op==0)//取款
{
String savenumstr="0";//防止玩家取消
savenumstr=JOptionPane.showInputDialog(null, "輸入要存的數量:", "Saving", JOptionPane.YES_NO_CANCEL_OPTION);
int savenum=Integer.parseInt(savenumstr);
if(player[i].cash>=savenum)
{
player[i].cash-=savenum;
player[i].saving+=savenum;
}
else
{
JOptionPane.showMessageDialog(null, "現金不夠!","infomation",
JOptionPane.INFORMATION_MESSAGE);
}
}
else//存款
{
String savenumstr="0";//防止玩家取消
savenumstr=JOptionPane.showInputDialog(null, "輸入要取的數量:", "Withdraw", JOptionPane.YES_NO_CANCEL_OPTION);
int savenum=Integer.parseInt(savenumstr);
if(player[i].saving>=savenum)
{
player[i].cash+=savenum;
player[i].saving-=savenum;
}
else
{
JOptionPane.showMessageDialog(null, "存款不夠!","infomation",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
if(player[j].preposition==player[j].position)
{
deal();
}
try {
Thread.sleep(300);
} catch (InterruptedException e)
{
}
}
}
}
private void deal()
{
int isnull = 1;
int lucky=0;
if(square[player[j].position].ownername==null||square[player[j].position].ownername.equals("public"))
{
if(square[player[j].position].name.endsWith("Go"))
{
}
else if(square[player[j].position].name.endsWith("Bank"))
{
JOptionPane.showMessageDialog(null, "得到銀行貸款1000元!","Congratulate",
JOptionPane.INFORMATION_MESSAGE);
player[j].cash+=1000;
}
else if(square[player[j].position].name.endsWith("Prison"))
{
player[j].delay=(int) (Math.random()*5+1);
JOptionPane.showMessageDialog(null, "進入監獄"+player[j].delay+"天!","infomation",
JOptionPane.INFORMATION_MESSAGE);
}
else if(square[player[j].position].name.endsWith("Lucky"))
{
lucky=(int) (Math.random()*2+1);
if(lucky==1)
{
JOptionPane.showMessageDialog(null, "撿到500元!","Congratulation",
JOptionPane.INFORMATION_MESSAGE);
player[j].cash+=500;
}
else
{
JOptionPane.showMessageDialog(null, "罰款500元!","Bad Lucky!",
JOptionPane.INFORMATION_MESSAGE);
player[j].cash-=100;
}
}
else
{
isnull=JOptionPane.showConfirmDialog(null, "該方格沒人擁有!是否購買?價格:"
+square[player[j].position].price+"租金:"+square[player[j].position].rent,
"提示",JOptionPane.YES_NO_OPTION );
}
}
else//支付租金
{
if(square[player[j].position].ownername.equals(playername[j]))
{
//do nothing
}
else
{
//找到方格擁有者
int owner = 0;
for(int num=0;num<playnum;num++)
{
if(player[num].name.equals(square[player[j].position].ownername))
{
owner=num;
break;
}
}
if(player[owner].delay>0)
{
JOptionPane.showMessageDialog(null,"該方格擁有者"+square[player[j].position].ownername
+"正在監獄中,免付租金!","Lucky",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "該方格為"+square[player[j].position].ownername
+"擁有!支付租金:"+square[player[j].position].rent);
//當前玩家金錢或存款減少
if(player[j].cash>square[player[j].position].rent)
{
player[j].cash-=square[player[j].position].rent;
}
else
{
player[j].saving=player[j].saving-(square[player[j].position].rent-player[j].cash);
player[j].cash=0;
}
//方格擁有者金錢增加
player[owner].cash+=square[player[j].position].rent;
}
}
}
//確定購買
if(isnull==0&&(square[player[j].position].rent<player[j].cash))
{
buy();
}
else
{
JOptionPane.showMessageDialog(null,"現金不足!","Bad Lucky",JOptionPane.INFORMATION_MESSAGE);
}
//資金小于0,玩家輸了,退出游戲
if(player[j].cash<0&&player[j].saving<0)
{
JOptionPane.showMessageDialog(null,playername[j]+"輸了,游戲結束!","sorry",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
j=(j+1)%playnum;
//更新玩家信息
playinfo.setText("當前玩家為: "+player[(j)%playnum].name+" "
+"現有現金:"+player[(j)%playnum].cash+" 存款:"+player[(j)%playnum].saving);
}
//購買方塊
private void buy()
{
player[j].cash-=square[player[j].position].price;
square[player[j].position].ownername=player[j].name;
square[player[j].position].label.setBorder(BorderFactory.createLineBorder(color[j]));
showhead();
}
private void showhead()
{
for(int k=1;k<=2;k++)//有沒有更好的方法使頭像保持在前?
{
if(player[j].position==0)
player[j].position=25;
else
player[j].position--;
if((player[j].position)%26>=0&&(player[j].position)%26<=7)
{
person[j].setBounds(player[j].position*81,0,81,60);
}
else if((player[j].position)%26>=8&&(player[j].position)%26<=12)
{
person[j].setBounds(7*81,(player[j].position-7)*60,81,60);
}
else if((player[j].position)%26>=13&&(player[j].position)%26<=20)
{
person[j].setBounds((20-player[j].position)*81,6*60,81,60);
}
else if((player[j].position)%26>=21&&(player[j].position)%26<=25)
{
person[j].setBounds(0,(26-player[j].position)*60,81,60);
}
player[j].position=(player[j].position+2)%26;
}
if(player[j].position<=1)
player[j].position+=24;
else
player[j].position-=2;
}
//擲色子函數
public void dice()
{
dicenum1=(int)(Math.random()*6+1);
dicenum2=(int)(Math.random()*6+1);
dicetotal=dicenum1+dicenum2;
}
//得到色子總點數
public int getDicetotal()
{
return dicetotal;
}
public static void main(String args[])
{
MonopolyGame game = new MonopolyGame();
//設置界面大小
game.setBounds(200, 100, 653, 578);
game.setResizable(false);
game.setVisible(true);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -