java推箱子

java推箱子

 

 

package pushbox;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class gameframe extends JFrame implements ActionListener,MouseListener ,KeyListener{
    //主面板类
    final int WALL=1,BOX=2,BOXONEND=3,END=4,MANDOWN=5,MANLEFT=6,MANRIGHT=7,MANUP=8,GRASS=9,MANDOWNONEND=10,MANLEFTONEND=11,MANRIGHTONEND=12,
            MANUPONEND=13;
    private Image ibuffer;
    private Graphics gbuffer;        
    private int grade=0;
    
    private int row=7,column=7,leftX=0,leftY=0;
               //记载人的行号列号      地图左上角的行列号
    private int mapRow=0,mapColumn=0;
    private int width=0,height=0;//记载屏幕的大小!!
    private boolean acceptKey=true;
    //程序用到的图片
    private Image pic[]=null;
    private int[][]mymap=null;
    private ArrayList list=new ArrayList();
    Sound sound;
    //在构造方法中初始化游戏地图,清空悔棋信息列表,同时播放MIDI背景音乐。
    public gameframe() {
        super("推箱子");
        setSize(600,600);
        setResizable(false);
        setLocation(300,20);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cont=getContentPane();
        cont.setLayout(null);
        cont.setBackground(Color.black);
        //最初的13张图片
        getPic();
        width=this.getWidth();
        height=this.getHeight();
        initMap();//初始化grade地图,清空悔棋列表信息
        this.addKeyListener( this);
        this.addMouseListener(this);
        this.setFocusable(true);
        setVisible(true);
        sound=new Sound();
        sound.loadSound();
        
    
    }
    public void initMap() {
        mymap=getMap(grade);
        list.clear();
        getMapSizeAndPosition();
        getManPosition();
    }
    public void getManPosition() {
        for(int i=0;i<mymap.length;i++) {
            for(int j=0;j<mymap[0].length;j++)
                if(mymap[i][j]==MANDOWN||mymap[i][j]==MANDOWNONEND
                ||mymap[i][j]==MANUP||mymap[i][j]==MANUPONEND
                ||mymap[i][j]==MANLEFT||mymap[i][j]==MANLEFTONEND
                ||mymap[i][j]==MANRIGHT||mymap[i][j]==MANRIGHTONEND) {
                    row=i;
                    column=j;
                    break;
                }
            
        }
    }
    private void getMapSizeAndPosition() {//获取游戏区域大小及显示游戏的左上角坐标位置
        mapRow=mymap.length;
        mapColumn=mymap[0].length;
        leftX=(width-mymap[0].length*30)/2;
        leftY=(height-mymap.length*30)/2;
        
    }
    public void getPic() {
        pic=new Image[14];
        for(int i=0;i<13;i++) {
            pic[i]=Toolkit.getDefaultToolkit().getImage("D://test//photo//"+i+".jpg");
        }
    }
    public int grassOrEnd(int man) {//判断人所在的位置是目的地还是通道
        int result=GRASS;
        if(man==MANDOWNONEND||man==MANLEFTONEND||man==MANRIGHTONEND||man==MANUPONEND) {
            result=END;
            
        }
        return result;
    }
    private void moveUp() {//向上
        //上一位是墙
        if(mymap[row-1][column]==WALL)
            return;
        //上一位是box或boxonend,则需考虑上上一位
        if(mymap[row-1][column]==BOX||mymap[row-1][column]==BOXONEND) {
            //上上一位是目的地或通道
            if(mymap[row-2][column]==END||mymap[row-2][column]==GRASS) {
                map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
                list.add(currMap);
                int boxTemp=mymap[row-2][column]==END?BOXONEND:BOX;
                int manTemp=mymap[row-1][column]==BOX?MANUP:MANUPONEND;
                mymap[row-2][column]=boxTemp;
                mymap[row-1][column]=manTemp;
                mymap[row][column]=grassOrEnd(mymap[row][column]);
                //修改人离开后人的坐标
                row--;
            }
            
            
        }
        if(mymap[row-1][column]==GRASS) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row-1][column]=MANUP;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            row--;
        }
        if(mymap[row-1][column]==END) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row-1][column]=MANUPONEND;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            row--;}

        
    }private void moveDown() {//向下
        //下一位是墙
        if(mymap[row+1][column]==WALL)
            return;
        //下一位是box或boxonend,则需考虑下下一位
        if(mymap[row+1][column]==BOX||mymap[row+1][column]==BOXONEND) {
            //下下一位是目的地或通道
            if(mymap[row+2][column]==END||mymap[row+2][column]==GRASS) {
                map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
                list.add(currMap);
                int boxTemp=mymap[row+2][column]==END?BOXONEND:BOX;
                int manTemp=mymap[row+1][column]==BOX?MANUP:MANUPONEND;
                mymap[row+2][column]=boxTemp;
                mymap[row+1][column]=manTemp;
                mymap[row][column]=grassOrEnd(mymap[row][column]);
                //修改人离开后人的坐标
                row++;
            }
            
            
        }if(mymap[row+1][column]==GRASS) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row+1][column]=MANUP;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            row++;
        }
        if(mymap[row+1][column]==END) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row+1][column]=MANUPONEND;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            row++;}
        
        
    }private void moveLeft() {//向左
        //上一位是墙
        if(mymap[row][column-1]==WALL)
            return;
        //上一位是box或boxonend,则需考虑上上一位
        if(mymap[row][column-1]==BOX||mymap[row][column-1]==BOXONEND) {
            //上上一位是目的地或通道
            if(mymap[row][column-2]==END||mymap[row][column-2]==GRASS) {
                map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
                list.add(currMap);
                int boxTemp=mymap[row][column-2]==END?BOXONEND:BOX;
                int manTemp=mymap[row][column-1]==BOX?MANUP:MANUPONEND;
                mymap[row][column-2]=boxTemp;
                mymap[row][column-1]=manTemp;
                mymap[row][column]=grassOrEnd(mymap[row][column]);
                //修改人离开后人的坐标
                column--;
            }
            
            
        }if(mymap[row][column-1]==GRASS) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row][column-1]=MANUP;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            column--;
        }
        if(mymap[row][column-1]==END) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row][column-1]=MANUPONEND;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            column--;}
        
        
    }private void moveRight() {//向右
        //上一位是墙
        if(mymap[row][column+1]==WALL)
            return;
        //上一位是box或boxonend,则需考虑上上一位
        if(mymap[row][column+1]==BOX||mymap[row][column+1]==BOXONEND) {
            //上上一位是目的地或通道
            if(mymap[row][column+2]==END||mymap[row][column+2]==GRASS) {
                map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
                list.add(currMap);
                int boxTemp=mymap[row][column+2]==END?BOXONEND:BOX;
                int manTemp=mymap[row][column+1]==BOX?MANUP:MANUPONEND;
                mymap[row][column+2]=boxTemp;
                mymap[row][column+1]=manTemp;
                mymap[row][column]=grassOrEnd(mymap[row][column]);
                //修改人离开后人的坐标
                column++;
            }
            
            
        }if(mymap[row][column+1]==GRASS) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row][column+1]=MANUP;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            column++;
        }
        if(mymap[row][column+1]==END) {
            map1 currMap=new map1(row,column,mymap);//保存当前全部游戏的地图信息到ArrayList的list中,方便撤销!!
            list.add(currMap);
            mymap[row][column+1]=MANUPONEND;
            mymap[row][column]=grassOrEnd(mymap[row][column]);
            //修改人离开后人的坐标
            column++;}
        
        
    }
    public boolean isFinished() {
        for(int i=0;i<mapRow;i++)
            for(int j=0;j<mapColumn;j++)
                if(mymap[i][j]==END||mymap[i][j]==MANDOWNONEND||mymap[i][j]==MANUPONEND||mymap[i][j]==MANLEFTONEND||mymap[i][j]==MANRIGHTONEND) {
                    return  false;
                    
                }
        return true;
    }
    public void paint(Graphics g) {//绘制地图
        for(int i=0;i<mapRow;i++)
            for(int j=0;j<mapColumn;j++) {
                if(mymap[i][j]!=0)
                    g.drawImage(pic[mymap[i][j]],leftX+j*30,leftY+i*30,this);
                g.setColor(Color.RED);
                g.setFont(new Font("楷体_2312",Font.BOLD,30));
                g.drawString("现在是第", 150, 140);
                g.drawString(String.valueOf(grade+1), 310, 140);
                g.drawString("关", 360, 140);
            }}
        
        
        
    
    /*public void paint1(Graphics g) {
        if(ibuffer==null) {
            ibuffer=createImage(this.getSize().width,this.getSize().height);
            gbuffer=ibuffer.getGraphics();
        }gbuffer.setColor(getBackground());
        gbuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
        for(int i=0;i<mapRow;i++)
            for(int j=0;j<mapColumn;j++) {
                if(mymap[i][j]!=0) 
                  gbuffer.drawImage(pic[mymap[i][j]],leftX+j*30,leftY+i*30,this);
        gbuffer.setColor(Color.RED);
        gbuffer.setFont(new Font("楷体_2312",Font.BOLD,30));
        gbuffer.drawString("现在是第", 150, 140);
        gbuffer.drawString(String.valueOf(grade+1), 310, 140);
        gbuffer.drawString("关", 360, 140);
        g.drawImage(ibuffer, 0, 0, this);
        
    }}*/
    public void update(Graphics g) {
        paint(g);
    }
    public int getManX() {
        return row;
    }
    public int getManY() {
        return column;
    }
    public int getGrade() {
        return grade;
    }
    public int[][] getMap(int grade){
        return map.getMap(grade)
;    }
    //显示提示信息对话框!!
    public void DisplayToast(String str ) {
        JOptionPane.showMessageDialog(null, str,"提示",JOptionPane.ERROR_MESSAGE);
    }
    //撤销移动操作
    public void undo() {
        if(list.size()>0) {
            map1 priorMap=(map1)list.get(list.size()-1);
            mymap=priorMap.getMap();
            row=priorMap.getManX();
            column=priorMap.getManY();
            repaint();
            list.remove(list.size()-1);
        }
        else {
            DisplayToast("无法在撤退了!");
        }
    }
    //实现下一关的初始化,
    public void nextGrade() {
        if(grade>=map.getCount()-1) {
            DisplayToast("恭喜你完成了所有关卡!!!!");
            sound.mystop();
            acceptKey=false;
        }else {
            grade++;
            initMap();
            repaint();
            acceptKey=true;
        }
    }
    //实现上一关初始化
    public void priorGrade() {
        grade--;
        acceptKey=true;
        if(grade<0)
            grade=0;
        initMap();
        repaint();
        
    }
    //键盘相关事件
    
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
    
    
    public void mouseClicked(MouseEvent e) {
        if(e.getButton()==MouseEvent.BUTTON3)
            undo();
    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }




