2015-09-15 26 views
1

由於某些原因,我的敵方艦船和敵方子彈即使是透明的,也會出現白色背景。即使圖像透明,某些圖像也只出現在背後有一個白色背景

電源CS

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace Example_Space_Game 
{ 
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     Random random = new Random(); 
     public int enemyBulletDamage; 

     //List 
     List<Asteroid> asteroidList = new List<Asteroid>(); 
     List<Enemy> enemyList = new List<Enemy>(); 

     //Instantiating new player and objects 
     Player p = new Player(); 
     Starfield sf = new Starfield();  

     //constructor 
     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      graphics.IsFullScreen = false; 
      graphics.PreferredBackBufferWidth = 800; 
      graphics.PreferredBackBufferHeight = 950; 
      this.Window.Title = " XNA - 2D Space Shooter "; 
      Content.RootDirectory = "Content"; 
      enemyBulletDamage = 10; 
     } 

     //init 
     protected override void Initialize() 
     { 
      base.Initialize(); 
     } 

     //Load Content 
     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      p.LoadContent(Content); 
      sf.LoadContent(Content); 
     } 

     //Unload Content 
     protected override void UnloadContent() 
     { 
     } 

     //Update 
     protected override void Update(GameTime gameTime) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       this.Exit(); 

      //Updating enemy's and checking collision of enemyship to playership 
      foreach (Enemy e in enemyList) 
      { 
       //check if enemyship is colliding with player 
       if(e.boundingBox.Intersects(p.boundingBox)) 
       { 
        p.health -= 40; 
        e.isVisible = false; 
       } 

       //check enemy bullet collision with player ship 
       for(int i = 0; i < e.bulletList.Count; i++) 
       { 
        if(p.boundingBox.Intersects(e.bulletList[i].boundingBox)) 
        { 
         p.health -= enemyBulletDamage; 
         e.bulletList[i].isVisible = false; 
        } 
       } 

       //check player bullet collision to enemy ship 
       for (int i = 0; i < p.bulletlist.Count; i++) 
       { 
        if(p.bulletlist[i].boundingBox.Intersects(e.boundingBox)) 
        { 
         p.bulletlist[i].isVisible = false; 
         e.isVisible = false; 
        } 
       } 

       e.Update(gameTime); 
      } 


      //for each asteroid in our asteroidList, update it and check for collisions 
      foreach(Asteroid a in asteroidList) 
      { 
       //check to see if any of the asteroids are colliding with our playership, if they are... set invisible to false (remove then from our list) 
       if(a.boundingBox.Intersects(p.boundingBox)) 
       { 
        p.health -= 20; 
        a.isVisible = false; 
       } 

       //iterate through our bulletList if any asteroid come in contacts with these bullets, destroyed bullet and asteroid 
       for (int i = 0; i < p.bulletlist.Count; i++) 
       { 
        if(a.boundingBox.Intersects(p.bulletlist[i].boundingBox)) 
        { 
         a.isVisible = false; 
         p.bulletlist.ElementAt(i).isVisible = false; 
        } 
       } 

       a.Update(gameTime); 
      } 

      p.Update(gameTime); 
      sf.Update(gameTime); 
      LoadAsteroids(); 
      LoadEnemies(); 

      base.Update(gameTime); 
     } 

     //Draw Content 
     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 
      spriteBatch.Begin(); 

      sf.Draw(spriteBatch); 
      p.Draw(spriteBatch); 
      foreach(Asteroid a in asteroidList) 
      { 
       a.Draw(spriteBatch); 
      } 

      foreach(Enemy e in enemyList) 
      { 
       e.Draw(spriteBatch); 
      } 

      spriteBatch.End(); 
      base.Draw(gameTime); 
     } 

     //Load Asteroids 
     public void LoadAsteroids() 
     { 
      // Creating random variables for our x and y axis of our controls 
      int randY = random.Next(-600, -50); 
      int randX = random.Next(0, 750); 

      //if there are less than 5 asteroids on the screen, than create more until there is 5 again 
      if(asteroidList.Count() < 5) 
      { 
       asteroidList.Add(new Asteroid(Content.Load<Texture2D>("asteroid"), new Vector2(randX, randY))); 
      } 

      //if any of the enemies in the list were destroyed (or invisible), then remove from the list 
      for (int i = 0; i < asteroidList.Count; i++) 
      { 
       if(!asteroidList[i].isVisible) 
       { 
        asteroidList.RemoveAt(i); 
        i--; 
       } 
      } 
     } 

     //Load Enemies 
     public void LoadEnemies() 
     { 
      // Creating random variables for our x and y axis of our controls 
      int randY = random.Next(-600, -50); 
      int randX = random.Next(0, 750); 

      //if there are less than 3 enemies on the screen, than create more until there is 3 again 
      if(enemyList.Count() < 3) 
      { 
       enemyList.Add(new Enemy(Content.Load<Texture2D>("enemyship"), new Vector2(randX, randY), Content.Load<Texture2D>("EnemyBullet"))); 
      } 

      //if any of the enemies in the list were destroyed (or invisible), then remove from the list 
      for (int i = 0; i < enemyList.Count; i++) 
      { 
       if(!enemyList[i].isVisible) 
       { 
        enemyList.RemoveAt(i); 
        i--; 
       } 
      } 
     } 
    } 
} 

