迷宫游戏 精灵移动过程中遇到墙之后不能穿过而是被阻挡如何设计

初步设计代码如下:

使用判断精灵头脚与每个数组中图片墙上下左右分别判断

如精灵的右侧与墙的左侧判断大小,同时判断精灵的头部是否被遮挡,精灵的脚部是否被遮挡

依次再判断向左的情况,向上的情况,向下的情况

package com.swift;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyEvent;

import com.rupeng.game.GameCore;

public class Migong implements Runnable{

    public static void main(String[] args) {
        GameCore.start(new Migong());
    }

    @Override
    public void run() {
        GameCore.setGameSize(805, 590);
        GameCore.setGameTitle("图片迷宫游戏—二维数组制作");
        GameCore.loadBgView("bg.png");
        //二维数组赋值10行10列,每张图片宽度80*56
        int[][] arr = {{0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,1,0,0,0,0},
                        {4,4,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,4,0,0,0},
                        {1,1,2,0,0,0,0,1,0,0},
                        {0,4,0,0,0,1,0,0,0,0},
                        {0,0,1,0,1,0,0,0,2,2}};
        //计数器
        int num=0;
        //根据元素的值在游戏窗口中生成图片墙
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {
                if(arr[i][j]==1) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "1.png");
                    GameCore.setImagePosition(num, j*80, i*56);
                    num++;
                }
                if(arr[i][j]==2) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "2.png");
                    GameCore.setImagePosition(num, j*80, i*56);
                    num++;
                }
                if(arr[i][j]==4) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "4.png");
                    GameCore.setImagePosition(num, j*80, i*56);
                    num++;
                }
            }
        }
        
        // 女精灵
                int spriteGirl = 0;
                GameCore.createSprite(spriteGirl, "butterfly");
                GameCore.playSpriteAnimate(spriteGirl, "fly", true);
                GameCore.setSpritePosition(spriteGirl, 100, 20);
                
                //得到女精灵的高度和宽度
                Dimension dGirl=GameCore.getSpriteSize(spriteGirl);
                int girlHeight=dGirl.height;
                int girlWidth=dGirl.width;
                
                for (;;) {

                    int codeNum = GameCore.getPressedKeyCode();
                    Point positionG = GameCore.getSpritePosition(spriteGirl);
                    //得到女精灵头和脚的位置
                    int girlUpY=positionG.y;
                    int girlDownY=positionG.y+girlHeight;
                    //得到女精灵左侧和右侧的位置
                    int girlLeftX=positionG.x;
                    int girlRightX=positionG.x+girlWidth;
                    
                    

                    if (codeNum == KeyEvent.VK_UP || codeNum == KeyEvent.VK_W) {
                        if (positionG.y > 30) {
                            GameCore.setSpritePosition(spriteGirl, positionG.x, --positionG.y);
                            GameCore.pause(11);
                        }
                    }

                    if (codeNum == KeyEvent.VK_DOWN || codeNum == KeyEvent.VK_S) {
                        if (positionG.y < 550) {
                            GameCore.setSpritePosition(spriteGirl, positionG.x, ++positionG.y);
                            GameCore.pause(11);
                        }
                    }
                    if (codeNum == KeyEvent.VK_LEFT || codeNum == KeyEvent.VK_A) {
                        if (positionG.x > 0) {
                            GameCore.setSpriteFlipX(spriteGirl, true);
                            GameCore.setSpritePosition(spriteGirl, --positionG.x, positionG.y);
                            GameCore.pause(2);
                        }
                    }
                    if (codeNum == KeyEvent.VK_RIGHT || codeNum == KeyEvent.VK_D) {
                        boolean hasRightWall=false;
                        for(int i=0;i<arr.length;i++) {
                            for(int j=0;j<arr[i].length;j++) {
                                if(arr[i][j]==0) {
                                    continue;
                                }
                                //得到墙的左侧和右侧位置
                                int imageLeftX=80*j;
                                int imageRightX=80*(j+1);
                                //得到墙的上边和下边位置
                                int imageUpY=56*i;
                                int imageDownY=56*(i+1);
                                if(girlRightX>=imageLeftX&&(girlUpY<=imageDownY&&girlUpY>=imageUpY
                                        ||girlDownY>=imageUpY&&girlDownY<=imageDownY)) {
                                    hasRightWall=true;
                                }
                                        
                            }
                        }
                        
                        
                        if (positionG.x < 800&&hasRightWall==false) {
                            GameCore.setSpriteFlipX(spriteGirl, false);
                            GameCore.setSpritePosition(spriteGirl, ++positionG.x, positionG.y);
                            GameCore.pause(1);
                        }
                    }
                }
        
    }

}

 用简单思路代替:

通过距离判定的方法

package com.swift;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyEvent;

