java实现推箱子小游戏(附源码)

先上效果图

可以通过AWSD进行移动和推箱子

自己弄出来的代码玩起来还是很有意思的。

代码一共是三个.java文件,代码内容如下所示

package ss;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * 1.继承窗体类为当前类的父类
 * @author lizhicheng
 *
 */
public class PushBox extends JFrame implements KeyListener{

	/**
	 * 3.声明一个二维数组的地图
	 */
	int[][] maps= 
		{
				{3,3,3,3,3,3,3,3},
				{3,0,0,0,0,0,0,3},
				{3,0,0,0,0,2,0,3},//3表示墙
				{3,0,0,0,3,0,3,3},//2表示箱子
				{3,0,0,1,0,0,0,3},//1表示人
				{3,0,3,0,2,0,0,3},//0表示空地
				{3,0,0,0,0,0,0,3},
				{3,3,3,3,3,3,3,3},
		};
	
	/**
	 * 6.声明两个变量来保存图标的起始位置
	 * 
	 */
	int top=10,left=25;
	int posX=3,posY=4;//保存大力水手的位置
	/**
	 * 2.构造方法添加固定的窗体设置
	 */
	public PushBox()
	{
		//设置标题
		this.setTitle("http://ai.52learn.online");
		//设置窗体大小
		this.setSize(500, 350);
		//设置窗体默认关闭操作
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置窗体可见
		this.setVisible(true);
		//设置窗体居中显示
		this.setLocationRelativeTo(null);
		//进行监听
		this.addKeyListener(this);
	}
	/**
	 * 4.窗体的绘制
	 * @param args
	 */
	@Override
	public void paint(Graphics g) {
		
		super.paint(g);
		refresh();//7.调用刷新
		
	}
	/**
	 * 5.重绘所有当前窗体的组件
	 * @param args
	 */
	public void refresh()
	{
		//获取当前窗体的画笔
		Graphics gs=this.getGraphics();
		//将图片资源引入
		ImageIcon wall = new ImageIcon("bian.png");
		ImageIcon area = new ImageIcon("kongdi.png");
		ImageIcon human = new ImageIcon("ren.png");
		ImageIcon box = new ImageIcon("xiang.png");
		//将资源绘制在窗体上
		for(int i = 0 ; i < maps.length ; i++)
		{
			for(int j = 0 ; j < maps[i].length ; j++)
			{
				if(maps[i][j]==3)
				{
					//绘制墙
					gs.drawImage(wall.getImage(),top+j*40,left+i*40,40,40, null);
				}
				else if(maps[i][j]==0)
				{
					//绘制空地
					gs.drawImage(area.getImage(),top+j*40,left+i*40,40,40, null);
				}
				else if(maps[i][j]==1)
				{
					//绘制水手
					gs.drawImage(human.getImage(),top+j*40,left+i*40,40,40, null);
				}
				else if(maps[i][j]==2)
				{
					//绘制箱子
					gs.drawImage(box.getImage(),top+j*40,left+i*40,40,40, null);
				}
			}
		}
	}
	public static void main(String[] args) {
		new PushBox();
	}
	
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		char ch=arg0.getKeyChar();//获取按键
		//JOptionPane.showMessageDialog(null, "你按了"+ch);
		if(ch=='w'||ch=='W')
		{
			//向上走动
			if(maps[posY-1][posX]==0)
			{
				maps[posY-1][posX]=1;
				maps[posY][posX]=0;
				posY--;
			}
			else if(maps[posY-1][posX]==2&&maps[posY-2][posX]==0)
			{
				maps[posY-2][posX]=2;
				maps[posY-1][posX]=1;
				maps[posY][posX]=0;
				posY--;
			}
		}
		else if(ch=='s'||ch=='S')
		{
			//向下走动
			if(maps[posY+1][posX]==0)
			{
				maps[posY+1][posX]=1;
				maps[posY][posX]=0;
				posY++;
			}
			else if(maps[posY+1][posX]==2&&maps[posY+2][posX]==0)
			{
				maps[posY+2][posX]=2;
				maps[posY+1][posX]=1;
				maps[posY][posX]=0;
				posY++;
			}
		}
		else if(ch=='a'||ch=='A')
		{
			//向左走
			if(maps[posY][posX-1]==0)
			{
				maps[posY][posX-1]=1;
				maps[posY][posX]=0;
				posX--;
			}
			else if(maps[posY][posX-1]==2&&maps[posY][posX-2]==0)
			{
				maps[posY][posX-2]=2;
				maps[posY][posX-1]=1;
				maps[posY][posX]=0;
				posX--;
			}
		}
		else if(ch=='d'||ch=='D')
		{
			//向右走
			if(maps[posY][posX+1]==0)
			{
				maps[posY][posX+1]=1;
				maps[posY][posX]=0;
				posX++;
			}
			else if(maps[posY][posX+1]==2&&maps[posY][posX+2]==0)
			{
				maps[posY][posX+2]=2;
				maps[posY][posX+1]=1;
				maps[posY][posX]=0;
				posX++;
			}
		}
		refresh();
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

package mg;

import java.util.Scanner;

public class migong {