敵人CS

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using Microsoft.Xna.Framework; 
    using Microsoft.Xna.Framework.Content; 
    using Microsoft.Xna.Framework.Graphics; 
    using Microsoft.Xna.Framework.Input; 

    namespace Example_Space_Game 
    { 
     public class Enemy 
     { 
      public Rectangle boundingBox; 
      public Texture2D texture, bulletTexture; 
      public Vector2 position; 
      public int health, speed, bulletDelay, currentDifficultyLevel; 
      public bool isVisible; 
      public List<Bullet> bulletList; 

      //Constructor 
      public Enemy(Texture2D newTexture, Vector2 newPosition, Texture2D newBulletTexture) 
      { 
       bulletList = new List<Bullet>(); 
       texture = newTexture; 
       bulletTexture = newBulletTexture; 
       health = 5; 
       position = newPosition; 
       currentDifficultyLevel = 1; 
       bulletDelay = 40; 
       speed = 5; 
       isVisible = true; 
      } 

      //Update 
      public void Update(GameTime gameTime) 
      { 
       //Update Collision rectangle 
       boundingBox = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); 

       //Update enemy movement 
       position.Y += speed; 

       //Move enemy to top of the screen if he fly's off the bottom 
       if (position.Y >= 950) 
        position.Y = -75; 

       EnemyShoot(); 
       UpdateBullets(); 
      } 

      //Draw 
      public void Draw(SpriteBatch spriteBatch) 
      { 
       //Draw enemy ship 
       spriteBatch.Draw(texture, position, Color.White); 

       //Drwa enemy bullets 
       foreach (Bullet b in bulletList) 
       { 
        b.Draw(spriteBatch); 
       } 
      } 

      //Update bullet function 
      public void UpdateBullets() 
      { 
       //for each bullet in out bulletList: update the movement and if the bullet hits the top of the screen remove it from the list 
       foreach (Bullet b in bulletList) 
       { 
        //BoundingBox for every bullet in our bulletList 
        b.boundingBox = new Rectangle((int)b.position.X, (int)b.position.Y, b.texture.Width, b.texture.Height); 

        //set movement for bullet 
        b.position.Y = b.position.Y + b.speed; 

        //if bullet hits the top of the screen, then make visible false 
        if (b.position.Y >= 950) 
         b.isVisible = false; 
       } 

       //iterate through bulletList and see if any of the bullets are not visible, if they aren't then remove that bullet from out bullet list 
       for (int i = 0; i < bulletList.Count; i++) 
       { 
        if(!bulletList[i].isVisible) 
        { 
         bulletList.RemoveAt(i); 
         i--; 
        } 
       } 
      } 

      //enemy shoot function 
      public void EnemyShoot() 
      { 
       //Shoot only if the bulletdelay resets 
       if (bulletDelay >= 0) 
        bulletDelay--; 

       if(bulletDelay <= 0) 
       { 
        //create new bullet and position it front and centre of enemy ship 
        Bullet newBullet = new Bullet(bulletTexture); 
        newBullet.position = new Vector2(position.X + texture.Width/2 - newBullet.texture.Width/2, position.Y + 30); 

        newBullet.isVisible = true; 

        if (bulletList.Count() < 20) 
         bulletList.Add(newBullet); 
       } 

       //reset bullet delay 
       if (bulletDelay == 0) 
        bulletDelay = 40; 
      } 
     } 
    } 

