我要在随机块中添加一些能力,您知道当您击中随机块时会下降。就像您获得2个球或更大的桨或较慢的球速度一样。
我需要在编写for循环和statement上的帮助,这可以在我的块级别上增加一些能力。就像1级的1个随机能力一样。因此,当您击中1个随机块的20个块时,这具有我的能力。它会像原始的突破游戏一样落到桨上。
我在想一个开关,如果一个随机块被击中并使用该开关并随机对其进行随机化。
void PowerUp()
{
powerups.Add(abilityballs_rect);
powerups.Add(abilitylong_rect);
powerups.Add(abilityslow_rect);
}
-
List<Rectangle> block = new List<Rectangle>();
List<Rectangle> block2 = new List<Rectangle>();
List<Rectangle> block3 = new List<Rectangle>();
List<Rectangle> block4 = new List<Rectangle>();
List<Rectangle> block5 = new List<Rectangle>();
List<Rectangle> block6 = new List<Rectangle>();
List<Rectangle> powerups = new List<Rectangle>();
-
if (level == 1)
{
if (block.Count == 14 && block2.Count == 14)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(252, 400), Color.White);
}
foreach (Rectangle g in block)
{
spriteBatch.Draw(block_texture, g, Color.LimeGreen);
}
foreach (Rectangle r in block2)
{
spriteBatch.Draw(block_texture, r, Color.IndianRed);
}
}
else if (level == 2)
{
if (block3.Count == 18 && block4.Count == 27)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
}
foreach (Rectangle b in block3)
{
spriteBatch.Draw(block_texture, b, Color.CornflowerBlue);
}
foreach (Rectangle y in block4)
{
spriteBatch.Draw(block_texture, y, Color.Yellow);
}
}
else if (level == 3)
{
if (block5.Count == 36 && block6.Count == 18)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
}
foreach (Rectangle o in block5)
{
spriteBatch.Draw(block_texture, o, Color.Orange);
}
foreach (Rectangle p in block6)
{
spriteBatch.Draw(block_texture, p, Color.HotPink);
}
}
-
void AddBlocks()
{
//LEVEL 1
for (int i = 1; i < 3; i++)
{
for (int f = 1; f < 8; f++)
{
block.Add(new Rectangle((f * 63) + 94, (i * 40) + 60, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 3; i++)
{
for (int g = 1; g < 8; g++)
{
block2.Add(new Rectangle((g * 63) + 94, (i * 40) + 40, block_texture.Width, block_texture.Height));
}
}
//LEVEL 2
for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 10; j++)
{
block3.Add(new Rectangle((j * 63) + 34, (i * 200) - 60, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 10; i++)
{
for (int k = 1; k < 4; k++)
{
block4.Add(new Rectangle((k * 103) + 143, (i * 20) + 140, block_texture.Width, block_texture.Height));
}
}
//LEVEL 3
for (int i = 1; i < 7; i++)
{
for (int j = 1; j < 7; j++)
{
block5.Add(new Rectangle((j * 63) + 127, (i * 20) + 190, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 10; i++)
{
for (int k = 1; k < 3; k++)
{
block6.Add(new Rectangle((k * 443) - 317, (i * 20) + 160, block_texture.Width, block_texture.Height));
}
}
}
-
void DeleteBlocks()
{
if (level == 1)
{
for (int j = 0; j < block.Count; j++)
{
if (ball_rect.Intersects(block[j]))
{
ball_speed.Y *= -1;
points += 1;
block.RemoveAt(j);
if (points > highscore)
{
highscore = points;
}
}
}
for (int k = 0; k < block2.Count; k++)
{
if (ball_rect.Intersects(block2[k]))
{
ball_speed.Y *= -1;
points += 1;
block2.RemoveAt(k);
if (points > highscore)
{
highscore = points;
}
}
}
if (block.Count == 0 && block2.Count == 0)
{
level++;
StartValueBallPaddle();
Start = false;
}
}
else if (level == 2)
{
for (int l = 0; l < block3.Count; l++)
{
if (ball_rect.Intersects(block3[l]))
{
ball_speed.Y *= -1;
points += 1;
block3.RemoveAt(l);
if (points > highscore)
{
highscore = points;
}
}
}
for (int m = 0; m < block4.Count; m++)
{
if (ball_rect.Intersects(block4[m]))
{
ball_speed.Y *= -1;
points += 1;
block4.RemoveAt(m);
if (points > highscore)
{
highscore = points;
}
}
}
if (block3.Count == 0 && block4.Count == 0)
{
level++;
StartValueBallPaddle();
Start = false;
}
}
else if (level == 3)
{
for (int n = 0; n < block5.Count; n++)
{
if (ball_rect.Intersects(block5[n]))
{
ball_speed.Y *= -1;
points += 1;
block5.RemoveAt(n);
if (points > highscore)
{
highscore = points;
}
}
}
for (int o = 0; o < block6.Count; o++)
{
if (ball_rect.Intersects(block6[o]))
{
ball_speed.Y *= -1;
points += 1;
block6.RemoveAt(o);
if (points > highscore)
{
highscore = points;
}
}
}
}
}
您可能需要将块数据封装到一个类,其中包含颜色,位置和动力。
您可以使用一种被称为" AddPowerUp(("之类的方法,或者将使用此方法将随机(或特定(的功率添加到块中。您可能需要一个用于供电的Getter。
现在,在游戏中,您将在列表中或2D数组或任何适合您的集合中保存所有级别的块然后,要将powerup应用于级别的随机块,您可以执行类似的操作(主要是伪代码,因此未测试(:
List<Block> blockList = CreateBlockLevel(1);
int randomIndex = new Random().Next(blockList.Length-1);
blockList[randomIndex].AddPowerup();
如果要向多个块上施加电动,则可以将其放在循环中,但是您可能需要检查该块是否已经有了电源。