java 代码的推箱子游戏

发布时间:2023年12月23日

package web;

import javax.swing.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class GameGUI extends JFrame {
    private JPanel gamePanel;

    private static final int CELL_SIZE = 50;
    private static final int MAX = 10;

    private static final int KongBai = 0;
    private static final int Kong = 1;
    private static final int Ren = 2;
    private static final int Xiang = 3;
    private static final int Qiang = 4;
    private static final int Jin = 5;

    private static final String KONGBAI ="  ";
    private static final String KONG ="空";
    private static final String REN ="人";
    private static final String XIANG ="箱";
    private static final String QING ="墙";
    private static final String JIN ="进";

    private int Ren_i=1, Ren_j=1;
    private int[][] Arr;
    boolean ESC = false;

    public GameGUI() {
        setTitle("推箱子游戏");
        setSize(MAX * CELL_SIZE, MAX * CELL_SIZE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        gamePanel = new JPanel() {
        	@Override
        	protected void paintComponent(Graphics g) {
        		 super.paintComponent(g);
            	System.out.println("2");                
                Font font = new Font("宋体", Font.PLAIN, 50);
                g.setFont(font);
                g.setColor(Color.BLACK);
                
                if(isOver()||ESC) {
                	g.drawString("游戏结束", 3*CELL_SIZE, 4*CELL_SIZE);
                	return ;
                }
                    
                // 在这里绘制游戏地图和角色
                for (int i = 0; i < MAX; i++) {
                    for (int j = 0; j < MAX; j++) {
                    	g.drawRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                    	
                        int cellType = Arr[i][j];
                        switch (cellType) {
                            // 绘制不同类型的格子
                        case KongBai:
                           // System.out.print(KONGBAI);
                        	g.setColor(Color.RED);
                        	g.fillRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                        	 g.setColor(Color.BLACK);
                            g.drawString(KONGBAI, i*CELL_SIZE, j*CELL_SIZE);
                            break;
                        case Kong:
                        	// System.out.print(KONG);
                        	g.setColor(Color.GREEN);
                        	g.fillRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                        	 g.setColor(Color.BLACK);
                            g.drawString(KONG, i*CELL_SIZE, j*CELL_SIZE);
                            break;
                        case Ren:
                        	// System.out.print(REN);
                        	g.setColor(Color.YELLOW);
                        	g.fillRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                        	 g.setColor(Color.BLACK);
                            g.drawString(REN, i*CELL_SIZE, j*CELL_SIZE);
                            break;
                        case Xiang:
                        	// System.out.print(XIANG);
                        	g.setColor(Color.MAGENTA);
                        	g.fillRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                        	 g.setColor(Color.BLACK);
                            g.drawString(XIANG, i*CELL_SIZE, j*CELL_SIZE);
                            break;
                        case Qiang:
                        	// System.out.print(QING);
                        	g.setColor(Color.BLUE);
                        	g.fillRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                        	 g.setColor(Color.BLACK);
                            g.drawString(QING, i*CELL_SIZE, j*CELL_SIZE);
                            break;
                        case Jin:
                        	// System.out.print(JIN);
                        	g.setColor(Color.CYAN);
                        	g.fillRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                        	 g.setColor(Color.BLACK);
                            g.drawString(JIN, i*CELL_SIZE, j*CELL_SIZE);
                            break;
                        case Jin+Ren:
                        	// System.out.print(REN);
                        	g.setColor(Color.LIGHT_GRAY);
                    	g.fillRect(i*CELL_SIZE, (j-1)*CELL_SIZE, CELL_SIZE, CELL_SIZE);
                    	 g.setColor(Color.BLACK);
                        g.drawString(REN, i*CELL_SIZE, j*CELL_SIZE);
                            break;
                        
                        }
                        // 绘制角色
                    }
                }
            }
        };
        add(gamePanel);

        Arr = new int[MAX][MAX];
        
        Init();

        gamePanel.setFocusable(true);
        gamePanel.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {}

            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                // 根据按键来移动角色
                switch (key) {
                	case KeyEvent.VK_ESCAPE:
                		ESC=true;
                		break;
                    case KeyEvent.VK_LEFT:
                        // 处理向上移动
                    	if(Arr[Ren_i-1][Ren_j]==KongBai || Arr[Ren_i-1][Ren_j] ==Jin ){
                            Arr[Ren_i][Ren_j] -= Ren;//Arr[Ren_i][Ren_j] = KongBai;
                            Arr[Ren_i-1][Ren_j] +=Ren;//Arr[Ren_i-1][Ren_j] =Ren;
                            Ren_i-=1;
                        }else if(Arr[Ren_i-1][Ren_j]==Xiang){//如果人前面是箱子
                            //分析箱子是否能被推动
                            if(Arr[Ren_i-2][Ren_j]==KongBai || Arr[Ren_i-2][Ren_j]==Kong){//箱子前面是空白,或空
                                Arr[Ren_i-2][Ren_j]=(Arr[Ren_i-2][Ren_j]==KongBai?Xiang:Jin);//改箱前面为箱或进
                                Arr[Ren_i][Ren_j] = KongBai;
                                Arr[Ren_i-1][Ren_j] =Ren;
                                Ren_i-=1;
                            }
                        }
                    	
                        break;
                    case KeyEvent.VK_RIGHT:
                        // 处理向下移动
                    	 if(Arr[Ren_i+1][Ren_j]==KongBai || Arr[Ren_i+1][Ren_j]==Jin){
                             Arr[Ren_i][Ren_j] -= Ren;
                             Arr[Ren_i+1][Ren_j] +=Ren;
                             Ren_i+=1;
                         }else if(Arr[Ren_i+1][Ren_j]==Xiang){//如果人前面是箱子
                             //分析箱子是否能被推动
                             if(Arr[Ren_i+2][Ren_j]==KongBai || Arr[Ren_i+2][Ren_j]==Kong){//箱子前面是空白,或空
                                 Arr[Ren_i+2][Ren_j]=(Arr[Ren_i+2][Ren_j]==KongBai?Xiang:Jin);//改箱前面为箱或进
                                 Arr[Ren_i][Ren_j] = KongBai;
                                 Arr[Ren_i+1][Ren_j] =Ren;
                                 Ren_i+=1;
                             }
                         }
                        break;
                    case KeyEvent.VK_UP:
                        // 处理向左移动
                    	 if(Arr[Ren_i][Ren_j-1]==KongBai || Arr[Ren_i][Ren_j-1]==Jin){
                             Arr[Ren_i][Ren_j] -= Ren;
                             Arr[Ren_i][Ren_j-1] +=Ren;
                             Ren_j-=1;
                         } if(Arr[Ren_i][Ren_j-1]==Xiang){//如果人前面是箱子
                         //分析箱子是否能被推动
                         if(Arr[Ren_i][Ren_j-2]==KongBai || Arr[Ren_i][Ren_j-2]==Kong){//箱子前面是空白,或空
                             Arr[Ren_i][Ren_j-2]=(Arr[Ren_i][Ren_j-2]==KongBai?Xiang:Jin);//改箱前面为箱或进
                             Arr[Ren_i][Ren_j] = KongBai;
                             Arr[Ren_i][Ren_j-1] =Ren;
                             Ren_j-=1;
                         }
                     }
                        break;
                    case KeyEvent.VK_DOWN:
                        // 处理向右移动
                    	 if(Arr[Ren_i][Ren_j+1]==KongBai || Arr[Ren_i][Ren_j+1]==Jin ){
                             Arr[Ren_i][Ren_j] -= Ren;
                             Arr[Ren_i][Ren_j+1] +=Ren;
                             Ren_j+=1;
                         }if(Arr[Ren_i][Ren_j+1]==Xiang){//如果人前面是箱子
                         //分析箱子是否能被推动
                         if(Arr[Ren_i][Ren_j+2]==KongBai || Arr[Ren_i][Ren_j+2]==Kong){//箱子前面是空白,或空
                             Arr[Ren_i][Ren_j+2]=(Arr[Ren_i][Ren_j+2]==KongBai?Xiang:Jin);//改箱前面为箱或进
                             Arr[Ren_i][Ren_j] = KongBai;
                             Arr[Ren_i][Ren_j+1] =Ren;
                             Ren_j+=1;
                         }
                     }
                        break;
                }
                // 刷新界面
                System.out.println("gamePanel.repaint();");
                gamePanel.repaint();
            }

            @Override
            public void keyReleased(KeyEvent e) {}
        });
    }
    
    static int GetNum(){
        int min =  2;
        int max = 7;
        return (int)(Math.random()*(max-min+1))+min;

    }
    

    private void Init() {
        // 初始化地图
    	 for(int i=0;i<MAX;i++){
             Arr[i] =  new int[MAX];
             for (int j = 0; j < MAX; j++) {
                     Arr[i][j]=i==0||j==0||i==MAX-1||j==MAX-1?Qiang:KongBai;
             }
    	 }
    	 Arr[Ren_i][Ren_j] = Ren;

         //随机产生4个箱 和 空  在 游戏里

         //4个箱字
         int i;
         i=0;
         while(i<4){
             int a=GetNum(),b=GetNum();
             if(Arr[a][b]==KongBai){
                 Arr[a][b] = Xiang;
                 i+=1;
             }
         }

         //4个空字

         i=0;
         while(i<4){
             int a=GetNum(),b=GetNum();
             if(Arr[a][b]==KongBai){
                 Arr[a][b] = Kong;
                 i+=1;
             }
         }
    }
    
    private boolean isOver(){
        for(int i=0;i<MAX;i++)
            for(int j=0;j<MAX;j++)
                if(Arr[i][j]==Xiang||Arr[i][j]==Kong)
                    return false;
        return true;
    }

    

    @Override
    public void paint(Graphics g) {
    	System.out.println("1");
        super.paint(g);
        if(1==1)
        	return ;
        Font font = new Font("宋体", Font.PLAIN, 50);
        g.setFont(font);
        g.setColor(Color.BLACK);
        // 在这里绘制游戏地图和角色
        for (int i = 0; i < MAX; i++) {
            for (int j = 0; j < MAX; j++) {
                int cellType = Arr[i][j];
                switch (cellType) {
                    // 绘制不同类型的格子
                case KongBai:
                   // System.out.print(KONGBAI);
                    g.drawString(KONGBAI, i*CELL_SIZE, j*CELL_SIZE);
                    break;
                case Kong:
                	// System.out.print(KONG);
                    g.drawString(KONG, i*CELL_SIZE, j*CELL_SIZE);
                    break;
                case Ren:
                	// System.out.print(REN);
                    g.drawString(REN, i*CELL_SIZE, j*CELL_SIZE);
                    break;
                case Xiang:
                	// System.out.print(XIANG);
                    g.drawString(XIANG, i*CELL_SIZE, j*CELL_SIZE);
                    break;
                case Qiang:
                	// System.out.print(QING);
                    g.drawString(QING, i*CELL_SIZE, j*CELL_SIZE);
                    break;
                case Jin:
                	// System.out.print(JIN);
                    g.drawString(JIN, i*CELL_SIZE, j*CELL_SIZE);
                    break;
                case Jin+Ren:
                	// System.out.print(REN);
                g.drawString(REN, i*CELL_SIZE, j*CELL_SIZE);
                    break;
                
                }
                // 绘制角色
            }
        }
    }
    
    
    
    

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GameGUI game = new GameGUI();
            game.setVisible(true);
        });
    }
}

文章来源:https://blog.csdn.net/laocooon/article/details/135164395
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。