子彈CS

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 

namespace Example_Space_Game 
{ 
    //Main 
    public class Bullet 
    { 
     public Rectangle boundingBox; 
     public Texture2D texture; 
     public Vector2 origin; 
     public Vector2 position; 
     public bool isVisible; 
     public float speed; 

     //Constructor 
     public Bullet(Texture2D newTexture) 
     { 
      speed = 10; 
      texture = newTexture; 
      isVisible = false; 

     } 

     //Draw 
     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(texture, position, Color.White); 
     } 
    } 
} 

玩家CS

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 


namespace Example_Space_Game 
{ 
    //main 
    public class Player 
    { 
     public Texture2D texture, bulletTexture, healthTexture; 
     public Vector2 position, healthbarPosition; 
     public int speed, health; 
     public float bulletDelay; 
     public Rectangle boundingBox, healthRectangle; 
     public bool IsColliding; 
     public List<Bullet> bulletlist; 

     //---Contructor--- 
     public Player() 
     { 
      bulletlist = new List<Bullet>(); 
      texture = null; 
      position = new Vector2(300, 300); 
      bulletDelay = 1; 
      speed = 10; 
      IsColliding = false; 
      health = 200; 
      healthbarPosition = new Vector2(50, 50); 
     } 

     //---Load Content--- 
     public void LoadContent(ContentManager Content) 
     { 
      texture = Content.Load<Texture2D>("ship"); 
      bulletTexture = Content.Load<Texture2D>("playerbullet"); 
      healthTexture = Content.Load<Texture2D>("healthbar"); 
     } 

     //---Draw--- 
     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(texture, position, Color.White); 
      spriteBatch.Draw(healthTexture, healthRectangle, Color.White); 

      foreach (Bullet b in bulletlist) 
       b.Draw(spriteBatch); 



     } 

     //---Update--- 
     public void Update(GameTime gameTime) 
     { 
      //Getting Keyboard State 
      KeyboardState keyState = Keyboard.GetState(); 

      //BoundingBox for player ship 
      boundingBox = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); 

      //Set rectangle for health bar 
      healthRectangle = new Rectangle((int)healthbarPosition.X,(int)healthbarPosition.Y, health, 25); 

      //Fire bullets 
      if(keyState.IsKeyDown(Keys.Space)) 
      { 
       Shoot(); 
      } 

      UpdateBullets(); 
      //Ship Controls 
      if (keyState.IsKeyDown(Keys.W)) 
       position.Y = position.Y - speed; 

      if (keyState.IsKeyDown(Keys.A)) 
       position.X = position.X - speed; 

      if (keyState.IsKeyDown(Keys.S)) 
       position.Y = position.Y + speed; 

      if (keyState.IsKeyDown(Keys.D)) 
       position.X = position.X + speed; 

      //keep ship in scren bounds 
      if (position.X <= 0) 
       position.X = 0; 

      if (position.X >= 800 - texture.Width) 
       position.X = 800 - texture.Width; 

      if (position.Y <= 0) 
       position.Y = 0; 

      if (position.Y >= 950 - texture.Height) 
       position.Y = 950 - texture.Height; 
     } 

     //---Shoot Method: used to set starting position of our bullets--- 
     public void Shoot() 
     { 
      //shoot only if bullet delay resets 
      if (bulletDelay >= 0) 
       bulletDelay--; 

      //if bullet delay is at 0: create a new bullet at player position, make it visible, then add the bulel to the list 
      if (bulletDelay <= 0) 
      { 
       Bullet newBullet = new Bullet(bulletTexture); 
       newBullet.position = new Vector2(position.X + 32 - newBullet.texture.Width/2, position.Y + 30); 

       //Making bullet visible 
       newBullet.isVisible = true; 

       if (bulletlist.Count() < 20) 
        bulletlist.Add(newBullet); 
      } 

      //reset bullet delay 
      if (bulletDelay == 0) 
       bulletDelay = 20; 
     } 

     //---Update bullet function--- 
     public void UpdateBullets() 
     { 
      //for each bullet in out bulletlist: update the movement and if the bullet hits the top of the screen remove it from the list 
      foreach (Bullet b in bulletlist) 
      { 
       //boundbox for our every bullet in our bulletlist 
       b.boundingBox = new Rectangle((int)b.position.X, (int)b.position.Y, b.texture.Width, b.texture.Height); 

       //set movement for bullet 
       b.position.Y = b.position.Y - b.speed; 

       //if bullet hits the top of the screen, then make visible false 
       if(b.position.Y<=0) 
        b.isVisible=false; 
      } 

      //Iterate through the bulletlist and see if any of the bullets are not visible, if they aren't remove that bullet from our bulletlist 
      for (int i = 0; i < bulletlist.Count; i++) 
      { 
       if (!bulletlist[i].isVisible) 
       { 
        bulletlist.RemoveAt(i); 
        i--; 
       } 
      }   
     } 
    } 
} 

