当前位置: 首页 > 知识库问答 >
问题:

基于图块的游戏上的重复图块

林玮
2023-03-14

我目前正在使用Java2D开发一款基于瓷砖的Java游戏。但是,我认为我错过了一些非常愚蠢的东西,或者可能不是,问题是我检查了我的代码数千次,我没有看到任何奇怪的东西。这就是我现在的游戏的印刷品:

(灰色瓷砖是砖,绿色瓷砖是草,棕色瓷砖是土)

所以砖块以某种奇怪的方式复制。这是我的关卡代码:

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import com.wg.MainComponent;
import com.wg.entity.Entity;
import com.wg.entity.Particle;
import com.wg.entity.Projectile;
import com.wg.entity.mob.Mob;
import com.wg.entity.mob.Player;

public abstract class Level {

    public BufferedImage levelImage;
    public BufferedImage[][] tile;
    public Rectangle[][] rect;
    public int tileSize;
    public float xOffset, yOffset;
    public ArrayList<BufferedImage> collision = new ArrayList<BufferedImage>();

    public ArrayList<Player> players = new ArrayList<Player>();
    public ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
    public ArrayList<Particle> particles = new ArrayList<Particle>();
    private List<Entity> entities = new ArrayList<Entity>();

    public BufferedImage[] tilesBufferedImages;
    public int[] xCollisionOffset;
    public int[] yCollisionOffset;

    public Level(BufferedImage levelImage, int tileSize, BufferedImage[] tilesBufferedImages, int[] xCollisionOffset, int[] yCollisionOffset) {
        this.tilesBufferedImages = tilesBufferedImages;
        this.xCollisionOffset = xCollisionOffset;
        this.yCollisionOffset = yCollisionOffset;

        this.levelImage = levelImage;
        this.tileSize = tileSize;

        tile = new BufferedImage[levelImage.getWidth()][levelImage.getHeight()];
        rect = new Rectangle[levelImage.getWidth()][levelImage.getHeight()];

        generateLevel();
    }

    public abstract void generateLevel();

    public void render(float xOffset, float yOffset, int scale, Graphics2D screen) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;

        for (int y = (int) Math.max(yOffset / (tileSize + scale), 0); y < Math.min((MainComponent.height + yOffset) / (tileSize + scale) + 1, levelImage.getHeight()); y++) {
            for (int x = (int) Math.max(xOffset / (tileSize + scale), 0); x < Math.min((MainComponent.width + xOffset) / (tileSize + scale) + 1, levelImage.getWidth()); x++) {
                if (tile[x][y] != null)
                    screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
            }
        }

        for (int i = 0; i < entities.size(); i++) {
            entities.get(i).render(screen);
        }

        for (int i = 0; i < players.size(); i++) {
            players.get(i).render(screen);
        }

        for (int i = 0; i < projectiles.size(); i++) {
            projectiles.get(i).render(screen);
        }

        for (int i = 0; i < particles.size(); i++) {
            particles.get(i).render(screen);
        }
    }

    public void add(Entity e) {
        if (e instanceof Particle) {
            particles.add((Particle) e);
        } else if (e instanceof Player) {
            players.add((Player) e);
        } else if (e instanceof Projectile) {
            projectiles.add((Projectile) e);
        } else {
            entities.add(e);
        }
    }

    public void update(int scale) {
        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                if (tile[x][y] != null && rect[x][y] == null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y] = new Rectangle(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + yCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                } else if (tile[x][y] != null && rect[x][y] != null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y].setBounds(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + xCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                }
            }
        }

        for (int i = 0; i < entities.size(); i++) {
            entities.get(i).update();
        }

        for (int i = 0; i < players.size(); i++) {
            players.get(i).update();
        }

        for (int i = 0; i < projectiles.size(); i++) {
            projectiles.get(i).update();
        }
        for (int i = 0; i < particles.size(); i++) {
            particles.get(i).update();
        }

        remove();
    }

    public List<Player> getPlayers() {
        return players;
    }

    public Player getPlayerAt(int index) {
        return players.get(index);
    }

    public Player getClientPlayer() {
        return players.get(0);
    }

    private void remove() {
        for (int i = 0; i < entities.size(); i++) {
            if (entities.get(i).isRemoved())
                entities.remove(i);
        }
        for (int i = 0; i < projectiles.size(); i++) {
            if (projectiles.get(i).isRemoved())
                projectiles.remove(i);
        }
        for (int i = 0; i < players.size(); i++) {
            if (players.get(i).isRemoved())
                players.remove(i);
        }
        for (int i = 0; i < particles.size(); i++) {
            if (particles.get(i).isRemoved())
                particles.remove(i);
        }
    }

    public boolean tileDownCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y + en.height));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y + en.height - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean tileUpCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y - 1));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y - 1 - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean tileLeftCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x - 1), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x - 1 - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean tileRightCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x + en.width), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x + en.width - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileDownCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y + en.height));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y + en.height - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileUpCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y - 1));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y - 1 - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileLeftCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x - 1), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x - 1 - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileRightCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x + en.width), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x + en.width - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean mobProjectileCollision(Mob en, Projectile pr, boolean fixed) {
        Rectangle p1 = null;
        if (fixed)
            p1 = new Rectangle(Math.round(en.x), Math.round(en.y), en.width, en.height);
        else if (!fixed)
            p1 = new Rectangle(Math.round(en.x - xOffset), Math.round(en.y - yOffset), en.width, en.height);

        Rectangle p2 = null;
        if (fixed)
            p2 = new Rectangle(Math.round(pr.x), Math.round(pr.y), pr.width, pr.height);
        else if (!fixed)
            p2 = new Rectangle(Math.round(pr.x - xOffset), Math.round(pr.y - yOffset), pr.width, pr.height);

        if (p1.intersects(p2))
            return true;

        return false;
    }

    public double calcDistance(Entity e1, Entity e2) {
        return Math.sqrt(((e1.x - e2.x) * (e1.x - e2.x)) + ((e1.y - e2.y) * (e1.y - e2.y)));
    }
}

