❶ 用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 );
}
鍒涘缓镞朵紶鍏ュ浘鐗囧悕绉板嵆鍙