如何建立一个Java的瓷砖地图2D游戏? [英] How to build a Tiled map in Java for a 2D game?

查看:195
本文介绍了如何建立一个Java的瓷砖地图2D游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道如何解决这个问题。

Not sure how to approach this problem.

基本上,我想要一个像素 - >瓷砖重新$ P $ 400x400的窗口psentation。在屏幕上,例如在每个坐标 120x300 应该是一个瓷砖的一部分。我最小的精灵为4像素,因此我们可以说,1平铺= 4个像素。玩家和敌人的精灵都是20×20,因此每个球员/坏家伙将占用5瓷砖。

Basically, I want a Pixel -> Tile representation of a 400x400 window. Each coordinate on the screen, e.g 120x300 should be part of a tile. My smallest sprite is 4 pixels, so we can say that 1 tile = 4 pixels. The player and enemy sprites are all 20 x 20, so each player/bad guy will occupy 5 tiles.

然后,我想用这个地图类:

Then I want to use this Map class to:

  • 由suppling瓦的索引/ ID检索玩家/怪物精灵的x / y坐标。

  • Retrieve the x/y coordinates of a player/monster sprite by suppling the index/id of the tile.

知道哪里是界限,因此它不会移动精灵超越 400×400 ,从而隐藏了。

Knowing where the boundaries are, so it doesn't move the sprite beyond 400x400, thus hiding it.

碰撞检测,知道瓦是否空置或没有。

Collision detection, knowing whether a tile is vacant or not.

如何才能做到这一点?说到具体围绕X,Y>瓷砖或瓷砖首页 - > X,Y转换(适当地绘制精灵)在这里。

How can this be done? Talking specifically about the x,y->tile or tile index->x,y conversion (for drawing the sprites appropriately) here.

推荐答案

首先,分割出一个像素,它只是涉及重新presentation,用砖,这是制约​​一个实际的游戏对象的概念它把在游戏中。

Firstly, split out the concept of a pixel, which is just concerned with representation, with a tile, which is an actual game object with constraints it places on the game.

我找到解开这样的事情是开始了勾画出你想要的粗糙API的好办法。是这样的:

I find a good way to disentangle things like this is to start out sketching out the rough API of what you want. Something like:

public class Board {
  public Board (int width, int height){.. }
  public boolean isOccupied(int x, int y){.. }
  public void moveTo(Point from, Point to) { .. 
    (maybe throws an exception for outofbounds )

其中,董事会内部的所有单位都在地砖,而不是像素。 然后像素信息可以从电路板independantly来源于瓷砖重新presentation有位内部乘法 -

where all internal units of the board are in tiles, not pixels. Then pixel information can be derived from the board independantly from the tile representation with a bit of internal multiplication-

  public Point getPixelPosition(int xTilePos, int yTilePos, int pixelsPerTile)..

瓷砖可以内部重新psented作为一个二维数组或单个阵列,在这种情况下,你使用某种类型的内部重新presentation计划到阵列映射到板方块,因此$ P $ MOD运算。​​

The tiles can be internally represented as a 2d array or a single array, in which case you'd use some kind of internal representation scheme to map your array to the board squares, thus the mod arithmetic.

这篇关于如何建立一个Java的瓷砖地图2D游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