我是一個試圖製作俄羅斯方塊的新手。現在我有一個塊是隨機的(7個塊之一),現在即使它在網格上移動,我使用PositionX = 15;所以它移動塊的距離(塊15到15)。問題是我不知道如何使塊在網格上平穩移動(例如,左右移動速度如此之快,而我不知道如何在網格上使其變慢) 類塊看起來像這樣:如何確保塊的位置在網格上
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);
}
}
我有不同的類並且Ive一個statusblock類其中塊的所有vorms是在它的7個不同的塊。
後來我想,以確保塊網格上的我不得不作出這樣的遊戲世界類:
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;
}
問題是:它給出了一個錯誤沃爾姆不阻止存在如何解決這使塊是在網格上?
編輯: 更新代碼(另一個但功能相同):
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;
}
所以你說的是一旦它不能再向下移動塊層變得與網格層相同?順便說一句,代碼和我的一樣嗎?它仍然給它一個錯誤。編輯:nvm我看到它,但它仍然給出錯誤 –
不,該代碼不同於第一個參數傳遞的類型。我沒有改變任何東西。是的,當tetromino被阻止進一步向下移動時,組件塊被放置在網格層中。 – Corey
它仍然給出錯誤。但因爲我不用層工作它會自動走? –