gpt4 book ai didi

java - Android - 以平滑的角色移动在 Canvas 上绘制迷宫

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:43 26 4
gpt4 key购买 nike

我目前正在创建一个基于 Tile 的游戏,它基本上使用两组 boolean 数组来绘制迷宫,以确定每面墙需要绘制的位置。

我让这一切正常工作,只绘制了 5 x 5 的迷宫部分(整个迷宫大小为 30 x 30)。但是,我遇到的问题是当我移动角色时,整个屏幕会跳动,这是因为正在绘制迷宫的下一部分,以保持 5 x 5 的纵横比。我已经尝试了各种不同的方法来尝试使它顺利运行,但似乎无法做到这一点。

有人可以建议或指导我一些链接/示例,以便我可以顺利进行迷宫和角色移动。下面是关于当前绘制迷宫的主 for 循环的一些基本代码,以及它引用的 2 个 boolean 数组以绘制墙壁。

// THE CODE TO DRAW THE MAZE AND ITS WALLS
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
float x = j * totalCellWidth;
float y = i * totalCellHeight;

if(currentY != 0)
indexY = i + currentY - 1;
else
indexY = i + currentY;

if(currentX != 0)
indexX = j + currentX - 1;
else
indexX = j + currentX;

// Draw Verticle line (y axis)
if (indexY < vLength && indexX < vLines[indexY].length && vLines[indexY][indexX])
{
RectOuterBackground.set((int)x + (int)cellWidth, (int)y, (int)x + (int)cellWidth + 15, (int)y + (int)cellHeight + 15);
canvas.drawBitmap(walls, null, RectOuterBackground, null);
}
// Draws Horizontal lines (x axis)
if (indexY < hLength && indexX < hLines[indexY].length && hLines[indexY][indexX])
{
RectOuterBackground.set((int)x, (int)y + (int)cellHeight,(int)x + (int)cellWidth + 15,(int)y + (int)cellHeight + 15);
canvas.drawBitmap(walls, null, RectOuterBackground, null);
}
}
}

// THE 2 BOOLEAN ARRAYS THE FORLOOP REFERENCES
boolean[][] vLines = new boolean[][]{
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true },
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true },
{true ,true ,true ,false,false,false,true ,false,false,true ,true },
{true ,true ,true ,false,false,true ,false,true ,true ,true ,true },
{true ,true ,false,true ,false,false,true ,false,false,true ,true },
{true ,true ,false,true ,true ,false,false,false,true ,true ,true },
{true ,true ,true ,false,false,false,true ,true ,false,true ,true },
{true ,true ,false,true ,false,false,true ,false,false,true ,true },
{true ,true ,false,true ,true ,true ,true ,true ,false,true ,true },
{true ,true ,false,false,false,true ,false,false,false,true ,true },
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true },
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true }
};
boolean[][] hLines = new boolean[][]{
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true },
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true },
{true ,true ,false,false,true ,true ,false,false,true ,false,true ,true },
{true ,true ,false,false,true ,true ,false,true ,false,false,true ,true },
{true ,true ,true ,true ,false,true ,true ,false,true ,true ,true ,true },
{true ,true ,false,false,true ,false,true ,true ,false,false,true ,true },
{true ,true ,false,true ,true ,true ,true ,false,true ,true ,true ,true },
{true ,true ,true ,false,false,true ,false,false,true ,false,true ,true },
{true ,true ,false,true ,false,false,false,true ,false,true ,true ,true },
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true },
{true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true ,true }
};

如果我能顺利地绘制那部分代码,我愿意完全重写它,但是尝试了几种不同的变体却不能让它顺利,我总是有跳跃。

我也试图让它与 Canvas 翻译一起工作,通过绘制 whoel 迷宫,然后缩放它以便只显示 5 x 5 然后简单地移动缩放部分使用翻译,同时保持角色在屏幕中央,那里有平滑的运动。但是,我无法让翻译顺利进行。

最佳答案

这是一个完整的迷宫类。它将 View 绘制到坐标 viewX 和 viewY 上,它们可以平滑移动(如 Boudjnah 的回答)或乘以单元尺寸。

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;

