導航:首頁 > 編程語言 > 華容道java

華容道java

發布時間:2024-05-13 11:13:21

❶ 用java開發小游戲

我給你個華容道的游戲參照下吧。可以自己照著做下。多看例子想想就行了。

找個游戲做的不好,不要笑話啊。

import java.awt.*;
import javax.swing.JApplet.*;
import java.awt.event.*;
import javax.swing.*;
class People extends JButton implements FocusListener
{
Rectangle rect=null;
int left_x,left_y;//按鈕左上角坐標.
int width,height; //按鈕的寬和高.
String name;
int number;
public People(int number,String s,int x,int y,int w,int h,HuaRongRoad road)
{
super(s);
name=s;
this.number=number;
left_x=x;
left_y=y;
width=w;
height=h;
setBackground(Color.GREEN);
road.add(this);
addKeyListener(road);

setBounds(x,y,w,h);
addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(Color.GREEN);
}
}
public class HuaRongRoad extends JApplet implements KeyListener,ActionListener
{
People people[]=new People[10];
Rectangle left,right,above,below;//華容道的邊界
JButton restart=new JButton("restart");
public void init()
{

getContentPane().setLayout(null);
getContentPane().add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
getContentPane().setBackground(Color.white);

people[0]=new People(0,"曹操",154,54,200,200,this);
people[1]=new People(1,"關羽",154,254,200,100,this);
people[2]=new People(2,"張飛",54,254,100,200,this);
people[3]=new People(3,"劉備",354,254,100,200,this);
people[4]=new People(4,"張遼",54,54,100,200,this);
people[5]=new People(5,"曹仁",354,54,100,200,this);
people[6]=new People(6,"兵 ",54,454,100,100,this);
people[7]=new People(7,"兵 ",354,454,100,100,this);
people[8]=new People(8,"兵 ",154,354,100,100,this);
people[9]=new People(9,"兵 ",254,354,100,100,this);

people[9].requestFocus();

people[0].setForeground(Color.white);
left=new Rectangle(49,49,5,510);
right=new Rectangle(454,49,5,510);
above=new Rectangle(49,49,410,5);
below=new Rectangle(49,554,410,5);

}
public void paint(Graphics g)
{ //華容道的邊界
super.paint(g);
g.setColor(Color.cyan);
g.fillRect(49,49,5,510);
g.fillRect(454,49,5,510);
g.fillRect(49,49,410,5);
g.fillRect(49,554,410,5);
//
g.drawString("單擊,按方向箭頭移動",100,20);
g.setColor(Color.red);
g.drawString("曹操到達該位置",110,300);

}
public void keyPressed(KeyEvent e)
{
People man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
man.left_y=man.left_y+100; //向下前進50個單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現就退回50個單位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}

if(e.getKeyCode()==KeyEvent.VK_UP)
{
man.left_y=man.left_y-100; //向上前進50個單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現就退回50個單位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
man.left_x=man.left_x-100; //向左前進50個單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現就退回50個單位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
man.left_x=man.left_x+100; //向右進50個單位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判斷是否和其他人或邊界重疊,出現就退回50個單位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}

}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{
getContentPane().removeAll();

this.init();
}
}

❷ java鍗庡歸亾錛岀偣鍑昏彍鍗曟庝箞璺充竴涓妗嗗嚭鏉

鑿滃崟榪樺緱娣誨姞鑿滃崟欏癸紝緇欒彍鍗曢」娣誨姞ActionListener
JMenuItem introItem = new JMenuItem("浠嬬粛");
introItem.addActionListener(this);
introMenu.add(introItem);
鍦ㄤ簨浠跺勭悊鏂規硶actionPerformed涓璋冪敤 JOptionPane.showMessageDialog(this, "寮瑰嚭鐨勫瑰硅瘽妗嗕腑鏄劇ず鐨勪俊鎮");//this鏄浣犵殑紿楀彛瀵硅薄
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, "寮瑰嚭鐨勫瑰硅瘽妗嗕腑鏄劇ず鐨勪俊鎮");
}

❸ JAVA鍗庡歸亾紼嬪簭鎵╁睍錛屾兂鐢ㄥ浘鐗囨浛鎹渚嬪傛浌鎿嶉偅浜涙枃瀛楋紝寰楁庝箞淇鏀逛唬鐮侊紝鎬

Person闇緇ф壙JButton
鐒跺悗浣跨敤setIcon鏂規硶璁劇疆鍥劇墖
鎶奝erson鐨勬瀯閫犳柟娉曞啓鎴愬備笅錛屽浘鐗囦綅緗鍜宩ava鏂囦歡鍚屼竴涓鏂囦歡澶逛笅錛屽傛灉涓嶈岋紝鍙鐪嬩竴涓媝ath鐨勫兼槸浠涔堬紝鏍規嵁鍏跺兼憜鏀懼浘鐗
Person(int number, String s) {
super(s);
this.number = number;
setFont(new Font("瀹嬩綋", Font.CENTER_BASELINE, 14));
setBackground(Color.pink);
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
Icon defaultIcon = new ImageIcon(path+"/a.jpg") ;
this.setIcon(defaultIcon );
}

鑻ュ皢Person(int number, String s) 鍐欐垚
Person(int number, String s,String pic) {
super(s);
this.number = number;
setFont(new Font("瀹嬩綋", Font.CENTER_BASELINE, 14));
setBackground(Color.pink);
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
Icon defaultIcon = new ImageIcon(path+"/"+pic) ;
this.setIcon(defaultIcon );
}
鍒涘緩鏃朵紶鍏ュ浘鐗囧悕縐板嵆鍙

閱讀全文

與華容道java相關的資料

熱點內容
什麼編譯器可以帶c11函數 瀏覽:18
如何理解程序員對自己電腦的感情 瀏覽:525
什麼是簡訊app 瀏覽:752
我的世界伺服器啟動器下載地址 瀏覽:790
雲伺服器公ip和內ip 瀏覽:948
手機淘寶app授權在哪裡 瀏覽:472
匯編程序的任務 瀏覽:973
dji編程玩具 瀏覽:21
dcs伺服器異常現象是什麼 瀏覽:201
java中的布局 瀏覽:702
單片機作業三 瀏覽:161
古代分數運演算法則 瀏覽:154
電腦大文件夾查找方法 瀏覽:938
什麼app可以買國外衣服 瀏覽:385
媽媽吃了命令葯丸 瀏覽:714
男的進國企做程序員 瀏覽:994
程序員的數學線性代數 瀏覽:373
冰箱壓縮機啟動器盒怎麼拆 瀏覽:443
雪崩pdf 瀏覽:952
桂林銀行app如何查詢積分和等級 瀏覽:285