@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(e.getKeyCode()==KeyEvent.VK_UP) {
        moveUp();
        
    }
    if(e.getKeyCode()==KeyEvent.VK_DOWN) {
        moveDown();
        
    }if(e.getKeyCode()==KeyEvent.VK_LEFT) {
        moveLeft();
        
    }if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
        moveRight();
        
    }
    repaint();
     this.setFocusable(true);
    if(isFinished()) {
        //禁用按键
        acceptKey=false;
        if(grade==map.getCount())
            JOptionPane.showMessageDialog(this,"恭喜通过最后一关");
        else {
            String msg="恭喜您通过第"+grade+"关!!!\n是否要进入下一关?";
            int type=JOptionPane.YES_NO_OPTION;
            String title="pass";
            int choice=0;
            choice=JOptionPane.showConfirmDialog(null, msg,title,type);
            if(choice==1)
                System.exit(0);
            else if(choice==0) {
                //进入下一关
                acceptKey=true;
                nextGrade();
            }
        }
        
    }
    
}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    
}public static void main(String args[]) {
    new gameframe();
}
}
package pushbox;
import java.io.InputStream;
public class map {
    static int mymap[][][]= {
            {
    
            {0,0,1,1,1,0,0,0},
            {0,0,1,9,4,0,0,0},
            {0,0,1,9,2,1,1,1},
            {1,4,9,2,5,9,1,1},
            {1,1,1,9,2,1,0,0},
            {0,0,0,1,4,1,0,0},
            {0,0,0,1,1,1,0,0}
            },
            {
        {1,1,1,1,1,1,0,0,0},
        {1,9,9,5,1,1,0,0,0},
        {1,9,2,2,1,1,1,1,1},
        {1,9,2,9,1,1,1,4,1},
        {1,1,1,9,1,1,1,4,1},
        {0,1,1,9,9,9,9,4,1},
        {0,1,9,9,9,1,9,9,1},
        {0,1,9,9,9,1,1,1,1},
        {0,1,1,1,1,1,0,0,0}
            }
            };
       static int count=mymap.length;
       public static int[][] getMap(int grade){
           int temp[][];
           if(grade>=0&&grade<count)
               temp=mymap[grade];
           else
               temp=mymap[0];
           int row=temp.length;
           int column=temp[0].length;
           int[][] result=new int[row][column];
           for(int i=0;i<row;i++) {
               for(int j=0;j<column;j++) {
                   result[i][j]=temp[i][j];
                   
               }
           }
           return result;
           
           
       }
       public static int getCount() {
           return count;
       }


}
package pushbox;

