推箱子游戏

跟着51CTO上的教程制作了一个简单的推箱子游戏

这是教程的下载地址:Java游戏开发-推箱子完整版视频课程(11集附源码)

源码下载地址:http://download.csdn.net/detail/u013547284/9859949

完成后的效果如下


目前只实现了一个关卡,可拓展为多关卡游戏。

源码比较简单,只有一个启动类和一个主功能界面类,这里直接贴出来了

package guo.app;

import guo.ui.MainFrame;
//这是启动类
public class App {
	public static void main(String[] args){
		new MainFrame();
	}
}
package guo.ui;

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
//这是主类
public class MainFrame extends Frame implements KeyListener, WindowListener{
	
	JLabel lab_wolf;
	
	public MainFrame(){
		//让目标(笼子)在画层最上层
		//目标(笼子)初始化
		targetInit();
		//先画人物后画背景(先add的组件在上层,避免人物被背景覆盖)
		//推箱子人物(灰太狼)初始化
		wolfInit();	
		//箱子(懒羊羊)初始化
		sheepInit();
		//障碍(树)初始化
		treeInit();
		//背景初始化
		backgroudInit();
		//设置主窗口界面显示效果
		setMainFrameUI();
		//设置键盘监听
		this.addKeyListener(this);
		this.addWindowListener(this);
	}
	
	JLabel[][] sheeps = new JLabel[12][16];
	
	//场景数据的模拟,使用二维数组模拟
	//1代表障碍,0代表空地
	int[][] datas = {
	 	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
	};
	//代表人物横向的位置
	int wx;
	//代表人物纵向的位置
	int wy;
	//当前有多少个箱子移动到了目标
	int num = 0;
	//代表箱子的总数
	int total = 3;
	
	private void treeInit() {
		//1.创建图片
		Icon ic = new ImageIcon("tree.png");
		//遍历二维数组
		for(int i=0; i<datas.length; i++){
			for(int j=0; j<datas[0].length; j++){
				//障碍的初始化
				if(datas[i][j] == 1){
					//2.创建JLable
					JLabel lab_tree = new JLabel(ic);
					//3.设置位置和大小
					//考虑到标题栏的长度,坐标需要横向修正12像素,纵向修正36像素
					lab_tree.setBounds(j*50+12, i*50+36, 50, 50);
					//4.添加到窗体
					this.add(lab_tree);
				}
			}
		}
		
	}

	
	
	//目标初始化
	private void targetInit() {
		// TODO 自动生成的方法存根
		//制作以第一目标
		//创建一张箱子图片
		Icon i = new ImageIcon("target.png");
		//使用JLable模拟目标
		JLabel lab_target1 = new JLabel(i);
		//设置箱子位置和大小
		//考虑到标题栏的长度,坐标需要横向修正12像素,纵向修正36像素
		lab_target1.setBounds(712, 236, 50, 50);
		//将箱子添加到窗体
		this.add(lab_target1);
		datas[4][14] = 8;	
		//制作其他两个目标
		JLabel lab_target2 = new JLabel(i);
		lab_target2.setBounds(712, 286, 50, 50);
		this.add(lab_target2);
		datas[5][14] = 8;
		
		JLabel lab_target3 = new JLabel(i);
		lab_target3.setBounds(712, 336, 50, 50);
		this.add(lab_target3);
		datas[6][14] = 8;
	}
	
	//箱子初始化
	private void sheepInit() {
		// TODO 自动生成的方法存根
		//制作第一只羊
		//创建一张箱子图片
		Icon i = new ImageIcon("sheep-no.png");
		//使用JLable模拟箱子
		JLabel lab_sheep1 = new JLabel(i);
		//设置箱子位置和大小
		//考虑到标题栏的长度,坐标需要横向修正12像素,纵向修正36像素
		lab_sheep1.setBounds(312, 236, 50, 50);
		//将箱子添加到窗体
		this.add(lab_sheep1);
		//修改箱子对应位置为4
		datas[4][6] = 4;
		//把JLabel组件放入到sheeps数组中
		sheeps[4][6] = lab_sheep1;
		
		//制作其他两只羊
		JLabel lab_sheep2 = new JLabel(i);
		lab_sheep2.setBounds(312, 336, 50, 50);	
		this.add(lab_sheep2);
		datas[6][6] = 4;
		sheeps[6][6] = lab_sheep2;
		
		JLabel lab_sheep3 = new JLabel(i);
		lab_sheep3.setBounds(312, 436, 50, 50);	
		this.add(lab_sheep3);
		datas[8][6] = 4;
		sheeps[8][6] = lab_sheep3;
	}

