我有一个2维Minecraft克隆的处理脚本。我目前一直在放置块,因为我需要将它们捕捉到大小的网格上
block_size
。代码如下:
final float block_size = 8;
final float ground_level = 8;
final float sky_level = 256;
final float x_min = 0;
final float x_max = 256;
class Player {
float x;
float y;
boolean facing;
Player(float spawn_x, float spawn_y) {
x = spawn_x;
y = spawn_y;
}
void move() {
if (keyPressed) {
if (key == CODED) {
switch(key) {
case 'a':
x--;
facing = false;
break;
case 'd':
x++;
facing = true;
break;
case ' ':
y--;
}
} else {
switch(keyCode) {
case LEFT:
x--;
facing = false;
break;
case RIGHT:
x++;
facing = true;
break;
case UP:
y--;
}
}
}
}
void physics() {
if (y < ground_level) {
y++;
}
x = constrain(x, x_min, x_max);
y = constrain(y, ground_level, sky_level);
}
void place_block() {
}
void break_block() {
}
}
class Block {
Block(float x_, float y_) {
}
void display() {
}
}
Player player = new Player(2, (x_max - x_min) / 2);
ArrayList<Block> blocks = new ArrayList<Block>();
问题在于
Block(float x_, float y_)
构造函数。我需要确保
x
从不高于
x_
,尽可能接近
x个_
,也是
block\u大小
。与相同
y
。
我试过了
x = floor(x_ / block_size) * block_size;
,但它不起作用。有没有人有一两行我可以使用的代码?