JAVA简易推箱子小游戏制作

以下是代码:

Run.java

 

public class Run {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Frame frame = new Frame();
	}

}
	

Frame.java

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

import javax.swing.JFrame;
public class Frame extends JFrame implements KeyListener {
	//  implements实现KeyListener 接口
	GamePanel gp;
	public Frame(){
		this.setSize(500,500);//窗口-尺寸
		this.setTitle("推箱子");//窗口-标题
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		gp= new GamePanel();
		this.add(gp);//增加画板
		this.addKeyListener(this);// 监听窗口事件	
		this.setVisible(true);//窗口-是否可视				
		}
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		gp.keyD(arg0.getKeyCode());
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	} 
}
	

PointManager .java

import java.awt.Graphics;

public class PointManager {
	GPoint[] dians =new GPoint[100];
	int number;
	public PointManager(int[][] mapData){
		number=0;
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				if(mapData[i][j]==4){
					createOnePoint(j*50,i*50);
				}
			}
		}
	}
	public void render(Graphics g){
		for(int i=0;i

GPoint .java

import java.awt.Graphics;
import java.awt.Image;

public class GPoint {
	Image image;
	int x,y;
	public GPoint(int x_,int y_){
		x=x_;
		y=y_;
		image=GameTools.getimage("point.png");
	}
	public void render(Graphics g){
		g.drawImage(image, x, y, null);
	}
}

GameTools .java

import java.awt.Image;

import javax.swing.ImageIcon;

public class GameTools {
	public static Image getimage(String name){
		ImageIcon icon = new ImageIcon(name);
		return icon.getImage();
	}
}

GamePanel.java

import java.awt.Graphics;

import javax.swing.JPanel;
public class GamePanel extends JPanel {
	Player player;
	Map map;
	BoxManager bm;
	PointManager pm;
	
	public void create(){
		player = new Player(200,200);
		map = new Map();
		bm = new BoxManager(map.mapData);
		pm = new PointManager(map.mapData);
	}
	public GamePanel(){
		create();
	}
	public void paint(Graphics g){
		map.render(g);
		pm.render(g);
		player.render(g);
		
		bm.render(g);
		
		
	}
	public void keyD(int key){
		switch(key){
		case 37://左
			player.move(-50, 0,map,bm,pm);
			break;
		case 38://上
			player.move(0, -50,map,bm,pm);
			break;
		case 39://右
			player.move(+50, 0,map,bm,pm);
			break;
		case 40://下
			player.move(0, +50,map,bm,pm);
			break;
		default:
		}
		this.repaint();
	}
	
}

BoxManager .java

import java.awt.Graphics;

public class BoxManager {
	Box[] boxes =new Box[100];
	int number;
	public BoxManager(int[][] mapData){
		number=0;
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				if(mapData[i][j]==3){
					createOneBox(j*50,i*50);
				}
			}
		}
	}
	public void render(Graphics g){
		for(int i=0;i

Box .java

import java.awt.Graphics;
import java.awt.Image;

public class Box {
	Image image;
	int x,y;
	boolean temp_success;
	public Box(int x_,int y_){
		x=x_;
		y=y_;
		image=GameTools.getimage("box.png");
	}
	public void render(Graphics g){
		if(temp_success==false){
			g.drawImage(image, x, y, null);
		}else{
			g.drawImage(GameTools.getimage("box2.png"), x, y, null);
		}
		
	}
	public boolean move(int dx,int dy,BoxManager bm,PointManager pm){
		if(bm.check(dx+x, dy+y)!=null){
			return false;
		}
		x+=dx;
		y+=dy;
		temp_success=pm.checkPoint(x, y);
		return true;
	}
}

Player .java

import java.awt.Graphics;
import java.awt.Image;

public class Player {
	Image image;
	int x,y;
	int temp_runNum;
	public Player(int x_,int y_){
		x=x_;
		y=y_;
		image=GameTools.getimage("player.png");
	}
	public void render(Graphics g){
		g.drawImage(image, x, y, null);
		g.drawString("已走 "+temp_runNum+" 步", 200, 30);
	}
	public void move(int dx,int dy,Map map,BoxManager bm,PointManager pm){
		if(map.HitCheck(dx+x, dy+y)==true){
			return;
		}
		if(bm.check(dx+x, dy+y)!=null){
			if(map.HitCheck((dx*2)+x,(dy*2)+y)==true){
				return;
			}
			if(bm.check(dx+x, dy+y).move(dx,dy,bm,pm)==false){
				return;
			}
		}
		x+=dx;
		y+=dy;
		temp_runNum++;
		
	}
}

Map.java

import java.awt.Graphics;
import java.awt.Image;

public class Map {
	Image[] image;
	int[][] mapData;
	public Map(){
		image = new Image[3];
		mapData = new int[][]{
			{2,2,2,2,2,2,2,2,2,2},
			{2,0,0,0,0,0,0,0,0,2},
			{2,0,1,1,1,1,1,1,0,2},
			{2,0,1,1,3,1,3,1,0,2},
			{2,0,1,4,1,1,1,1,0,2},
			{2,0,1,1,1,1,1,1,0,2},
			{2,0,1,4,1,1,1,1,0,2},
			{2,0,1,1,1,1,1,1,0,2},
			{2,0,0,0,0,0,0,0,0,2},
			{2,2,2,2,2,2,2,2,2,2}
		};
		for(int i=0;i<3;i++){
			image[i]=GameTools.getimage("map"+i+".png");
		}
		
		
	}
	public void render(Graphics g){
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				switch(mapData[i][j]){
				case 2:
					g.drawImage(image[2],j*50, i*50, null);
					break;
				case 0:
					g.drawImage(image[0],j*50, i*50, null);
					break;
				default:
					g.drawImage(image[1],j*50, i*50, null);
				}
			}
		}
	}
	public boolean HitCheck(int x,int y){
		if(mapData[y/50][x/50]==0){
			return true;
		}
		return false;
	}
}

 

 

图片和源代码:https://download.csdn.net/download/qq_20198003/11752269

你可能感兴趣的:(JAVA)