简单的推箱子游戏的实现

简单的推箱子游戏的实现

复制代码后请自行添加大小为51*51 pixels的图片素材;保存在material文件夹下;
代码如下:
/**
 * 简单的推箱子游戏
 */
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Sokoban extends JFrame{
    private static final long serialVersionUID = 1L;
    private Color colr= new Color(255,255,255);
    private BufferedImage wall,box,pusher,base,background;
    private int pos_x,pos_y,x_shift,y_shift,high,weight;

    public static void main(String[] args) {
        new Sokoban();
    }

    private int[][] map = new int[][]{
        {0,8,8,8,8,8,8,8,0,0},
        {0,8,0,0,0,0,0,8,8,8},
        {8,8,4,8,8,8,0,0,0,8},
        {8,2,0,0,4,0,0,4,0,8},
        {8,0,1,1,8,0,4,0,8,8},
        {8,8,1,1,8,0,0,0,8,0},
        {0,8,8,8,8,8,8,8,8,0}};

    private Container cont =this.getContentPane();

    public Sokoban(){
        high = map.length;
        weight = map[1].length;
        setSize(weight*51+10,high*51+25);
        setDefaultCloseOperation(3);
        setLocation(300,30);

        addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){

                    char key =e.getKeyChar();
                    if (key=='w'||key=='W'){    //up
                        x_shift=0;
                        y_shift=-1;
                        movement( y_shift, x_shift);
                    }
                    else if(key=='a'||key=='A'){    //left
                        x_shift=-1;
                        y_shift=0;
                        movement( y_shift, x_shift);
                    }
                    else if(key=='s'||key=='S'){    //down
                        x_shift=0;
                        y_shift=1;
                        movement( y_shift, x_shift);
                    }
                    else if(key=='d'||key=='D'){    //right
                        x_shift=1;
                        y_shift=0;
                        movement( y_shift, x_shift);
                    }
            }
        });
        setVisible(true);
    }

    public void paint(Graphics g){
        g = cont.getGraphics();
        g.setColor(colr);
        g.fillRect(0, 0, 600,548);

        try{
            wall= ImageIO.read(this.getClass().getResource("/material/brick.png"));
            box= ImageIO.read(this.getClass().getResource("/material/box.png"));
            pusher= ImageIO.read(this.getClass().getResource("/material/pusher.png"));
            base=ImageIO.read(this.getClass().getResource("/material/base.png"));
            background=ImageIO.read(this.getClass().getResource("/material/background.png"));
        }catch(IOException ex){
            ex.printStackTrace();
        }

        for(int i = 0;i<weight;i++){
            for(int j=0;j<high;j++){
                if (map[j][i]>=8)   g.drawImage(wall,51*i,51*j,null);
                else if (map[j][i]>=4)  g.drawImage(box,51*i,51*j,null);
                else if (map[j][i]>=2)  {
                    g.drawImage(pusher,51*i,51*j,null);
                    pos_y=j;
                    pos_x=i;
                }
                else if (map[j][i]==1)  g.drawImage(base,51*i,51*j,null);
            }
        }
    }

    public void isWin(){
        int sum_5 = 0; 
        for(int i = 0;i<weight;i++){
            for(int j=0;j<high;j++)
                if (map[j][i]==4) sum_5=sum_5+1;
        }

        if (sum_5==0){
        JOptionPane.showMessageDialog(null,"恭喜您获得了胜利!!");
        System.exit(0);
        }
    }

    public void movement(int y_shift,int x_shift){
        Graphics gr = cont.getGraphics();//获得当前的Panel中的Graphics;
        if(map[y_shift+pos_y][x_shift+pos_x]>=4&&map[y_shift+pos_y][x_shift+pos_x]<=5)
            //下个位置是箱子;
            {
                if(map[2*y_shift+pos_y][2*x_shift+pos_x]>=4) return; //下两个位置不是墙也不是箱子;

                    map[pos_y][pos_x]=map[pos_y][pos_x]-2;
                    map[y_shift+pos_y][x_shift+pos_x]=map[y_shift+pos_y][x_shift+pos_x]-2;
                    map[2*y_shift+pos_y][2*x_shift+pos_x]=map[2*y_shift+pos_y][2*x_shift+pos_x]+4;

                    if(map[pos_y][pos_x]==1)
                    gr.drawImage(base, 51*pos_x,51*pos_y, null);
                    else gr.drawImage(background, 51*pos_x,51*pos_y, null);

                    gr.drawImage(pusher, 51*(pos_x+x_shift),51*(pos_y+y_shift), null);
                    gr.drawImage(box, 51*(pos_x+2*x_shift),51*(pos_y+2*y_shift), null);

                    pos_x=x_shift+pos_x;pos_y=y_shift+pos_y;

            }
            else {  //下个位置不是箱子;
                if(map[y_shift+pos_y][x_shift+pos_x]>=8) return;    //下个位置也不是墙;

                map[pos_y][pos_x]=map[pos_y][pos_x]-2;
                map[y_shift+pos_y][x_shift+pos_x]=map[y_shift+pos_y][x_shift+pos_x]+2;

                if(map[pos_y][pos_x]==1)
                    gr.drawImage(base, 51*pos_x,51*pos_y, null);
                else gr.drawImage(background, 51*pos_x,51*pos_y, null);

                gr.drawImage(pusher, 51*(pos_x+x_shift),51*(pos_y+y_shift), null);
                pos_x=x_shift+pos_x;pos_y=y_shift+pos_y;
            }
        isWin();
    }
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值