小行星CS

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 


namespace Example_Space_Game 
{ 
    public class Asteroid 
    { 
     public Rectangle boundingBox; 
     public Texture2D texture; 
     public Vector2 position; 
     public Vector2 origin; 
     public float rotationAngle; 
     public int speed; 

     public bool isVisible; 
     Random random = new Random(); 
     public float randX, randY; 

     //Constructor 
     public Asteroid(Texture2D newTexture, Vector2 newPosition) 
     { 
      position = newPosition; 
      texture = newTexture; 
      speed = 4; 
      isVisible = true; 
      randX = random.Next(0, 750); 
      randY = random.Next(-600, -50); 

     } 

     //Load Content 
     public void LoadContent(ContentManager Content) 
     { 

     } 

     //Update 
     public void Update(GameTime gameTime) 
     { 
      //Set bounding box for collision 
      boundingBox=new Rectangle((int)position.X,(int)position.Y, 45, 45); 

      //updating origin for rotation 
      //origin.X = texture.Width/2; 
      //origin.Y = texture.Height/2; 

      //Udate Movement 
      position.Y = position.Y + speed; 
      if (position.Y>=950) 
       position.Y=-50; 

      //Rotate Asteroid 
      //float elapsed =(float)gameTime.ElapsedGameTime.TotalSeconds; 
      //rotationAngle += elapsed; 
      //float circle=MathHelper.Pi * 2; 
      //rotationAngle=rotationAngle % circle; 
     } 

     //Draw 
     public void Draw(SpriteBatch spriteBatch) 
     { 
      if(isVisible) 
       spriteBatch.Draw(texture, position, Color.White); 
     } 
    } 
} 

星空CS

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
namespace Example_Space_Game 
{ 
    public class Starfield 
    { 
     public Texture2D texture; 
     public Vector2 bgPos1, bgPos2; 
     public int speed; 

     //Constructor 
     public Starfield() 
     { 
      texture=null; 
      bgPos1=new Vector2(0,0); 
      bgPos2 = new Vector2(0, -950); 
      speed=5; 

     } 

     //Load Content 
     public void LoadContent(ContentManager Content) 
     { 
      texture = Content.Load<Texture2D>("space"); 
     } 

     //Draw 
     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(texture, bgPos1, Color.White); 
      spriteBatch.Draw(texture, bgPos2, Color.White); 
     } 

     //Update 
     public void Update(GameTime gametime) 
     { 
      //Setting speed for background scrolling 
      bgPos1.Y = bgPos1.Y + speed; 
      bgPos2.Y = bgPos2.Y + speed; 

      //Scrolling background (repeating) 
      if (bgPos1.Y >=950) 
      { 
       bgPos1.Y = 0; 
       bgPos2.Y = -950; 
      } 
     } 

    } 

} 

以上是我所有的代碼,搞不清怎麼截圖我的遊戲玩了,所以你可以看到什麼事情爲好。但由於某種原因,它只是我的敵人子彈和船,這是顯示白色背景。我已經在Photoshop中檢查過它們,它們都是透明的,就像所有其他圖像一樣。

如果任何人都可以是任何幫助將是非常巨大的FUL謝謝

+0

使用.png圖像 –

+0

所有的圖像都是.png, – shaneo

+1

我想你需要爲圖形設備/精靈批處理設置BlendMode,以使用AlphaBlend – LInsoDeTeh

回答

1

而不是使用

spriteBatch.Begin(); 

的請嘗試使用

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); 

的AlphaBlend是「一個內置的狀態對象並設置了alpha混合,即使用alpha混合源數據和目標數據。「