JAVA开发的一个推箱子游戏原型



      自从打通了JAVA的任督二脉之后,想法不断出现,想停下来都不行,逼的只能编程实现。下面是开发一个简单的推箱子游戏。
      推箱子游戏很简单,用C大一的学生都做出来,关键要解决的问题有:地图, 元素, 移动。我这里构造一个10*10的地图,元素就人和箱子,移动就是人在地图上的位置。
      好了开始编程了,用JAVA编游戏肯定不能是控制台,太不方便了,要用图形编程。我用button做地图最基本元素,button的不同颜色代表不同事物,黑色是墙,白色是空地,人是绿色,箱子是红色。
      设计了三个类,一个是元素类(人,箱子),一个地图类,最主要的游戏类(包含地图类,和人,箱子)。

      具体如下:元素类

class Elem {
    int loc_x;
    int loc_y;
    int role; //1是人,2是箱子
}
    地图类

class Map{
    JButton[][] mapBt;
    JPanel panel;
    JFrame frame;
    Map()
    {
        mapBt = new JButton[10][10];
        frame = new JFrame("Boxman");
        panel = new JPanel();
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 添加面板
        frame.add(panel);

        int i, j;
        for(i=0; i<10; i++)
            for(j=0; j<10; j++)
                mapBt[i][j] = new JButton();
        for(i=0; i<10; i++)
            for(j=0; j<10; j++)
            {
                panel.add(mapBt[i][j]);
           }
        initmap();
        frame.setVisible(true);
    }
    //初始化地图就是把外转一圈置黑为墙,内部设置为白
    void initmap()
    {
        int i, j;
        for(i=0; i<10; i++)
            for(j=0; j<10; j++)
            {
                mapBt[i][j].setBackground(Color.BLACK);
                mapBt[i][j].setBounds(20*j, 20*i,20, 20);

           }
        for(i=1; i<9; i++)
            for(j=1; j<9; j++)
            {
                mapBt[i][j].setBackground(Color.WHITE);
                mapBt[i][j].setBounds(20*j, 20*i,20, 20);
           }
    }
}
  游戏类

  

class Boxgame{
	  Elem man;
	  Elem box;
	  Map gameMap;
	  Boxgame()
	  {      //初始化人和箱子及地图
		gameMap = new Map();
		man = new Elem();  
	        box = new Elem();
	        man.loc_x = 4;
	        man.loc_y = 2;
	        man.role = 1;
	        box.loc_x = 6;
	        box.loc_y = 3;
	        box.role = 2;
	  }
	   void run() {    
		   gameKeyEvent(gameMap);       
	   }
	  
	   void setElem(Elem ele)
	    {
	    	if(ele.role == 1)
	            gameMap.mapBt[ele.loc_y][ele.loc_x].setBackground(Color.BLUE); //人是蓝色
	    	else if(ele.role == 2)
	    		gameMap.mapBt[ele.loc_y][ele.loc_x].setBackground(Color.RED);  //箱子是红色
	    }
        void move()//人物的移动就是不同颜色到地图上
        {
            setElem(man);
            setElem(box);
        }
	    void gameKeyEvent(final Map map)
	    {
	    	map.frame.requestFocus();
	    	//响应键盘按键事件
	    	map.frame.addKeyListener(new KeyAdapter()
	        {
	            public void keyPressed(KeyEvent e)
	            {
	                 int key = e.getKeyCode();
	                   if(key == KeyEvent.VK_A)
	                   {
	                       man.loc_x = man.loc_x -1;
	                       map.initmap();
	                       move();
	                   }
	                   if(key == KeyEvent.VK_D)
	                   {    
	                       man.loc_x = man.loc_x + 1;
	                       map.initmap();
	                       move();
	                   }
	                   if(key == KeyEvent.VK_W)
	                   {
	                       man.loc_y = man.loc_y -1;
	                       map.initmap();
	                       move();
	                   }
	                   if(key == KeyEvent.VK_S)
	                   {    
	                       man.loc_y = man.loc_y + 1;
	                       map.initmap();
	                       move();
	                   }
	            }
	        });
	    }
}
  这个类里最难的是事件的窗口事件的监听,不过既然任督二脉打通了就没什么不好理解,只要去找用法就行了。

  包和运行代码如下:

  

package example;
import java.awt.Color;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.io.IOException;
import java.awt.event.*;

import javax.swing.*;


public class Example {
	 public static void main(String[] args) {    
	        // 创建 game实例
	       Boxgame game = new Boxgame();
	       game.run();
	    }
}
      将上面的代码结合起来就能运行了

      效果如下图先,通过键盘a,d,s,w移动后如下图所示

       哈哈成功移动,当然了其它的具体逻辑(如通关、不能撞墙等)就不用写了。

     

     不过还有一个问题就是程序启动的时候,地图并没有按我设置的排序,必须按一下才可以恢复。不知道什么原因,不是我暂时学习的重点,先不考虑了,后面再说。

   

     




  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,推箱子游戏一个比较有趣的小游戏,下面是一个Java实现的简单版本: ```java import java.util.Scanner; public class SokobanGame { public static void main(String[] args) { char[][] map = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', ' ', ' ', 'O', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', ' ', ' ', ' ', 'X', ' ', ' ', ' ', ' ', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} }; int playerX = 3; int playerY = 3; Scanner input = new Scanner(System.in); while (true) { // 打印地图 for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { System.out.print(map[i][j] + " "); } System.out.println(); } // 判断是否胜利 boolean isWin = true; for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { if (map[i][j] == 'O') { isWin = false; break; } } } if (isWin) { System.out.println("恭喜你胜利了!"); break; } // 获取用户输入 System.out.print("请输入移动方向(w:上,s:下,a:左,d:右):"); String direction = input.nextLine(); // 移动玩家 int newPlayerX = playerX; int newPlayerY = playerY; switch (direction) { case "w": newPlayerX--; break; case "s": newPlayerX++; break; case "a": newPlayerY--; break; case "d": newPlayerY++; break; } if (map[newPlayerX][newPlayerY] != '#') { if (map[newPlayerX][newPlayerY] == 'X') { // 推箱子 int newBoxX = newPlayerX; int newBoxY = newPlayerY; switch (direction) { case "w": newBoxX--; break; case "s": newBoxX++; break; case "a": newBoxY--; break; case "d": newBoxY++; break; } if (map[newBoxX][newBoxY] != '#') { map[playerX][playerY] = ' '; map[newPlayerX][newPlayerY] = 'P'; map[newBoxX][newBoxY] = 'X'; playerX = newPlayerX; playerY = newPlayerY; } } else { // 移动玩家 map[playerX][playerY] = ' '; map[newPlayerX][newPlayerY] = 'P'; playerX = newPlayerX; playerY = newPlayerY; } } } } } ``` 代码中用二维字符数组 `map` 存储地图,其中 `#` 表示墙壁,`O` 表示目标点,`X` 表示箱子,`P` 表示玩家。`playerX` 和 `playerY` 分别表示玩家的坐标,通过用户输入的方向移动玩家,当玩家移动到箱子旁边时,按照相应的方向推动箱子。当所有的箱子都被推到目标点上时,游戏胜利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值