Java 如何制作无限平铺贴图?(爪哇)

Java 如何制作无限平铺贴图?(爪哇),java,image,swing,awt,Java,Image,Swing,Awt,我在制作平铺地图时遇到了以下问题: 当我移动我的角色时,它会离开地图,然后掉落(由于重力) 如何使这张地图无限大? 还有,我如何存储哪些块被破坏了,哪些没有?这样我就可以用相同的地图重新绘制屏幕,当你走回到起点时,布罗肯块仍然在那里 只要告诉我是否需要提供代码 我会给你我的世界 package game.test.src; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import ja

我在制作平铺地图时遇到了以下问题: 当我移动我的角色时,它会离开地图,然后掉落(由于重力) 如何使这张地图无限大? 还有,我如何存储哪些块被破坏了,哪些没有?这样我就可以用相同的地图重新绘制屏幕,当你走回到起点时,布罗肯块仍然在那里

只要告诉我是否需要提供代码

我会给你我的世界

package game.test.src;

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

import javax.swing.ImageIcon;

public class World {

    public Rectangle[] blocks;
    public boolean[] isSolid;
    public Image[] blockImg;
    public final int arrayNum = 500;

    //Block Images
    public Image BLOCK_GRASS, BLOCK_DIRT, BLOCK_STONE, BLOCK_SKY;

    private int x, y, xDirection, yDirection;;

    //map navigation
    static final int PAN_UP = 0, PAN_DOWN = 1, PAN_LEFT= 2, PAN_RIGHT = 3;

    public World(){
        BLOCK_GRASS = new ImageIcon("H:/2D game test/Game test 2/src/game/test/src/images/tile_grass.png").getImage();
        BLOCK_DIRT = new ImageIcon("H:/2D game test/Game test 2/src/game/test/src/images/tile_dirt.png").getImage();
        BLOCK_STONE = new ImageIcon("H:/2D game test/Game test 2/src/game/test/src/images/tile_stone.png").getImage();
        BLOCK_SKY = new ImageIcon("H:/2D game test/Game test 2/src/game/test/src/images/tile_sky.png").getImage();
        blocks = new Rectangle[500];
        blockImg = new Image[500];
        isSolid = new boolean[arrayNum];
        loadArrays();
    }