public class Maze {
private RectF drawRect = new RectF();

private Bitmap[] bitmaps;
private int[][] tileType;

private float screenWidth, screenHeight;

/**
* Initialize a new maze.
* @param wallBitmap The desired bitmaps for the floors and walls
* @param isWall The wall data array. Each true value in the array represents a wall and each false represents a gap
* @param xCellCountOnScreen How many cells are visible on the screen on the x axis
* @param yCellCountOnScreen How many cells are visible on the screen on the y axis
* @param screenWidth The screen width
* @param screenHeight The screen height
*/
public Maze(Bitmap[] bitmaps, int[][] tileType, float xCellCountOnScreen, float yCellCountOnScreen, float screenWidth, float screenHeight){
this.bitmaps = bitmaps;
this.tileType = tileType;

this.screenWidth = screenWidth;
this.screenHeight = screenHeight;

drawRect.set(0, 0, screenWidth / xCellCountOnScreen, screenHeight / yCellCountOnScreen);
}

/**
* Get the type of the cell. x and y values are not coordinates!
* @param x The x index of the cell
* @param y The y index of the cell
* @return The cell type
*/
public int getType(int x, int y){
if(y < tileType.length && x < tileType[y].length) return tileType[y][x];
return 0;
}

public float getCellWidth(){ return drawRect.width(); }
public float getCellHeight(){ return drawRect.height(); }

/**
* Draws the maze. View coordinates should have positive values.
* @param canvas Canvas for the drawing
* @param viewX The x coordinate of the view
* @param viewY The y coordinate of the view
*/
public void drawMaze(Canvas canvas, float viewX, float viewY){
int tileX = 0;
int tileY = 0;
float xCoord = -viewX;
float yCoord = -viewY;

while(tileY < tileType.length && yCoord <= screenHeight){
// Begin drawing a new column
tileX = 0;
xCoord = -viewX;

while(tileX < tileType[tileY].length && xCoord <= screenWidth){
// Check if the tile is not null
if(bitmaps[tileType[tileY][tileX]] != null){

// This tile is not null, so check if it has to be drawn
if(xCoord + drawRect.width() >= 0 && yCoord + drawRect.height() >= 0){

// The tile actually visible to the user, so draw it
drawRect.offsetTo(xCoord, yCoord); // Move the rectangle to the coordinates
canvas.drawBitmap(bitmaps[tileType[tileY][tileX]], null, drawRect, null);
}
}

// Move to the next tile on the X axis
tileX++;
xCoord += drawRect.width();
}

// Move to the next tile on the Y axis
tileY++;
yCoord += drawRect.height();
}
}
}

要使用这个类,首先像这样在你的onCreate函数中初始化它

// 0 == floor, 1 == wall, 2 == different looking wall
int[][] maze = {
{0, 0, 0, 0, 0},
{0, 1, 2, 1, 0},
{0, 0, 0, 1, 0},
{1, 1, 2, 1, 0},
{0, 0, 0, 0, 0}
};

Bitmap[] bitmaps = {
BitmapFactory.decodeResource(getResources(), R.drawable.floor),
BitmapFactory.decodeResource(getResources(), R.drawable.firstwall)
BitmapFactory.decodeResource(getResources(), R.drawable.secondwall)
};

// Chance the 480 and 320 to match the screen size of your device
maze = new Maze(bitmaps, maze, 5, 5, 480, 320);

然后在你的 onDraw 或类似的函数中绘制它

maze.draw(canvas, viewX, viewY);

getType(int x, int y)、getCellWidth() 和 getCellHeight() 函数应该可以帮助您实现与迷宫的任何交互。您还可以将自己的函数添加到类中。您还可以修改此类以记录任何开始和结束的图 block 。我不会为你制作整个游戏:)

如果您将角色设置为坐标 (x, y) 并将其绘制到坐标 (x - viewX, y - viewY),无论 View 位于何处,您都可以相对于迷宫拥有一个固定的位置。

希望对您有所帮助!

关于java - Android - 以平滑的角色移动在 Canvas 上绘制迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747138/

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