简单的推箱子游戏的实现
复制代码后请自行添加大小为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'){
x_shift=0;
y_shift=-1;
movement( y_shift, x_shift);
}
else if(key=='a'||key=='A'){
x_shift=-1;
y_shift=0;
movement( y_shift, x_shift);
}
else if(key=='s'||key=='S'){
x_shift=0;
y_shift=1;
movement( y_shift, x_shift);
}
else if(key=='d'||key=='D'){
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;ifor(int j=0;jif (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;ifor(int j=0;jif (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();
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();
}
}