gpt4 book ai didi

java - 我如何在基于图 block 的 Android 游戏中检查碰撞?

转载 作者:行者123 更新时间:2023-11-29 21:58:18 24 4
gpt4 key购买 nike

我如何检查基于图 block 的游戏中的碰撞?我看过 android SDK 的示例,但我无法理解它们,因为我是 Android 编程的新手。

这是我的英雄参加的我的 map 和图 block 类(class),因此我可以让他在世界各地移动

 class Map{
int X = 0;
int Y = 0;
int tileSize = 30;

int [][]map= {
//0 = empty tile
//1 = wall
//2 = player
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,2,1,1,0,0,0,0,0,1},
{1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};



ArrayList<Tile> tiles = new ArrayList<Tile>();;

public Map(){
loadArray();
}

public void loadArray(){

for(int xTiles = 0; xTiles< map.length; xTiles++ ){
for(int yTiles = 0; yTiles< map[xTiles].length; yTiles++){
if(map[xTiles][yTiles]==1){//Wall
tiles.add(new Tile(X, Y, tileSize, map[xTiles][yTiles]));
tiles.get(tiles.size()-1).setColor(Color.GRAY);
tiles.get(tiles.size()-1).walkable = false;
X+=tileSize;
}else if(map[xTiles][yTiles]==0){//Empty Tile
tiles.add(new Tile(X, Y, tileSize, map[xTiles] [yTiles]));
X+=tileSize;
}else if(map[xTiles][yTiles]==2){//Player
tiles.add(new Tile(X, Y, tileSize, map[xTiles][yTiles]));
X+=tileSize;
}
}
Y+=tileSize;
X=0;
}
}

public void drawMap(Canvas canvas){
for(int index = 0; index < tiles.size(); index++){
tiles.get(index).drawTile(canvas);
}
}

}

class Tile{
private int type;
int X;
int Y;
int size;
boolean walkable = true;
Paint color = new Paint();;
//rect for the bounds
Rect bounds = new Rect();

public Tile(){
type = 0;
X = 0;
Y = 0;
size = 0;
color.setColor(Color.TRANSPARENT);
bounds.set(X, Y, X+size, Y+size);

}



public Tile(int X, int Y , int tileSize, int type){
this.type = type;
this.X = X;
this.Y = Y;
size = tileSize;
color.setColor(Color.TRANSPARENT);
bounds.set(X, Y, X+size, Y+size);
}



public int getX() {
return X;
}



public int getY() {
return Y;
}



public void setX(int x) {
X = x;
}



public void setY(int y) {
Y = y;
}



public int getType() {
// TODO Auto-generated method stub
return type;
}
public Paint getColor() {
return color;
}

public void setColor(int color) {
this.color.setColor(color);
}

public void drawTile(Canvas canvas){
canvas.drawRect(bounds, color);
}
}

这是我的英雄类(class)

 class Hero{
private static final int SIZE = 30;
//Coordinates
int X = 60;
int Y = 60;
int sx=30, sy=30;
private Paint paint = new Paint();
public Rect Bounds = new Rect(X,Y,X+30,Y+30);
boolean solid = true;

//Map Things
Map map;

public Hero(Map map){
this.map = map;
paint.setColor(Color.RED);
paint.setStyle(Style.FILL_AND_STROKE);

location();
}




private void location() {

for(int index = 0; index<map.tiles.size(); index++){
if(map.tiles.get(index).getType() == 2){
this.X = map.tiles.get(index).getX();
this.Y = map.tiles.get(index).getY();

}
}

}




public void move(int KeyCode) {
switch(KeyCode){

case KeyEvent.KEYCODE_DPAD_DOWN:
{if(Collision()){Y+=sy;} break;}
case KeyEvent.KEYCODE_DPAD_UP:
{Y-=sy; break;}
case KeyEvent.KEYCODE_DPAD_LEFT:
{X-=sx; break;}
case KeyEvent.KEYCODE_DPAD_RIGHT:
{X+=sx; break;}
}

}







private boolean Collision() {
for(int index = 0; index< map.tiles.size(); index++){
if(Rect.intersects(map.tiles.get(index).bounds, this.Bounds)){
return map.tiles.get(index).walkable;}
}
return false;
}




public int getX() {
return X;
}



public int getY() {
return Y;
}



public void setX(int x) {
X = x;
}



public void setY(int y) {
Y = y;
}


public void position(int x, int y){
X = x;
Y = y;

}

public void update(){
Bounds.set(X, Y, (X+SIZE), (Y+SIZE));

}


public void Draw(Canvas canvas){
canvas.drawRect(Bounds, paint);
canvas.drawText("Player", Bounds.left, Bounds.top-5, paint);
}
}

如何检查碰撞?我应该只循环每一个图 block ,然后检查玩家的新坐标是否会发生碰撞吗?

这可能不是正确的方法,但它确实有效。我像这样为每个移动方向右、左、上和下添加了方法

private void moveLeft() {
for(int index = 0 ; index < map.tiles.size();index++){
if(yCoord-1 == map.tiles.get(index).yCoord && xCoord == map.tiles.get(index).xCoord){
if(map.tiles.get(index).walkable){
X-=SIZE;
//My coordinates are flipped for some reason my Xs are Ys and Ys are Xs
yCoord-=1;
break;
}
}
}

}

最佳答案

你必须记录英雄的最后位置,在每次移动事件中你可以检查下一个方 block 相对于英雄的方向。当你在二维矩阵中实现时,如果英雄的位置是 map[0][0] 并且玩家一次下降一个位置,那么 map 中的相对位置即 map[1][0]。这样你就可以最小化比较。每个方向都有二维权重,例如 向上运动可以有 [CurrentPosition.x-1] [CurrentPosition.y-1],向左可以有 [CurrentPosition.x] [CurrentPosition.y+1] 等....

关于java - 我如何在基于图 block 的 Android 游戏中检查碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12648868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com