华容道小游戏求帮助!!!!!!!!!!!
本人今年学的Java,现在想学着编个简单的华容道小游戏,正确运行后就发现很多bug,希望哪位大神帮忙改下。大家一起来共同学习下!package HuaRongDao;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
class People extends Button implements FocusListener{
Rectangle rect=null;
int leftX,leftY;
int width,heigth;
String name;
int number;
People(int number,String name,int leftX,int leftY,int width,int heigth,Hua_Rong_Road road){
super(name);
this.name=name;
this.number=number;
this.leftX=leftX;
this.leftY=leftY;
this.width=width;
this.heigth=heigth;
rect=new Rectangle(leftX,leftY,width,heigth);
setBackground(Color.orange);
setBounds(leftX,leftY,width,heigth);
road.add(this);
addKeyListener(road);
addFocusListener(this);
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
setBackground(Color.CYAN);
}
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
setBackground(Color.orange);
}
}
class Hua_Rong_Road extends JPanel implements KeyListener{
Rectangle left,right,above,below;
People people[]=new People[10];
Hua_Rong_Road(){
setLayout(null);
people[0] = new People(0, "张辽",50,50,50,100,this);
people[1] = new People(1, "曹操",100,50,100,100,this);
people[2] = new People(2, "曹仁",200,50,50,100,this);
people[3] = new People(3, "张飞",50,150,50,100,this);
people[4] = new People(4, "关羽",100,150,100,50,this);
people[5] = new People(5, "刘备",200,150,50,100,this);
people[6] = new People(6, "兵",100,200,50,50,this);
people[7] = new People(7, "兵",150,200,50,50,this);
people[8] = new People(8, "兵",50,250,50,50,this);
people[9] = new People(9, "兵",200,250,50,50,this);
people[1].setForeground(Color.red);
left=new Rectangle(49,49,1,251);
right=new Rectangle(251,49,1,251);
above=new Rectangle(49,49,200,1);
below=new Rectangle(49,301,200,1);
setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.red);
g.fillRect(49,49,1,251);
g.fillRect(251,49,1,251);
g.fillRect(49,49,200,1);
g.fillRect(49,301,200,1);
g.drawString("单击对应的人物,然后按键盘上的方向箭头移动",50,30);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,280);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
People man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN){
man.leftY=man.leftY+50;
man.setLocation(man.leftX,man.leftY);
for(int i=0;i<10;i++){
if(man.rect.intersects(people[i].rect)&&man.number!=i){
man.leftY=man.leftY-50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
if(man.rect.intersects(below)){
man.leftY=man.leftY-50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP){
man.leftY=man.leftY-50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
for(int i=0;i<10;i++){
if(man.rect.intersects(people[i].rect)&&man.number!=i){
man.leftY=man.leftY+50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
if(man.rect.intersects(above)){
man.leftY=man.leftY+50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
man.leftX=man.leftX+50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
for(int i=0;i<10;i++){
if(man.rect.intersects(people[i].rect)&&man.number!=i){
man.leftX=man.leftX-50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
if(man.rect.intersects(right)){
man.leftX=man.leftX-50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
man.leftX=man.leftX-50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
for(int i=0;i<10;i++){
if(man.rect.intersects(people[i].rect)&&man.number!=i){
man.leftX=man.leftX+50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
if(man.rect.intersects(left)){
man.leftX=man.leftX+50;
man.setLocation(man.leftX,man.leftY);
man.rect.setLocation(man.leftX,man.leftY);
}
}
}
public static void main(String args[]){
JFrame f=new JFrame();
f.add("Center",new Hua_Rong_Road());
f.setVisible(true);
f.setBounds(5,5,400,400);
}
}