這是一個java 棋類遊戲的原程序代碼,覺得不錯就拿出來跟大家分享:
import java.awt.event.*;
class ChessPad extends Panel implements MouseListener,ActionListener
{ int x=-1,y=-1, 棋子顏色=1;
Button button=new Button("重新開局");
TextField text_1=new TextField("請黑棋下子"),
text_2=new TextField();
ChessPad()
{ setSize(440,440);
setLayout(null);setBackground(Color.orange);
addMouseListener(this);add(button);button.setBounds(10,5,60,26);
button.addActionListener(this);
add(text_1);text_1.setBounds(90,5,90,24);
add(text_2);text_2.setBounds(290,5,90,24);
text_1.setEditable(false);text_2.setEditable(false);
}
public void paint(Graphics g)
{ for(int i=40;i<=380;i=i+20)
{ g.drawLine(40,i,400,i);
}
g.drawLine(40,400,400,400);
for(int j=40;j<=380;j=j+20)
{ g.drawLine(j,40,j,400);
}
g.drawLine(400,40,400,400);
g.fillOval(97,97,6,6); g.fillOval(337,97,6,6);
g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);
g.fillOval(217,217,6,6);
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON1_MASK)
ChessPoint_black chesspoint_black=new ChessPoint_black(this);
ChessPoint_white chesspoint_white=new ChessPoint_white(this);
int a=(x+10)/20,b=(y+10)/20;
if(x/20<2||y/20<2||x/20>19||y/20>19)
{}
else
{
if(棋子顏色==1)
{ this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-10,b*20-10,20,20);
棋子顏色=棋子顏色*(-1);
text_2.setText("請白棋下子");
text_1.setText("");
}
else if(棋子顏色==-1)
{ this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-10,b*20-10,20,20);
棋子顏色=棋子顏色*(-1);
text_1.setText("請黑棋下子");
text_2.setText("");
}
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{ this.removeAll();棋子顏色=1;
add(button);button.setBounds(10,5,60,26);
add(text_1);text_1.setBounds(90,5,90,24);
text_2.setText("");text_1.setText("請黑棋下子");
add(text_2);text_2.setBounds(290,5,90,24);
}
}
class ChessPoint_black extends Canvas implements MouseListener
ChessPoint_black(ChessPad p)
{ setSize(20,20);chesspad=p; addMouseListener(this);
}
public void paint(Graphics g)
{ g.setColor(Color.black);g.fillOval(0,0,20,20);
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{ chesspad.remove(this);
chesspad.棋子顏色=1;
chesspad.text_2.setText("");chesspad.text_1.setText("請黑棋下子");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()>=2)
chesspad.remove(this);
}
}
class ChessPoint_white extends Canvas implements MouseListener
{ ChessPad chesspad=null;
ChessPoint_white(ChessPad p)
{ setSize(20,20);addMouseListener(this);
chesspad=p;
}
public void paint(Graphics g)
{ g.setColor(Color.white);g.fillOval(0,0,20,20);
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{ chesspad.remove(this);chesspad.棋子顏色=-1;
chesspad.text_2.setText("請白棋下子"); chesspad.text_1.setText("");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()>=2)
chesspad.remove(this);
}
}
public class Chess extends Frame
{ ChessPad chesspad=new ChessPad();
Chess()
{ setVisible(true);
setLayout(null);
Label label=
new Label("單擊左鍵下棋子,雙擊喫棋子,用右鍵單擊棋子悔棋",Label.CENTER);
add(label);label.setBounds(70,55,440,26);
label.setBackground(Color.orange);
add(chesspad);chesspad.setBounds(70,90,440,440);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
pack();setSize(600,550);
}
public static void main(String args[])
{ Chess chess=new Chess();
}
}
java 華容道遊戲的簡單源代碼,覺得不錯就拿出來跟大家分享一下.
import java.awt.event.*;
public class MoveExample
{ public static void main(String args[])
{ new Hua_Rong_Road();
}
}
class Person extends Button
implements FocusListener
{ int number;
Color c=new Color(255,245,170);
Person(int number,String s)
{ super(s);
setBackground(c);
this.number=number;
c=getBackground();
addFocusListener(this);
}
public void focusGained(FocusEvent e)
{ setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{ setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{ Person person[]=new Person[10];
Button left,right,above,below;
Button restart=new Button("重新開始");
public Hua_Rong_Road()
{ init();
setBounds(100,100,320,360);
setVisible(true);
validate();
addWindowListener( new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
}
public void init()
{ setLayout(null);
add(restart);
restart.setBounds(100,320,120,25);
restart.addActionListener(this);
String name[]={"曹操","關羽","張飛","劉備","周瑜","黃忠","兵","兵","兵","兵"};
for(int k=0;k<name.length;k++)
{ person[k]=new Person(k,name[k]);
person[k].addMouseListener(this);
person[k].addKeyListener(this);
add(person[k]);
}
person[0].setBounds(104,54,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54, 154,50,100);
person[3].setBounds(204,154,50,100);
person[4].setBounds(54, 54, 50,100);
person[5].setBounds(204, 54, 50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
person[9].requestFocus();
left=new Button(); right=new Button();
above=new Button(); below=new Button();
add(left); add(right);
add(above); add(below);
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{ Person man=(Person)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ go(man,below);
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{ go(man,above);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ go(man,left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{ go(man,right);
}
}
public void mousePressed(MouseEvent e)
{ Person man=(Person)e.getSource();
int x=-1,y=-1;
x=e.getX();
y=e.getY();
int w=man.getBounds().width;
int h=man.getBounds().height;
if(y>h/2)
{ go(man,below);
}
if(y<h/2)
{ go(man,above);
}
if(x<w/2)
{ go(man,left);
}
if(x>w/2)
{ go(man,right);
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void go(Person man,Button direction)
{ boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below)
y=y+50;
else if(direction==above)
y=y-50;
else if(direction==left)
x=x-50;
else if(direction==right)
x=x+50;
manRect.setLocation(x,y);
Rectangle directionRect=direction.getBounds();
for(int k=0;k<10;k++)
{ Rectangle personRect=person[k].getBounds();
if((manRect.intersects(personRect))&&(man.number!=k))
{ move=false;
}
}
if(manRect.intersects(directionRect))
{ move=false;
}
if(move==true)
{ man.setLocation(x,y);
}
}
public void actionPerformed(ActionEvent e)
{ dispose();
new Hua_Rong_Road();
}
}