为我的Java游戏添加偏移量
因此,正如标题所述,我正在尝试为我的java游戏添加偏移量。一位朋友给了我一个提示,我需要减去在屏幕上渲染瓷砖的偏移量 所以我创建了一个随机世界生成器并进行了偏移,但我遇到了一个问题 我的代码:为我的Java游戏添加偏移量,java,scroll,Java,Scroll,因此,正如标题所述,我正在尝试为我的java游戏添加偏移量。一位朋友给了我一个提示,我需要减去在屏幕上渲染瓷砖的偏移量 所以我创建了一个随机世界生成器并进行了偏移,但我遇到了一个问题 我的代码: public void generateMap(Graphics g) { block = seed.nextInt(2); //Render Dirt if(block == 0) { g.drawImage(Assets.d
public void generateMap(Graphics g) {
block = seed.nextInt(2);
//Render Dirt
if(block == 0) {
g.drawImage(Assets.dirt, x - GameState.xOffset, y - GameState.yOffset, null);
x += 32;
}
//Render Grass
if(block == 1) {
g.drawImage(Assets.grass, x - GameState.xOffset, y - GameState.yOffset, null);
x += 32;
}
//Check Where the X is
if(x > xFinish) {
if(y < yFinish) {
x = xStart;
y += 32;
}
}
}
完成后,我运行它,但它会执行以下操作:
有什么简单的方法可以解决这个问题,使屏幕向左“滚动”吗?有什么简单的方法可以解决这个问题吗… 可能不会。游戏很复杂。不要因此而打消你的念头 您正在生成游戏世界,并使用相同的方法绘制-您不想这样做。这一点非常重要——您不希望在一个地方有一大堆代码来做同样的事情。在这种情况下,需要拆分生成世界和图形代码的功能 对于世界生成,生成一次游戏世界,并使用您喜欢的任何格式将其保存到存储器中。请远离图纸代码-它在那里没有位置
表示在你的世界中的块,考虑这样的事情:
class Block {
public BlockType getType() {
return type;
}
public int getxPosition() {
return xPosition;
}
public int getyPosition() {
return yPosition;
}
// hashCode(), equals(), etc omitted, they should be implemented
public static enum BlockType {
Dirt(Assets.dirt),
Grass(Assets.grass);
private final BufferedImage image;
BlockType(BufferedImage image) {
this.image = image;
}
public BufferedImage getImage() {
return image;
}
}
private final BlockType type;
private final int xPosition;
private final int yPosition;
private Block(BlockType type, int xPosition, int yPosition) {
this.type = type;
this.xPosition = xPosition;
this.yPosition = yPosition;
}
public static Block getInstance(BlockType type, int xPosition, int yPosition) {
return new Block(type, xPosition, yPosition);
}
}
然后可以使用Block.getInstance()
生成一次映射,如下所示:
class GameState {
private final int WORLD_SIZE = 1024;
private Block[][] _world = new Block[WORLD_SIZE][WORLD_SIZE];
private static Random seed = new Random();
public void generateMap() {
int blockTypeLength = Block.BlockType.values().length;
for (int x = 0; x < WORLD_SIZE; x++) {
for (int y = 0; y < WORLD_SIZE; y++) {
int blockType = seed.nextInt(blockTypeLength);
_world[x][y] = Block.getInstance(Block.BlockType.values()[blockType], x, y);
}
}
}
public Block[][] getMap() {
return _world; // not thread safe, shares internal state, all the usual warnings
}
如果这有帮助,那太好了!我很高兴能帮上忙。如果没有,或者如果一些想法还不清楚,那么我会考虑也许通过教程或书籍,让你通过制作游戏。在这样的过程中,不要忘记学习您正在编写代码的平台 您是否有一些关于您在
资产
,游戏状态
等类型中使用的API的信息(最好是名称)?另外,你到底想达到什么样的效果——无论发生什么事情,都能达到滚动效果?有什么不同吗?其实没什么好说的,资产只是存储了BuffereImage图片,所以我不必每次都加载它们。GameState类有tick方法,每秒调用generateMap方法大约60次。我正在尝试在地图上导航,但仍然将播放器放在一个地方,希望这是有意义的。这不是滚动,还是滚动,而是在屏幕截图中产生效果?这很有帮助。:)谢谢,我还有最后一个问题,如果我想让infinate世界一代(像在游戏minecraft中那样有块),我会怎么做?因为在你的代码中,它只生成一次世界,就是这样。
class GameState {
private final int WORLD_SIZE = 1024;
private Block[][] _world = new Block[WORLD_SIZE][WORLD_SIZE];
private static Random seed = new Random();
public void generateMap() {
int blockTypeLength = Block.BlockType.values().length;
for (int x = 0; x < WORLD_SIZE; x++) {
for (int y = 0; y < WORLD_SIZE; y++) {
int blockType = seed.nextInt(blockTypeLength);
_world[x][y] = Block.getInstance(Block.BlockType.values()[blockType], x, y);
}
}
}
public Block[][] getMap() {
return _world; // not thread safe, shares internal state, all the usual warnings
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Block[][] screen = new Block[16][16]; // declare a screen buffer to draw
// Assumes player is in the center of the screen
int screenRadiusX = GameFrame.Assets.SCREENBOUNDS_X / 2 / blockSize;
int screenRadiusY = GameFrame.Assets.SCREENBOUNDS_Y / 2 / blockSize;
for (int x = state.playerX - 8, xS = 0; x < state.playerX + 8; x++, xS++) {
for (int y = state.playerY - 8, yS = 0; y < state.playerY + 8; y++, yS++) {
screen[xS][yS] = world[x][y];
}
}
for (int x = 0; x < screen.length; x++) {
for (int y = 0; y < screen.length; y++) {
Block current = screen[x][y];
g.drawImage(current.getType().getImage(),
x * blockSize, // blockSize is the pixel dimension of
y * blockSize,
null
);
}
}
}