即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

极简的推箱子游戏的实现

编程语言 qq_36855981 30℃ 0评论
本文目录
[隐藏]

1.简单的推箱子游戏的实现

复制代码后请自行添加大小为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;

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

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

    private Container cont =this.getContentPane();

    public Sokoban(){
        setSize(525,548);
        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/backgound.png"));
        }catch(IOException ex){
            ex.printStackTrace();
        }

        for(int i = 0;i<10;i++){
            for(int j=0;j<10;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<10;i++){
            for(int j=0;j<10;j++)
                if (map[j][i]==5) sum_5=sum_5+1;
        }

        if (sum_5==3){
        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]>=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;
                    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();
    }
}

使用键盘的WASD操作方向;

转载请注明:CodingBlog » 极简的推箱子游戏的实现

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情