我目前正在开展一个游戏项目,其中对象从屏幕顶部掉落,而底部的人需要抓住正确的对象。
Texture2D blockTexture;
List<Vector2> blockPositions = new List<Vector2>();
float BlockSpawnProbability = 0.01f;
const int BlockFallSpeed = 2;
以下是产生新的下降块
if (random.NextDouble() < BlockSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - blockTexture.Width);
blockPositions.Add(new Vector2(x, -blockTexture.Height));
}
personHit = false;
for (int i = 0; i < blockPositions.Count; i++)
{
// Animate this block falling
blockPositions[i] =
new Vector2(blockPositions[i].X,
blockPositions[i].Y + BlockFallSpeed);
// Get the bounding rectangle of this block
Rectangle sprite =
new Rectangle((int)blockPositions[i].X, (int)blockPositions[i].Y,
blockTexture.Width, blockTexture.Height);
// check collision with person
if (personRectangle.Intersects(sprite))
personHit = true;
if (blockPositions[i].Y > Window.ClientBounds.Height)
{
blockPositions.RemoveAt(i);
i--;
}
}
base.Update(gameTime);
}
绘制块
foreach (Vector2 blockPosition in blockPositions)
spriteBatch.Draw(blockTexture, blockPosition, Color.White);
我理解,如果有人说他们没有在这里给出我们的答案,但我只是想要一些帮助,以及如何做到这一点......
如何制作一个数组,其中随机拾取对象并从屏幕上掉落而不是逐个添加Texture2D。
我确实试图找到关于此的教程,但我不能.. 我会感激任何帮助。
答案 0 :(得分:0)
在我的游戏中,我总是有相同的模式(更少或更相似)。
我有一个用于项目类的类来保留常规属性:
public class ItemClass
{
public int ID;
public string Name;
public int Points;
public Texture2D Texture;
public ItemClass(int pID, int pName, int pPoints, int pTetxure2D)
{
this.ID = pID;
this.Name = pName;
this.Points = pPoints;
this.Texture = pTexture;
}
public Item New()
{
return new Item(this.ID);
}
}
用于保存单个属性的项目实例的一个类:
public class Item
{
public int ClassID;
public Vector2D Position;
public Item(int pClassID)
{
this.ClassID = pClassID;
}
}
游戏类的主要部分:
public CoolGame : Game
{
private Dictionary<int, ItemClass> itemClasses; // each item class should nave unique ID
private List<Item> items; // real items; better also make as dictionary with unique ID for each
private SpriteBatch spriteBatch;
// call in LoadContent to register all item classes you want use in game
private LoadItemClasses()
{
this.itemClasses = new Dictionary<int, ItemClass>();
this.itemClasses.Add(0, new ItemClass(0, "Silver coin", 1, silver_coin_texture));
this.itemClasses.Add(1, new ItemClass(1, "Gold Coin", 3, gold_coin_texture));
this.itemClasses.Add(2, new ItemClass(2, "Diamond", 10, diamond_texture));
this.itemClasses.Add(3, new ItemClass(3, "Anchor", -5, anchor_texture));
/// and so on...
this.items = new List<Item>();
}
// call to create real item that you can draw and catch
private void CreateRandomItem()
{
Vector2D randomPosition = ...;
int randomItemClassID = ...;
ItemClass itemClass = this.itemClasses[randomItemClassID];
Item item = itemClass.New();
item.Position = randomPosition;
this.items.Add(item);
}
private void UpdateItems()
{
foreach (Item item in this.items)
{
// update item position, check collision and so on
}
}
// you use position from item, but texture from itemClass, so you can use only one Texture2D object for whole your items of this class
private void DrawItems()
{
ItemClass itemClass = null;
this.spriteBatch.Begin();
foreach (Item item in this.items)
{
itemClass = this.itemClasses[item.ClassID];
spriteBatch.Draw(itemClass.Texture, item.Position, color.White);
}
this.spriteBatch.End();
}
}
还谷歌一些关于瓷砖地图,以提高你的游戏性能。