import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class HRRoad {
public static void main(String[] args) {
HRFrame hr=new HRFrame();
hr.setBounds(400, 100, 216, 290);
hr.setDefaultCloseOperation(3);
hr.setVisible(true);
}
}
class HRFrame extends JFrame implements KeyListener{
private People [] ps=new People[10];
public HRFrame(){
this.setLayout(null);
ps[0]=new People("周", 0, 0, 50, 100);
ps[1]=new People("曹操", 50, 0, 100, 100);
ps[2]=new People("黄", 150, 0, 50, 100);
ps[3]=new People("张", 0, 100, 50, 100);
ps[4]=new People("关羽", 50, 100, 100, 50);
ps[5]=new People("刘", 150, 100, 50, 100);
ps[6]=new People("兵", 50, 150, 50, 50);
ps[7]=new People("兵", 100, 150, 50, 50);
ps[8]=new People("兵", 0, 200, 50, 50);
ps[9]=new People("兵", 150,200, 50, 50);
for(int i=0;i<ps.length;i++){
this.add(ps[i]);
ps[i].addKeyListener(this);
}
}
public boolean iswin(){//检测是否赢的游戏
if(ps[1].getX()==50&&ps[1].getY()==150){
return true;
}
return false;
}
public boolean hit(People pp){//检测是否可以移动
for(int i=0;i<ps.length;i++)
if(pp!=ps[i]){
if(pp.getBounds().intersects(ps[i].getBounds())
||(pp.getX()+pp.getWidth())>200||(pp.getY()+pp.getHeight())>250
||pp.getX()<0||pp.getY()<0){
return true;
}
}
return false;
}
public void keyPressed(KeyEvent e) {
int n=e.getKeyCode();
People c=(People)e.getComponent();
if(n==KeyEvent.VK_UP){
c.setY(c.getY()-50);
}else if(n==KeyEvent.VK_DOWN){
c.setY(c.getY()+50);
}else if(n==KeyEvent.VK_LEFT){
c.setX(c.getX()-50);
}else if(n==KeyEvent.VK_RIGHT){
c.setX(c.getX()+50);
}
c.setLocation(c.getX(), c.getY());
if(hit(c)){
if(n==KeyEvent.VK_UP){
c.setY(c.getY()+50);
}else if(n==KeyEvent.VK_DOWN){
c.setY(c.getY()-50);
}else if(n==KeyEvent.VK_LEFT){
c.setX(c.getX()+50);
}else if(n==KeyEvent.VK_RIGHT){
c.setX(c.getX()-50);
}
c.setLocation(c.getX(), c.getY());
}if(hit(c)){
JOptionPane.showMessageDialog(this, "你赢了!!!");
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class People extends JButton implements FocusListener{
private String name;
private int x;
private int y;
private int width;
private int height;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public People(String name,int x,int y,int width,int height){
setBounds(x, y, width, height);
this.setBackground(Color.gray);
this.setText(name);
this.name=name;
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.addFocusListener(this);
}
public void keyPressed(KeyEvent e) {
}
public void focusGained(FocusEvent e) {
this.setBackground(Color.red);
}
public void focusLost(FocusEvent e) {
this.setBackground(Color.gray);
}
}