级别被渲染到渲染(Graphics2D)方法中。它是一个抽象类,所以我有一个子类,Level1,如果它是图像中的一个。

import static com.wg.MainComponent.spritesheet;

import java.awt.Color;
import java.awt.image.BufferedImage;

import com.wg.BufferedImageLoader;
import com.wg.entity.mob.Player;

public class Level1 extends Level {

    public static final BufferedImage bricks = crop(0, 2);
    public static final BufferedImage dirt = crop(1, 2);
    public static final BufferedImage grass = crop(2, 2);
    public static final BufferedImage[] tilesList = { bricks, dirt, grass };
    public static final int[] defaultxCollisionOffset = { 0, 0, 0 };
    public static final int[] defaultyCollisionOffset = { 0, 0, 0 };
    private static BufferedImage level = BufferedImageLoader.load("/level1.png");

    private static BufferedImage crop(int x, int y) {
        return spritesheet.getSubimage(x * 16, y * 16, 16, 16);
    }

    public void generateLevel() {
        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                Color c = new Color(level.getRGB(x, y));
                String data = String.format("%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue());
                if (data.equals("838383"))
                    tile[x][y] = bricks;
                else if (data.equals("bea100"))
                    tile[x][y] = dirt;
                else if (data.equals("23d200"))
                    tile[x][y] = grass;
            }
        }
    }

    public Level1(float xOffset, float yOffset) {
        super(level, 16, tilesList, defaultxCollisionOffset, defaultyCollisionOffset);
        collision.add(grass);
        collision.add(dirt);
        collision.add(bricks);
        add(new Player(50, 20, 1, 2, 32 + 32, 32 + 32, this));
        this.xOffset = xOffset;
        this.yOffset = yOffset;
    }
}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class MCVE extends JFrame {

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    private MCVELevel1 level1 = new MCVELevel1(0, 0);

    public static void main(String[] args) {
        MCVE mcve = new MCVE();
        mcve.setVisible(true);
        mcve.setSize(WIDTH, HEIGHT);
        mcve.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        level1.render(50, 400, 32, (Graphics2D) g);
    }

    private static class MCVELevel1 extends MCVELevel {

        private static BufferedImage spritesheet = BufferedImageLoader.load("C://image//tiles.png");
        public static final BufferedImage bricks = crop(0, 0);
        public static final BufferedImage dirt = crop(1, 0);
        public static final BufferedImage grass = crop(2, 0);
        public static final BufferedImage[] tilesList = { bricks, dirt, grass };
        public static final int[] defaultxCollisionOffset = { 0, 0, 0 };
        public static final int[] defaultyCollisionOffset = { 0, 0, 0 };
        private static BufferedImage level = BufferedImageLoader.load("C://image//level1.png");

        private static BufferedImage crop(int x, int y) {
            return spritesheet.getSubimage(x * 16, y * 16, 16, 16);
        }

        public void generateLevel() {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    Color c = new Color(level.getRGB(x, y));
                    String data = String.format("%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue());
                    if (data.equals("838383"))
                        tile[x][y] = bricks;
                    else if (data.equals("bea100"))
                        tile[x][y] = dirt;
                    else if (data.equals("23d200"))
                        tile[x][y] = grass;
                }
            }
        }

        public MCVELevel1(float xOffset, float yOffset) {
            super(level, 16, tilesList, defaultxCollisionOffset, defaultyCollisionOffset);
            this.xOffset = xOffset;
            this.yOffset = yOffset;
        }
    }

    public static class BufferedImageLoader {
        public static BufferedImage load(String path) {
            try {
                return ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + path);
        }

        public static BufferedImage load(URL url) {
            try {
                return ImageIO.read(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + url.getPath());
        }
    }
}