	//推箱子人物初始化
	private void wolfInit() {
		//设置人物最初的位置
		wx = 4;
		wy = 5;
		//创建一张人物图片
		Icon i = new ImageIcon("wolf-zm.png");
		//使用JLable模拟人物
		lab_wolf = new JLabel(i);
		//设置人物位置和大小
		//考虑到标题栏的长度,坐标需要横向修正12像素,纵向修正36像素
		lab_wolf.setBounds(12 + wx*50, 36 + wy*50, 50, 50);
		//将人物添加到窗体
		this.add(lab_wolf);
	}

	//背景初始化
	private void backgroudInit(){
		//创建一个图片
		Icon i = new ImageIcon("bg.png");
		//使用JLable制作背景
		JLabel lab_bg = new JLabel(i);
		//设置组件的位置和大小
		//考虑到标题栏的长度,需要横向修正12像素,纵向修正36像素
		lab_bg.setBounds(12, 36, 800, 600);
		//将JLable添加到窗体
		this.add(lab_bg);		 
	}
	
	//设置主窗口界面显示效果
	private void setMainFrameUI(){
		this.setLayout(null);//设置布局
		this.setTitle("推箱子");//窗口标题
		this.setLocation(110, 30);//窗口位置
		setSize(826,650);//窗口大小
		this.setResizable(false);
		setVisible(true);//设置为可视
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO 自动生成的方法存根
		//获取键码值,分辨按键
		//左 37
		//上 38
		//右 39
		//下 40
		int key = arg0.getKeyCode();
		if(key == 37){
			//让人物向左移动
			//人    空地
			if(datas[wy][wx-1] == 0){
				wx = wx-1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x-50, y);
				Icon i = new ImageIcon("wolf-zb.png");
				lab_wolf.setIcon(i);
				return;
			}
			//人    树
			if(datas[wy][wx-1] == 1){
				return;
			}
			//人    箱子     树
			if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 1){
				return;
			}
			//人    箱子    箱子
			if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 4){
				return;
			}
			//人    箱子    目标箱子
			if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 12){
				return;
			}
			//人    目标箱子    树
			if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 1){
				return;
			}
			//人    目标箱子    箱子
			if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 4){
				return;
			}
			//人    目标箱子    目标箱子
			if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 12){
				return;
			}
			//人物    空目标
			if(datas[wy][wx-1] == 8){
				wx = wx-1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x-50, y);
				Icon i = new ImageIcon("wolf-ym.png");
				lab_wolf.setIcon(i);
				return;
			}
			//人物    箱子    空地
			if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 0){
				datas[wy][wx-1] = 0;
				datas[wy][wx-2] = 4;
			}
			//人物    箱子    空目标
			if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 8){
				datas[wy][wx-1] = 0;
				datas[wy][wx-2] = 12;
				num++;
			}
			//人    目标箱子    空地
			if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 0){
				datas[wy][wx-1] = 8;
				datas[wy][wx-2] = 4;
				num--;
			}
			//人    目标箱子    空目标
			if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 8){
				datas[wy][wx-1] = 8;
				datas[wy][wx-2] = 12;
			}
			//移动箱子
			sheeps[wy][wx-1].setLocation(12+(wx-2)*50,36+wy*50);
			sheeps[wy][wx-2] = sheeps[wy][wx-1];
			sheeps[wy][wx-1] = null;
			//移动人
			wx = wx-1;
			int x =(int)lab_wolf.getLocation().getX();
			int y =(int)lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x-50, y);
			Icon i = new ImageIcon("wolf-zb.png");
			lab_wolf.setIcon(i);
			victory();
			return;
		}
		if(key == 38){
			if(datas[wy-1][wx] == 0){
				wy = wy-1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x, y-50);
				Icon i = new ImageIcon("wolf-sm.png");
				lab_wolf.setIcon(i);
				return;
			}
			
			if(datas[wy-1][wx] == 1){
				return;
			}
			if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 1){
				return;
			}
			if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 4){
				return;
			}
			if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 12){
				return;
			}
			if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 1){
				return;
			}
			if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 4){
				return;
			}
			if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 12){
				return;
			}
			
			if(datas[wy-1][wx] == 8){
				wy = wy-1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x, y-50);
				Icon i = new ImageIcon("wolf-sm.png");
				lab_wolf.setIcon(i);
				return;
			}

			if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 0){
				datas[wy-1][wx] = 0;
				datas[wy-2][wx] = 4;
			}

			if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 8){
				datas[wy-1][wx] = 0;
				datas[wy-2][wx] = 12;
				num++;
			}
			if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 0){
				datas[wy-1][wx] = 8;
				datas[wy-2][wx] = 4;
				num--;
			}
			if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 8){
				datas[wy-1][wx] = 8;
				datas[wy-2][wx] = 12;
			}
			sheeps[wy-1][wx].setLocation(12+wx*50,36+wy*50-100);
			sheeps[wy-2][wx] = sheeps[wy-1][wx];
			sheeps[wy-1][wx] = null;
			wy = wy-1;
			int x =(int)lab_wolf.getLocation().getX();
			int y =(int)lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x, y-50);
			Icon i = new ImageIcon("wolf-sm.png");
			lab_wolf.setIcon(i);
			victory();
			return;
		}
		if(key == 39){
			//让人物向右移动
			//人    空地
			if(datas[wy][wx+1] == 0){
				wx = wx+1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x+50, y);
				Icon i = new ImageIcon("wolf-ym.png");
				lab_wolf.setIcon(i);
				return;
			}
			//人    树
			if(datas[wy][wx+1] == 1){
				return;
			}
			//人    箱子     树
			if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 1){
				return;
			}
			//人    箱子    箱子
			if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 4){
				return;
			}
			//人    箱子    目标箱子
			if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 12){
				return;
			}
			//人    目标箱子    树
			if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 1){
				return;
			}
			//人    目标箱子    箱子
			if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 4){
				return;
			}
			//人    目标箱子    目标箱子
			if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 12){
				return;
			}
			//人物    空目标
			if(datas[wy][wx+1] == 8){
				wx = wx+1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x+50, y);
				Icon i = new ImageIcon("wolf-ym.png");
				lab_wolf.setIcon(i);
				return;
			}
			//人物    箱子    空地
			if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 0){
				datas[wy][wx+1] = 0;
				datas[wy][wx+2] = 4;
			}
			//人物    箱子    空目标
			if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 8){
				datas[wy][wx+1] = 0;
				datas[wy][wx+2] = 12;
				num++;
			}
			//人    目标箱子    空地
			if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 0){
				datas[wy][wx+1] = 8;
				datas[wy][wx+2] = 4;
				num--;
			}
			//人    目标箱子    空目标
			if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 8){
				datas[wy][wx+1] = 8;
				datas[wy][wx+2] = 12;
			}
			//移动箱子
			sheeps[wy][wx+1].setLocation(12+(wx+2)*50,36+wy*50);
			sheeps[wy][wx+2] = sheeps[wy][wx+1];
			sheeps[wy][wx+1] = null;
			//移动人
			wx = wx+1;
			int x =(int)lab_wolf.getLocation().getX();
			int y =(int)lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x+50, y);
			Icon i = new ImageIcon("wolf-ym.png");
			lab_wolf.setIcon(i);
			victory();
			return;
		}
		if(key == 40){
			if(datas[wy+1][wx] == 0){
				wy = wy+1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x, y+50);
				Icon i = new ImageIcon("wolf-zm.png");
				lab_wolf.setIcon(i);
				return;
			}
			
			if(datas[wy+1][wx] == 1){
				return;
			}
			if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 1){
				return;
			}
			if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 4){
				return;
			}
			if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 12){
				return;
			}
			if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 1){
				return;
			}
			if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 4){
				return;
			}
			if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 12){
				return;
			}
			
			if(datas[wy+1][wx] == 8){
				wy = wy+1;
				int x =(int)lab_wolf.getLocation().getX();
				int y =(int)lab_wolf.getLocation().getY();
				lab_wolf.setLocation(x, y+50);
				Icon i = new ImageIcon("wolf-zm.png");
				lab_wolf.setIcon(i);
				return;
			}

			if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 0){
				datas[wy+1][wx] = 0;
				datas[wy+2][wx] = 4;
			}

			if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 8){
				datas[wy+1][wx] = 0;
				datas[wy+2][wx] = 12;
				num++;
			}
			if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 0){
				datas[wy+1][wx] = 8;
				datas[wy+2][wx] = 4;
				num--;
			}
			if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 8){
				datas[wy+1][wx] = 8;
				datas[wy+2][wx] = 12;
			}
			sheeps[wy+1][wx].setLocation(12+wx*50,36+wy*50+100);
			sheeps[wy+2][wx] = sheeps[wy+1][wx];
			sheeps[wy+1][wx] = null;
			wy = wy+1;
			int x =(int)lab_wolf.getLocation().getX();
			int y =(int)lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x, y+50);
			Icon i = new ImageIcon("wolf-zm.png");
			lab_wolf.setIcon(i);
			victory();
			return;
		}
	}

	private void victory() {
		if(num == total){
			//移除窗体键盘事件,避免用户多余操作
			this.removeKeyListener(this);
			//以下代码为广告植入,需要与美工配合后最终定稿
			JDialog victory = new JDialog(this,"恭喜您取得了胜利!",true);
			victory.setSize(500, 300);
			victory.setLocationRelativeTo(null);
			victory.setLayout(null);
			JLabel info = new JLabel(new ImageIcon("gg.jpg"));
			info.setBounds(2, 2, 486, 243);
			victory.add(info);
			victory.setVisible(true);
		}
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO 自动生成的方法存根
		System.exit(0);
	}	
}
下面的是使用到的图片资源,命名格式已写出


wolf-zm.png


wolf-sm.png


wolf-zb.png


wolf-ym.png



tree.png


target.png


sheep-no.png


bg.png


gg.jpg

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值