public class map1 {
    int manX=0;int manY=0; int mymap[][];
    int grade;
    //此方法用于撤销,撤销只需要人的位置和地图的当前状态!!!
    public map1(int manX,int manY,int[][]mymap) {
        this.manX=manX;
        this.manY=manY;
        int row=mymap.length;
        int column=mymap[0].length;
        int temp[][]=new int[row][column];
        for(int i=0;i<row;i++)
            for(int j=0;j<column;j++) {
                temp[i][j]=mymap[i][j];
                
            }
        this.mymap=temp;
    }//次构造方法用于保存操作!!
    //回复地图是需要人的位置,地图的当前状态,和关卡数!!
    public map1(int manX,int manY,int[][]mymap,int grade) {
        this(manX,manY,mymap);
        this.grade=grade;
        
        
    }
    public int getManX() {
        return manX;
    }
    public int getManY() {
        return manY;
    }
    public int[][] getMap(){
        return mymap;
    }
    public int getGrade() {
        return grade;
    }
    

}
package pushbox;//播放背景音乐

import java.io.File;
import java.io.IOException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class Sound {
    String path=new String("D://test//music//");
    String file=new String("starwars.mid");
    Sequence seq;
    boolean sign;
    Sequencer midi;
    void loadSound() {
        try {
            seq=MidiSystem.getSequence(new File(path+file));
            midi=MidiSystem.getSequencer();
            midi.open();
            midi.setSequence(seq);
            midi.start();
            midi.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
        } catch (Exception e) {
            
            
        
            e.printStackTrace();
            sign=true;}
        
        
    }
        void mystop() {
            midi.stop();
            midi.close();
            sign=false;
        }
        boolean isplay() {
            return sign;
        }
        void setMusic(String e) {
            file=e;
        }
        
}

 

上一篇:The package java.awt is not accessible的解决方案


下一篇:事件监听