	public static void main(String[] args) {
		int[][] maps= 
		{
				{3,3,3,3,3,3,3,3},
				{3,0,0,0,0,0,0,3},
				{3,0,0,0,0,2,0,3},//3表示墙
				{3,0,0,0,3,0,3,3},//2表示箱子
				{3,0,0,1,0,0,0,3},//1表示人
				{3,0,3,0,2,0,0,3},//0表示空地
				{3,0,0,0,0,0,0,3},
				{3,3,3,3,3,3,3,3},
		};
		//打印输出地图在控制台上
		for(int i = 0 ; i < maps.length ; i++)
		{
			for(int j = 0 ; j < maps[i].length ; j++)
			{
				System.out.print(maps[i][j]);
			}
			System.out.println();
		}
		//声明两个变量记录人的位置 
		int posX=3,posY=4;
		//接收用户键盘的输入
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入方向:上:w,下:s,左:a,右:d : ");
		//运行程序
		while(true)
		{
			String str=sc.next();
			if(str.equalsIgnoreCase("w"))
			{
				//向上
				if(maps[posY-1][posX]==0)
				{
					maps[posY-1][posX]=1;
					maps[posY][posX]=0;
					posY--;
				}
				else if(maps[posY-1][posX]==2&&maps[posY-2][posX]==0)
				{
					maps[posY-2][posX]=2;
					maps[posY-1][posX]=1;
					maps[posY][posX]=0;
					posY--;
				}
			}
			else if(str.equalsIgnoreCase("s"))
			{
				//向下
				if(maps[posY+1][posX]==0)
				{
					maps[posY+1][posX]=1;
					maps[posY][posX]=0;
					posY++;
				}
				else if(maps[posY+1][posX]==2&&maps[posY+2][posX]==0)
				{
					maps[posY+2][posX]=2;
					maps[posY+1][posX]=1;
					maps[posY][posX]=0;
					posY++;
				}
			}
			else if(str.equalsIgnoreCase("a"))
			{
				//向左
				if(maps[posY][posX-1]==0)
				{
					maps[posY][posX-1]=1;
					maps[posY][posX]=0;
					posX--;
				}
				else if(maps[posY][posX-1]==2&&maps[posY][posX-2]==0)
				{
					maps[posY][posX-2]=2;
					maps[posY][posX-1]=1;
					maps[posY][posX]=0;
					posX--;
				}
			}
			else if(str.equalsIgnoreCase("d"))
			{
				//向右
				if(maps[posY][posX+1]==0)
				{
					maps[posY][posX+1]=1;
					maps[posY][posX]=0;
					posX++;
				}
				else if(maps[posY][posX+1]==2&&maps[posY][posX+2]==0)
				{
					maps[posY][posX+2]=2;
					maps[posY][posX+1]=1;
					maps[posY][posX]=0;
					posX++;
				}
			}
			else if(str.equalsIgnoreCase("quit"))
			{
				System.out.print("游戏即将结束\n");
				System.exit(0);
			}
			//打印输出地图在控制台上
			for(int i = 0 ; i < maps.length ; i++)
			{
				for(int j = 0 ; j < maps[i].length ; j++)
				{
					System.out.print(maps[i][j]);
				}
				System.out.println();
			}
			System.out.print("请输入方向:上:w,下:s,左:a,右:d : ");
		}
	}
}
package lzc;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Pig extends JFrame implements KeyListener{

	//进行引入图片
	ImageIcon pig = new ImageIcon("右猪.gif");
	JLabel pigLab = new JLabel(pig);
	//背景
	ImageIcon grass = new ImageIcon("草地.gif");
	JLabel grassLab = new JLabel(grass);
	//声明一个构造方法
	public Pig() {
		// TODO Auto-generated constructor stub
		this.setTitle("小猪佩奇");//设置窗体的标题
		this.setSize(841,310);//设置窗体的大小
		this.setVisible(true);//设置窗口的可见
		this.setLocationRelativeTo(null);//设置窗体居中显示
		//设置窗体的关闭默认操作:当你点击关闭窗口就自动退出程序
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置窗体大小不可改变
		this.setResizable(false);
		
		pigLab.setBounds(300, 150, 71, 88);
		
		//添加背景
		this.add(grassLab);
		grassLab.setBounds(10,10,841,310);
		//添加佩奇
		grassLab.add(pigLab);
		//进行监听
		this.addKeyListener(this);
		//move();
	}
	void move()
	{
		int x=600,y=40;
		while(true)
		{
			x-=6;
			pigLab.setLocation(x, y);
			try {
				Thread.currentThread().sleep(10);
			} catch (Exception a) {
			}
			if(x<0) x=840;
			
		}
	}
	public static void main(String[] args) {
		new Pig();
	}
	/**
	 *用户按下并松开键盘上的按键
	 */
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		char ch=arg0.getKeyChar();//获取按键
		//获取佩琪的位置
		Point p=pigLab.getLocation();
		int x=p.x,y=p.y;
		//JOptionPane.showMessageDialog(null, "你按了"+ch);
		switch(ch)
		{
		case 'w': 
			//向上走动
			y=y-20;
			if(y<0)
			{
				y=310;
			}
			break;
		case 's': 
			//向下走动
			y=y+20;
			if(y>310) 
			{
				y=0;
			}
			break;
		case 'a':
			//向左走
			x=x-20;
			if(x<0) 
			{
				x=841;
			}
			break;
		case 'd':
			//向右走
			x=x+20;
			if(x>841) 
			{
				x=0;
			}
			break;
		default :;
		}
		pigLab.setLocation(x, y);
		
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}

获取代码和相关的图像资源请查看全文

隐藏内容 需要支付:¥10

未经允许不得转载!java实现推箱子小游戏(附源码)