import com.rupeng.game.GameCore;

public class Migong implements Runnable{

    public static void main(String[] args) {
        GameCore.start(new Migong());
    }

    @Override
    public void run() {
        GameCore.setGameSize(805, 590);
        GameCore.setGameTitle("图片迷宫游戏—二维数组制作");
        GameCore.loadBgView("bg.png");
        //二维数组赋值10行10列,每张图片宽度80*56
        int[][] arr = {{0,0,0,0,0,0,0,1,0,0},
                        {0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,1,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0},
                        {0,0,2,0,0,0,4,0,0,0},
                        {0,0,0,0,0,0,0,1,0,0},
                        {0,0,0,0,0,1,0,0,0,0},
                        {0,0,1,0,1,0,0,0,2,2}};
        //计数器
        int num=0;
        //根据元素的值在游戏窗口中生成图片墙
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {
                if(arr[i][j]==1) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "1.png");
                    GameCore.setImagePosition(num, j*80, i*56);
                    num++;
                }
                if(arr[i][j]==2) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "2.png");
                    GameCore.setImagePosition(num, j*80, i*56);
                    num++;
                }
                if(arr[i][j]==4) {
                    GameCore.createImage(num);
                    GameCore.setImageSource(num, "4.png");
                    GameCore.setImagePosition(num, j*80, i*56);
                    num++;
                }
            }
        }
        
        // 女精灵
                int spriteGirl = 0;
                GameCore.createSprite(spriteGirl, "butterfly");
                GameCore.playSpriteAnimate(spriteGirl, "fly", true);
                GameCore.setSpritePosition(spriteGirl, 100, 20);
                
                //得到女精灵的高度和宽度
                Dimension dGirl=GameCore.getSpriteSize(spriteGirl);
                int girlHeight=dGirl.height;
                int girlWidth=dGirl.width;
                
                for (;;) {

                    int codeNum = GameCore.getPressedKeyCode();
                    Point positionG = GameCore.getSpritePosition(spriteGirl);
                    //得到女精灵头和脚的位置
                    int girlUpY=positionG.y;
                    int girlDownY=positionG.y+girlHeight;
                    int girlCenterY=(girlUpY+girlDownY)/2;
                    //得到女精灵左侧和右侧的位置
                    int girlLeftX=positionG.x;
                    int girlRightX=positionG.x+girlWidth;
                    int girlCenterX=(girlLeftX+girlRightX)/2;
                    
                    

                    if (codeNum == KeyEvent.VK_UP || codeNum == KeyEvent.VK_W) {
                        if (positionG.y > 30) {
                            GameCore.setSpritePosition(spriteGirl, positionG.x, --positionG.y);
                            GameCore.pause(11);
                        }
                    }

                    if (codeNum == KeyEvent.VK_DOWN || codeNum == KeyEvent.VK_S) {
                        if (positionG.y < 550) {
                            GameCore.setSpritePosition(spriteGirl, positionG.x, ++positionG.y);
                            GameCore.pause(11);
                        }
                    }
                    if (codeNum == KeyEvent.VK_LEFT || codeNum == KeyEvent.VK_A) {
                        if (positionG.x > 0) {
                            GameCore.setSpriteFlipX(spriteGirl, true);
                            GameCore.setSpritePosition(spriteGirl, --positionG.x, positionG.y);
                            GameCore.pause(2);
                        }
                    }
                    if (codeNum == KeyEvent.VK_RIGHT || codeNum == KeyEvent.VK_D) {
                        boolean hasRightWall=false;
                        for(int i=0;i<arr.length;i++) {
                            for(int j=0;j<arr[i].length;j++) {
                                if(arr[i][j]==0) {
                                    continue;
                                }
                                //得到墙的左侧和右侧位置
                                int imageLeftX=80*j;
                                int imageRightX=80*(j+1);
                                int imageCenterX=(imageLeftX+imageRightX)/2;
                                //得到墙的上边和下边位置
                                int imageUpY=56*i;
                                int imageDownY=56*(i+1);
                                int imageCenterY=(imageUpY+imageDownY)/2;
                                int distance=(int)(Math.sqrt(Math.pow((girlCenterX-imageCenterX),2)+Math.pow((girlCenterY-imageCenterY),2)));
                                if(distance<100) {
                                    hasRightWall=true;
                                }
                                        
                            }
                        }
                        
                        if (positionG.x < 800&&hasRightWall==false) {
                            GameCore.setSpriteFlipX(spriteGirl, false);
                            GameCore.setSpritePosition(spriteGirl, ++positionG.x, positionG.y);
                            GameCore.pause(1);
                        }
                    }
                }
        
    }

}

 

posted @ 2017-11-04 21:16  Advancing-Swift  阅读(404)  评论(0编辑  收藏  举报