MCVELevel类:

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public abstract class MCVELevel {

    public BufferedImage levelImage;
    public BufferedImage[][] tile;
    public Rectangle[][] rect;
    public int tileSize;
    public float xOffset, yOffset;

    public BufferedImage[] tilesBufferedImages;
    public int[] xCollisionOffset;
    public int[] yCollisionOffset;

    public MCVELevel(BufferedImage levelImage, int tileSize, BufferedImage[] tilesBufferedImages, int[] xCollisionOffset, int[] yCollisionOffset) {
        this.tilesBufferedImages = tilesBufferedImages;
        this.xCollisionOffset = xCollisionOffset;
        this.yCollisionOffset = yCollisionOffset;

        this.levelImage = levelImage;
        this.tileSize = tileSize;

        tile = new BufferedImage[levelImage.getWidth()][levelImage.getHeight()];
        rect = new Rectangle[levelImage.getWidth()][levelImage.getHeight()];

        generateLevel();
    }

    public abstract void generateLevel();

    public void render(float xOffset, float yOffset, int scale, Graphics2D screen) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;

        for (int y = (int) Math.max(yOffset / (tileSize + scale), 0); y < Math.min((MCVE.HEIGHT + yOffset) / (tileSize + scale) + 1, levelImage.getHeight()); y++) {
            for (int x = (int) Math.max(xOffset / (tileSize + scale), 0); x < Math.min((MCVE.WIDTH + xOffset) / (tileSize + scale) + 1, levelImage.getWidth()); x++) {
                if (tile[x][y] != null)
                    screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
            }
        }
    }

    public void update(int scale) {
        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                if (tile[x][y] != null && rect[x][y] == null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y] = new Rectangle(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + yCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                } else if (tile[x][y] != null && rect[x][y] != null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y].setBounds(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + xCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                }
            }
        }
    }
}

非常感谢。

编辑:在MCVELevel更新代码:

public void render(float xOffset, float yOffset, int scale, Graphics2D screen) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;

        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                if (tile[x][y] != null)
                    screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
            }
        }
    }

共有2个答案

解高昂
2023-03-14

(我的另一个答案是正确的,但它没有回答具体问题;它修复了一个稍后会变得明显的问题。)

