java華容道代碼的思路 數(shù)字華容道java代碼

java編程的思路

java基本編程思路:

成都創(chuàng)新互聯(lián)公司,專注為中小企業(yè)提供官網(wǎng)建設、營銷型網(wǎng)站制作、成都響應式網(wǎng)站建設、展示型成都網(wǎng)站制作、成都做網(wǎng)站等服務,幫助中小企業(yè)通過網(wǎng)站體現(xiàn)價值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設與網(wǎng)站營銷推廣問題。

第一步:寫框架(內層和外層):

public class?類名{//外層

public static void main(String[ ] args){//內層

}

}

第二步:看題目中是否有“接收”“輸入”“錄入”等字眼,則導入util包,創(chuàng)建Scanner掃描儀對象:

import??java.util.Scanner;//寫在文件的第一行

Scanner??input = new Scanner(System.in);//寫在main方法中的第一行

第三步:編寫main方法中的主體代碼

A、接收、輸入、錄入:

System.out.println(“提示信息”);

數(shù)據(jù)類型??變量名=__掃描數(shù)據(jù)___;

說明:掃描不同類型的數(shù)據(jù),使用不同的掃描方法。以下介紹了常用的類型的掃描方法。

int型?????使用nextInt();

String型?????使用next ()或nextLine();

double型??????使用nextDouble();

B、計算:算術運算等

C、輸出:

System.out.println(輸出的內容);

或者

System.out.println(“提示內容:”+輸出的內容);

用java編程 華容道游戲

import java.awt.*;

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;kname.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(yh/2)

{

go(man,below);

}

if(yh/2)

{

go(man,above);

}

if(xw/2)

{

go(man,left);

}

if(xw/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;k10;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();

}

}

這是我們課本上的,顏色不一樣,其他的都差不多,不過是用awt組件寫的,你應該是要用swing寫的吧,照這個改改吧...

用java設計一個華容道游戲

import java.awt.*;

import java.awt.event.*;

public class MoveExample //主類

{

public static void main(String args[]) //定義主方法

{

new Hua_Rong_Road(); //創(chuàng)建對象

}

}

class Person extends Button implements FocusListener

{

int number;

Color c = new Color(128,128,128);

Person(int number,String s)//構造方法

{

super(s);//調用父類s的構造方法

setBackground(c);//設置組件的背景色

this.number = number;//調用當前的number

c = getBackground();

addFocusListener(this);//添加焦點事件監(jiān)聽器

}

public void focusGained(FocusEvent e)//焦點事件觸發(fā)

{

setBackground(Color.blue);

}

public void focusLost(FocusEvent e)//焦點事件失去

{

setBackground(c);

}

}

class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener

{

Person person[] = new Person[10];//person類的數(shù)組

Button left,right,above,below;

Button restart = new Button("重新開始");

public Hua_Rong_Road() //華容道的構造方法

{

init(); //初始化

setBounds(100,100,320,360);//設置窗口在屏幕上出現(xiàn)位置,和窗口大小

setVisible(true);//設置窗口可見

setResizable(true);//設置窗口可調節(jié)

validate();//刷新

addWindowListener( new WindowAdapter()//獲得窗口事件監(jiān)視器

{

public void windowClosing(WindowEvent e)//窗口正在被關閉時,窗口監(jiān)視器調用該方法

{

System.exit(0);

}

}

);

}

public void init()

{

setLayout(null);//設置默認布局

add(restart);//添加重新開始

restart.setBounds(100,320,120,25);//重新開始按鈕大小

restart.addActionListener(this);//事件源獲得監(jiān)視器

String name[] = {"曹操","關羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};

for(int k = 0;kname.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();//畫出游戲界面邊框,并用定義的left,right,above,below控制大小

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)//響應用戶按下方向光標的操作;用KeyEvent類中的getkeycode()判斷哪個鍵被按下

{

go(man,below); //go方法控制移動

}

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(yh/2)

{

go(man,below);

}

if(yh/2)

{

go(man,above);

}

if(xw/2)

{

go(man,left);

}

if(xw/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;k10;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();

}

}

當前標題:java華容道代碼的思路 數(shù)字華容道java代碼
分享路徑:http://muchs.cn/article14/dosdcge.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、軟件開發(fā)、做網(wǎng)站、面包屑導航網(wǎng)站改版、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設