2015-12-04 22 views
0

我目前正在研究一個遊戲項目,其中物體從屏幕的頂部落下,而底部的人需要抓住正確的物體。如何在XNA中從屏幕頂部跌落超過1個Texture2d?

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); 

我明白,如果有人說,他們這裏不給我們答案,但我只是想一些幫助,以及如何做到這一點..

我怎樣才能讓一個數組,其中的對象被採摘隨機地,他們從屏幕上掉下來,而不是逐個添加Texture2Ds。

我確實試圖找到這個教程,但我不能.. 我會很感激任何幫助。

+0

你的問題還不太清楚。你能改進它嗎?正如我可以看到你做對了heare:使用一個紋理和位置列表。 – Silveor

+0

謝謝您的回覆,我很抱歉,如果我不清楚。 目前在遊戲中,我有一個塊從屏幕的頂部下降......我想讓3-4個不同的物體落下。如蘋果和香蕉以及這些塊.. 但我只是不知道如何添加其他對象.... 我認爲這將是更容易,如果我做一個對象的數組,他們得到隨機挑選並落下,但我需要幫助。 – hTeeML

回答

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(); 
    } 
} 

另外谷歌一些關於瓷磚地圖,以提高您的遊戲性能。