    private void loadArrays(){
        for(int i = 0; i < arrayNum; i++){
            if(x >= 500){
                x = 0;
                y += 20;
            }
            if(i >= 0 && i < 100){
                blockImg[i] = BLOCK_SKY;
                isSolid[i] = false;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            if(i >= 100 && i < 125){
                blockImg[i] = BLOCK_GRASS;
                isSolid[i] = true;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            if(i >= 125 && i < 225){
                blockImg[i] = BLOCK_DIRT;
                isSolid[i] = true;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            if(i >= 225 && i < 500){
                blockImg[i] = BLOCK_STONE;
                isSolid[i] = true;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            x += 20;
        }
    }

    public void draw(Graphics g){
        for(int i = 0; i < arrayNum; i++){
            g.drawImage(blockImg[i], blocks[i].x, blocks[i].y, null);
        }
    }

    public void moveMap(){
        for(Rectangle r : blocks){
            r.x += xDirection;
            r.y += yDirection;
        }
    }
    public void stopMoveMap(){
        setXDirection(0);
        setYDirection(0);
    }
    private void setXDirection(int dir){
        xDirection = dir;
    }
    private void setYDirection(int dir){
        yDirection = dir;
    }
    public void navigateMap(int nav){
        switch(nav){
            default:
                System.out.println("default case entered... Doing nothing.");
                break;
            case PAN_UP:
                setYDirection(-1);
                break;
            case PAN_DOWN:
                setYDirection(1);
                break;
            case PAN_LEFT:
                setXDirection(-1);
                break;
            case PAN_RIGHT:
                setXDirection(1);
                break;
        }
    }
}
package game.test.src;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.Rectangle;
导入javax.swing.ImageIcon;
公共阶级世界{
公共矩形[]街区;
公共布尔[]isSolid;
公众形象【】区块img;
公共最终int arrayNum=500;
//块图像
公众形象挡草、挡土、挡石、挡天;
私有int x,y,xDirection,yDirection;;
//地图导航
静态最终内平移上=0,平移下=1,平移左=2,平移右=3;
公共世界(){
BLOCK_GRASS=新图像图标(“H:/2D游戏测试/游戏测试2/src/game/test/src/images/tile_GRASS.png”).getImage();
BLOCK_DIRT=new ImageIcon(“H:/2D游戏测试/游戏测试2/src/game/test/src/images/tile_DIRT.png”).getImage();
BLOCK_STONE=new ImageIcon(“H:/2D游戏测试/游戏测试2/src/game/test/src/images/tile_STONE.png”).getImage();
BLOCK_SKY=new ImageIcon(“H:/2D游戏测试/游戏测试2/src/game/test/src/images/tile_SKY.png”).getImage();
块=新矩形[500];
blockImg=新图像[500];
isSolid=新布尔[arrayNum];
loadArrays();
}
私有void加载数组(){
for(int i=0;i=500){
x=0;
y+=20;
}
如果(i>=0&&i<100){
blockImg[i]=BLOCK_天空;
isSolid[i]=假;
块[i]=新矩形(x,y,20,20);
}
如果(i>=100&&i<125){
blockImg[i]=BLOCK_GRASS;
isSolid[i]=真;
块[i]=新矩形(x,y,20,20);
}
如果(i>=125&&i<225){
blockImg[i]=块土;
isSolid[i]=真;
块[i]=新矩形(x,y,20,20);
}
如果(i>=225&&i<500){
blockImg[i]=块石;
isSolid[i]=真;
块[i]=新矩形(x,y,20,20);
}
x+=20;
}
}
公共空间绘制(图g){
for(int i=0;i
这是我的Player.java

package game.test.src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

public class Player {

    static final int MOVE_UP = 0, MOVE_DOWN = 1, MOVE_LEFT= 2, MOVE_RIGHT = 3;
    private World world;

    private Rectangle playerRect;
    private Image playerImg;

    //Block Variables
    private int hoverX, hoverY;
    private boolean hovering = false;

    protected static int xDirection;
    protected static int yDirection;
    private Weapon weapon;

    public Player(World world){
        this.world = world;
        playerImg = new ImageIcon("H:/2D game test/Game test 2/src/game/test/src/images/Character.png").getImage();
        playerRect = new Rectangle(50, 0, 10, 36);
        weapon = new Weapon(weapon.PICKAXE);
    }
    private static void setXDirection(int d){
        xDirection = d;

    }
    private static void setYDirection(int d){
        yDirection = d;

    }
    public void update()
    {
        move();
        checkForCollision();

    }
    private void checkForCollision() {



    }
    private void move()
    {
        playerRect.x += xDirection;
        playerRect.y += yDirection;
        gravity();
    }
    private void gravity()
    {
        for(int i=0;i<world.arrayNum; i++)
        {
            if(!world.isSolid[i])
            {
                setYDirection(1);

            }
            else if(world.isSolid[i] && playerRect.intersects(world.blocks[i]))
            {
                setYDirection(0);
            }
        }
    }
    //MotionEvents

    public void mousePressed(MouseEvent e)
    {

    }

    public void mouseReleased(MouseEvent e)
    {

    }

    public void mouseClicked(MouseEvent e)
    {

    }

    public void mouseMoved(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        int px = playerRect.x;
        int py = playerRect.y;
        for(int i = 0; i < world.arrayNum; i++)
        {
            if(weapon.isEquipped(Weapon.PICKAXE) &&
                    x > world.blocks[i].x && x < world.blocks[i].x + world.blocks[i].width && 
                    y > world.blocks[i].x && y < world.blocks[i].y + world.blocks[i].height && world.isSolid[i] && 
                    (world.blocks[i].x + (world.blocks[i].width / 2) ) <= (px + playerRect.width/2) + weapon.WEAPON_RADIUS &&
                    (world.blocks[i].x + (world.blocks[i].width / 2) ) >= (px + playerRect.width/2) - weapon.WEAPON_RADIUS &&
                    (world.blocks[i].y + (world.blocks[i].height / 2) ) <= (py + playerRect.height/2) + weapon.WEAPON_RADIUS &&
                    (world.blocks[i].y + (world.blocks[i].height / 2) ) >= (py + playerRect.height/2) - weapon.WEAPON_RADIUS)

            {
                hovering = true;
                hoverX = world.blocks[i].x;
                hoverY = world.blocks[i].y;
                break;
            }
            else
                hovering = false;
        }
    }
    public void mouseDragged(MouseEvent e)
    {

    }
    public void mouseEntered(MouseEvent e)
    {

    }
    public void mouseExited(MouseEvent e)
    {

    }

    //Drawing Methods
    public void draw(Graphics g)
    {
        g.drawImage(playerImg, playerRect.x, playerRect.y, null);
        if(hovering)
            drawBlockOutline(g);

    }

    private void drawBlockOutline(Graphics g)
    {
        g.setColor(Color.black);
        g.drawRect(hoverX, hoverY, world.blocks[0].width,world.blocks[0].height);
    }



    private class Weapon
    {
        public static final int UNARMED = 0;
        public static final int PICKAXE = 1; 
        public static final int GUN = 2;

        public int CURRENT_WEAPON;

        public int WEAPON_RADIUS;

        public Weapon(int w)

        {
            switch(w)
            {
                default:
                    System.out.println("No weapon selected");
                    break;
                case UNARMED:
                    CURRENT_WEAPON = UNARMED;
                    WEAPON_RADIUS = 100;
                    break;

                case PICKAXE:
                    CURRENT_WEAPON = PICKAXE;
                    WEAPON_RADIUS = 100;
                    break;

                case GUN:
                    CURRENT_WEAPON = GUN;
                    WEAPON_RADIUS = 100;
                    break;

            }

        }

        public void selectWeapon(int w)
        {
            switch(w)
            {
                default:
                    System.out.println("No weapon selected");
                    break;
                case UNARMED:
                    CURRENT_WEAPON = UNARMED;
                    WEAPON_RADIUS = 100;
                    break;

                case PICKAXE:
                    CURRENT_WEAPON = PICKAXE;
                    WEAPON_RADIUS = 100;
                    break;

                case GUN:
                    CURRENT_WEAPON = GUN;
                    WEAPON_RADIUS = 100;
                    break;

            }

        }

        public boolean isEquipped(int w)

        {
            if(w == CURRENT_WEAPON)
            {
                return true;
            }
            else
                return false;



        }


    }
    public void moveMap(){
        for(Rectangle r : world.blocks){
            r.x += xDirection;
            r.y += yDirection;
        }
    }
    public static void stopMoveMap(){
        setXDirection(0);
        setYDirection(0);
    }
    private static void setXDirection1(int dir){
        xDirection = dir;
    }
    private static void setYDirection1(int dir){
        yDirection = dir;
    }

    public static void navigatePlayer(int nav){
        switch(nav){
            default:
                System.out.println("default case entered... Doing nothing.");
                break;
            case MOVE_UP:
                setYDirection1(-1);
                break;
            case MOVE_DOWN:
                setYDirection1(1);
                break;
            case MOVE_LEFT:
                setXDirection1(-1);
                break;
            case MOVE_RIGHT:
                setXDirection1(1);
                break;
        }
    }
}
package game.test.src;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.Rectangle;
导入java.awt.event.MouseEvent;
导入javax.swing.ImageIcon;
公开课选手{
静态最终整数上移=0,下移=1,左移=2,右移=3;
私人世界;
私人矩形播放器等;
私人形象扮演者;
//块变量
私有int hoverX,hoverY;
私有布尔悬停=false;
受保护的静态int xDirection;
受保护的静态输入方向;
私人武器;
公共玩家(世界){
这个世界=世界;
playerImg=newimageicon(“H:/2D游戏测试/game测试2/src/game/test/src/images/Character.png”).getImage();
playerRect=新矩形(50,0,10,36);
武器=新武器(武器.鹤嘴锄);
}
私有静态void setXDirection(int d){
xDirection=d;
}
私有静态void setYDirection(int d){
y方向=d;
}
公共无效更新()
{
move();
检查碰撞();
}
私有void checkForCollision(){
}
私人空位移动()
{
playerRect.x+=xDirection;
playerRect.y+=y方向;
重力();
}
私人空间重力()
{
对于(int i=0;i world.blocks[i].x&&xworld.blocks[i].x&&y