我是一个试图制作俄罗斯方块的新手。现在我有一个随机的块(7个块中的一个),现在即使它确实在网格上移动,我使用PositionX = 15;这样它就可以移动块的距离(块为15乘15)。问题是我不知道如何使块在网格上平滑移动(例如向左和向右移动如此之快,我不知道如何在网格上使其变慢) Class Block看起来像这样:
public class Block
{
public Vector2 Position;
public bool[,] Vorm;
protected int Rotation; // how many times it rotates
protected int MaxRotation; // How many vorms of rotation
protected int PositionX;
protected int PositionY;
protected Color color;
protected Texture2D block;
int BlockYspeed;
int BlockXspeed;
float FallTimer; // public bool FallingBlock = false;
bool active;
Random Random = new Random();
int r;
public Block()
{
this.Vorm = new bool[4, 4];
r = Random.Next(0, 7);
}
public void LoadContent(ContentManager Content)
{
block = Content.Load<Texture2D>("Block");
}
public bool[,] blokvorm
{
get
{
int x = 4;
int y = 4;
bool[,] Vorm = new bool[x, y];
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
{
Vorm[i, j] = false;
}
int r = Random.Next(7);
switch (r)
{
case 0:
Vorm = StatusBlock.IBlock1;
break;
case 1:
Vorm = StatusBlock.TBlock1;
break;
case 2:
Vorm = StatusBlock.ZBlock1;
break;
case 3:
Vorm = StatusBlock.JBlock1;
break;
case 4:
Vorm = StatusBlock.LBlock1;
break;
case 5:
Vorm = StatusBlock.SBlock1;
break;
case 6:
Vorm = StatusBlock.OBlock1;
break;
default:
break;
}
return Vorm;
}
}
public virtual void rotation()
{ }
public void HandleInput()
{
if (Tetris.inputHelper.KeyPressed(Keys.Down))
{
PositionY += 15;
}
if (Tetris.InputHelper.KeyPressed(Keys.Up))
{
Rotation += 1;
if (Rotation == MaxRotation)
Rotation = 0;
rotation();
}
if (Tetris.InputHelper.IsKeyDown(Keys.Left))
{
PositionX -= 15;
}
if (Tetris.InputHelper.IsKeyDown(Keys.Right))
{
PositionX += 15;
}
}
public void Collision()
{
}
public void speed(GameTime gameTime)
{
double SpeedCounter = 3;
int LevelCounter = 1;
if (LevelCounter < Tetris.GameWorld.level)
{
SpeedCounter -= 0.5;
LevelCounter++;
}
FallTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (Math.Round(FallTimer, 1) == SpeedCounter)
{
FallTimer = 0;
BlockYspeed = 15;
BlockXspeed = 15;
PositionY = PositionY += BlockYspeed;
//BlockPosition.Y += 15;
}
}
public void Update(GameTime gametime)
{
BlockYspeed = 15;
HandleInput();
Collision();
PositionX += BlockXspeed;
PositionY += BlockYspeed;
}
public void Draw(SpriteBatch spriteBatch, ContentManager Content)
{
//block = Content.Load<Texture2D>("Block");
spriteBatch.Begin();
// if (!FallingBlock)
{
Vector2 BlockPosition;
BlockPosition = new Vector2(90 + PositionX, 0 + PositionY);
DrawBlock(spriteBatch, BlockPosition);
spriteBatch.End();
}
}
private void DrawBlock(SpriteBatch spriteBatch, Vector2 Position)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (this.Vorm[i, j])
spriteBatch.Draw(block, Position + new Vector2(i * block.Width, j * block.Height), this.color);
}
}
我在不同的类中有7个不同的块,并且我有一个状态块类,其中块的所有风格都在其中。
然后我想确保这些块在网格中我必须在GameWorld类中进行此操作:
public void AddToGrid(Block.Vorm b, int x, int y)
{
Vector2 gridPos = (Tetris.screen - new Vector2(grid.GetLength(0) * CellSize, grid.GetLength(1) * CellSize) / 2);
b.Position = gridPos + new Vector2(x * CellSize, y * CellSize);
grid[x, y] = b;
}
问题是:它给出了一个错误,即块中不存在vorm如何解决这个问题以使块在网格上?
编辑: 更新代码(另一个但功能相同):
public enum CanIPlaceBlock
{
Yes, No, OutOfScreen
}
public CanIPlaceBlock canIPlaceBlock(bool[,] grid, bool[,] Vorm, int x, int y)
{
for (int a = 0; a <Vorm.GetLength(0); a++)
for(int b=0; b<Vorm.GetLength(1);b++)
{
int PositionX = x + a;
int PositionY = y + b;
if (Vorm[a, b] == false)
{
if(PositionX<0 || PositionX>grid.GetLength(0))
{
return CanIPlaceBlock.OutOfScreen;
}
if(PositionY >grid.GetLength(1) || grid[a,b]==false)
{
return CanIPlaceBlock.No;
}
}
}
return CanIPlaceBlock.Yes;
}
答案 0 :(得分:1)
您获得的具体错误是因为您尝试将类成员用作类型。 Block.Vorm
引用Block
类的成员,在这种情况下无效。
尝试:
public void AddToGrid(Block b, int x, int y)
{
Vector2 gridPos = (Tetris.screen - new Vector2(grid.GetLength(0) * CellSize, grid.GetLength(1) * CellSize) / 2);
b.Position = gridPos + new Vector2(x * CellSize, y * CellSize);
grid[x, y] = b;
}
那就是说,我怀疑你需要稍微调整你的游戏逻辑。
当前下降的tetromino是(对于经典俄罗斯方块)4个单位区块的特定排列的集合。一旦将tetromino放置在其最终位置,这些块就成为布局的一部分。因此,请尝试将tetromino本身视为与布局交互的独立实体,而不是布局中存在的内容。
将tetromino视为一个独立的实体,它存在于布局“上方”的层中,但受其内容的约束。一旦tetromino再也无法进一步向下移动布局中的4个单元格,您将把块放入并销毁tetromino对象,或者使用下一个布置重置它。然后进行填充线检测等。