一、GameBox
import java.awt.Graphics;
import java.awt.Image;
public class GameBox {
Image im;
int x,y;
int i;
//构造
public GameBox(int x,int y){
im=Tools.getImageByName("box.png");
this.x=x;
this.y=y;
}
public void render(Graphics g){
if(i==4){//当i等于4时显示变颜色的箱子
im=Tools.getImageByName("box2.png");
}else{
im=Tools.getImageByName("box.png");
}
g.drawImage(im,x,y,null);
}
public boolean moveBox(int dx,int dy,GameMap map,GameBoxManagerbm,GamePointManager pm){
if(map.isHit(x+dx, y+dy)==true)
return false;
if(bm.chack(x+dx, y+dy)!=null)
return false;
x+=dx;
y+=dy;
if(pm.chack(x, y)==true){
i=4;
}else{
i=0;
}
return true;
}
}
二、GameBoxManager
import java.awt.Graphics;
public class GameBoxManager {
GameBox[] boxes = new GameBox[100];
int num;
public GameBoxManager(){
num=0;
}
public void createBox(int x,int y){
boxes[num] =new GameBox(x,y);
num++;
}
//所有箱子渲染
public void render(Graphics g){
for(int i=0;i<num;i++){
boxes[i].render(g);
}
}
//检测某个位置是否有箱子
public GameBox chack(int x,int y){
for(int i=0;i<num;i++){
if(x ==boxes[i].x && y==boxes[i].y){
return boxes[i];
}
}
return null;
}
}
三、Gameframe
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Gameframe extends JFrameimplements KeyListener {//继承,implements实现
GamePanelgp;
//构造方法,没有返回值,也没有void,方法名和类名一致
public Gameframe(){
this.setSize(500,500);//像素
this.setTitle("推箱子");//标题
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭
gp=new GamePanel();
//添加监听
this.addKeyListener(this);
this.add(gp);
this.setVisible(true);
}
@Override
publicvoid keyPressed(KeyEvent e) {
gp.keyDown(e.getKeyCode());
}
@Override
publicvoid keyReleased(KeyEvent arg0) {
//TODO Auto-generated method stub
}
@Override
publicvoid keyTyped(KeyEvent arg0) {
//TODO Auto-generated method stub
}
}
四、Gamemap
import java.awt.Graphics;
import java.awt.Image;
//构造方法
public class GameMap {
byte[][] map=new byte[][]{
{2,2,2,2,2,2,2,2,2,2},
{2,2,0,0,0,0,0,2,2,2},
{2,0,0,1,4,1,0,2,2,2},
{2,0,1,3,4,3,0,0,2,2},
{2,0,1,1,4,3,1,0,2,2},
{2,0,1,3,4,1,1,0,2,2},
{2,0,1,3,4,3,0,0,2,2},
{2,0,0,1,4,1,0,2,2,2},
{2,2,0,0,0,0,0,2,2,2},
{2,2,2,2,2,2,2,2,2,2}
};
Image[] images=new Image[3];
//加载地图图片
public GameMap(){
for (int i = 0; i <3; i++) {
images[i]=Tools.getImageByName("map"+i+".png");
}
}
public void render(Graphics g){
for (int i = 0; i <10; i++) {
for (int j = 0; j < 10; j++) {
intid=map[i][j];
if(id>2){
id=1;
}
g.drawImage(images[id],50*j,50*i,null);
}
}
}
public boolean isHit(int x,int y){
if(y/50<0||y/50>=map.length||x/50<0||x/50>=map.length)
return true;
if(map[y/50][x/50]==0){
return true;
}
return false;
}
}
五、GamePanel
import java.awt.Graphics;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
GamePlayer Player;
GameMap map;
GameBoxManager bm ;
GamePointManager pm ;
public GamePanel(){
Player=new GamePlayer(300,250);
map=new GameMap();
pm=new GamePointManager();
bm=new GameBoxManager();
createMap();
}
public void paint(Graphics g){
map.render(g);
pm.render(g);
Player.render(g);
bm.render(g);
}
public void keyDown(int keyCode){
switch(keyCode){
case 37://向左
Player.movePlayer(-50, 0,bm,map,pm);
break;
case 38://向上
Player.movePlayer(0, -50,bm,map,pm);
break;
case 39://向右
Player.movePlayer(50, 0,bm,map,pm);
break;
case 40://向下
Player.movePlayer(0, 50,bm,map,pm);
break;
}
this.repaint();
}
//创建箱子
public void createMap(){
for(int i=0;i<map.map.length;i++){
for(int j=0;j<map.map[i].length;j++){
switch(map.map[i][j]){
case 3:
bm.createBox(j*50, i*50);
break;
}
switch(map.map[i][j]){
case 4:
pm.createPoint(j*50, i*50);
break;
}
}
}
}
}
六、GamePlayer玩家
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class GamePlayer {
Image im;//图片
int x,y;
//初始化属性值
public GamePlayer(int x,int y){
//加载图片
// ImageIcon icon=newImageIcon("player.png");
// im=icon.getImage();
im=Tools.getImageByName("player.png");
this.x=x;
this.y=y;
}
//显示图片 渲染
public void render(Graphics g){
g.drawImage(im,x,y,null);
}
public void movePlayer(int dx,int dy,GameBoxManager bm,GameMapmap,GamePointManager pm){
//检测
if(map.isHit(x+dx, y+dy)==true){
return;
}
GameBox box=bm.chack(x+dx, y+dy);
if(box !=null){
if(box.moveBox(dx,dy,map,bm,pm)==false){
return;
}
}
x+=dx;
y+=dy;
}
}
七、GamePoint 圆点
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class GamePoint {
Image im;//图片
int x,y;
//初始化属性值
public GamePoint(int x,int y){
//加载图片
// ImageIcon icon=newImageIcon("player.png");
// im=icon.getImage();
im=Tools.getImageByName("point.png");
this.x=x;
this.y=y;
}
//显示图片 渲染
public void render(Graphics g){
g.drawImage(im,x,y,null);
}
}
八、GamePointManager
import java.awt.Graphics;
public class GamePointManager {
GamePoint[] points = new GamePoint[100];
int num;
public GamePointManager(){
num=0;
}
public void createPoint(int x,int y){
points[num] =new GamePoint(x,y);
num++;
}
//所有点渲染
public void render(Graphics g){
for(int i=0;i<num;i++){
points[i].render(g);
}
}
// 检测某个位置是否有箱子
public boolean chack(int x,int y){
for(int i=0;i<num;i++){
if(x ==points[i].x && y==points[i].y){
return true;
}
}
return false;
}
}
九、Run
public class Run {
publicstatic void main(String[] args) {
Gameframegf=new Gameframe();
}
}
十、Tools
import java.awt.Image;
import javax.swing.ImageIcon;
public class Tools {
public static Image getImageByName(String name){
ImageIcon icon=new ImageIcon(name);
Imageim=icon.getImage();
returnim;
}
}
图片