我花了一段时间才弄明白。我用你的创造了一个更小的MCVE。这个版本可能会帮助未来的读者调试原png(问题中)的问题。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class MCVE extends JFrame {
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;

    public static void main(String[] args) {
        MCVE mcve = new MCVE();
        mcve.setVisible(true);
        mcve.setSize(WIDTH, HEIGHT);
        mcve.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        render((Graphics2D) g);
    }

    public BufferedImage[][] tile;
    public static final int TILE_SIZE = 16;
    private static BufferedImage spritesheet = BufferedImageLoader
            .load("tiles.png");
    private static final BufferedImage bricks = crop(0, 0);
    private static final BufferedImage dirt = crop(1, 0);
    private static final BufferedImage grass = crop(2, 0);
    private static final BufferedImage levelImage = BufferedImageLoader.load("level1.png");
    int widthInTiles;
    int heightInTiles;

    private static BufferedImage crop(int x, int y) {
        return spritesheet.getSubimage(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE,
                TILE_SIZE);
    }

    public MCVE() {
        widthInTiles = levelImage.getWidth();
        heightInTiles = levelImage.getHeight();

        generateLevel();
    }

    public void generateLevel() {
        tile = new BufferedImage[widthInTiles][heightInTiles];
        for (int y = 0; y < heightInTiles; y++) {
            for (int x = 0; x < widthInTiles; x++) {
                Color c = new Color(levelImage.getRGB(x, y));
                switch (c.getRGB()) {
                case -8158333:
                    tile[x][y] = bricks;
                    break;
                case -14429696:
                    tile[x][y] = grass;
                    break;
                case -4284160:
                    tile[x][y] = dirt;
                }
            }
        }
    }

    public void render(Graphics2D screen) {
        int scale = 16;
        int yes = 0, no = 0;
        for (int y = 0; y < heightInTiles; y++) {
            for (int x = 0; x < widthInTiles; x++) {
                if (tile[x][y] != null) {
                    screen.drawImage(tile[x][y], (int) (x * (TILE_SIZE + scale)),
                            (int) (y * (TILE_SIZE + scale)),
                            (TILE_SIZE + scale), (TILE_SIZE + scale),
                            Color.BLACK, null);
                }
            }
        }
    }

    public static class BufferedImageLoader {
        public static BufferedImage load(String path) {
            try {
                return ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + path);
        }

        public static BufferedImage load(URL url) {
            try {
                return ImageIO.read(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + url.getPath());
        }
    }
}
王景山
2023-03-14

问题在于render方法中的块:

for (int y = (int) Math.max(yOffset / (tileSize + scale), 0); y < Math.min((MCVE.HEIGHT + yOffset) / (tileSize + scale) + 1, levelImage.getHeight()); y++) {
    for (int x = (int) Math.max(xOffset / (tileSize + scale), 0); x < Math.min((MCVE.WIDTH + xOffset) / (tileSize + scale) + 1, levelImage.getWidth()); x++) {
        if (tile[x][y] != null)
            screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
    }
}

这似乎是在说:“对于我想画的像素,画图块”(尽管我可能错了..每次我读for-loop guard时都会得到不同的解释)。问题在于,您是根据绘图区域来计算x和y的,但是您是根据< code>tiles[]数组的索引来使用它们的。你想要“对于我想画的瓷砖,画瓷砖”。

generateLevel() 中的循环正在做正确的事情。使用这种类型的循环(for (y = 0 y

这再次提醒我为什么代码应该编写为可测试的,以及为什么测试应该编写:)

 类似资料:
  • 本文向大家介绍基于JS实现简单滑块拼图游戏,包括了基于JS实现简单滑块拼图游戏的使用技巧和注意事项,需要的朋友参考一下 成品效果   总结 以上所述是小编给大家介绍的基于JS实现简单滑块拼图游戏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持! 如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

  • 问题内容: 该脚本绘制控件,英雄,表面和地图: 有一个英雄,当玩家使用控件移动他时必须重新绘制,所有其他可绘制对象也必须重新绘制。问题在于绘制地图是一个漫长的过程,因此我创建的地图越大,英雄移动的速度就越慢,因为必须绘制地图的每个图块。有没有一种方法可以将所有图块以其他方法放置到一个位图,然后在canvas方法中绘制该位图? 问题答案: 最好的选择是只绘制地图在屏幕上可见的部分。这样,无论整个地图

  • 在我的大学课程中,我们必须创建一个基于文本的游戏,在这个游戏中,你可以进出房间收集物品。我仍在尝试掌握python和一般编码的诀窍,因此我一直在努力完成这个项目。在大多数情况下,我的代码可以正常工作,但我可以多次添加一个项目。如何将其添加到库存中,然后从文件室中删除,以防止用户多次添加?

  • 本文向大家介绍基于Android平台实现拼图小游戏,包括了基于Android平台实现拼图小游戏的使用技巧和注意事项,需要的朋友参考一下 一、需求描述 拼图是一款益智类经典游戏了,本游戏学习了一些前辈们的经验,整体来说讲,将图片用切图工具进行切割,监听用户手指滑动事件,当用户对凌乱的图片,在一定的时间内拼凑恢复成原来的样子,则成功闯关。 根据游戏不同的关卡对图片进行动态的切割。玩家可以在随意交换任意

  • 我正在尝试用SFML 2.1为我的2D超级马里奥兄弟克隆包碰撞。我尝试了很多不同的解决方案和想法,但我无法让任何东西正常工作。 我有一个播放器,它目前在自己的更新()中检查碰撞(如下所示) 当前: 1. 我的碰撞检测到碰撞略微偏移。如果你能找到任何明显的错误,我会很高兴知道。 2.我不确定这是否是一个足够好的解决方案,因为我需要检测4个方向的碰撞(跳跃,撞到我的头块。摔倒在方块上,左右碰撞) 3.

  • 目前,我正在尝试用Java开发基于2D平铺的侧滚游戏,主要基于David Brackeen的“用Java开发游戏”中的代码和示例 目前地图文件的大小为100x100个图块(每个图块为64x64像素)。我已经将系统配置为仅显示玩家可见的图块。Graphics系统由ScreenManager类管理,该类返回当前BufferStrategy的图形对象,如下所示: ScreenManager.java 来