C# 即使图像是透明的,某些图像仅在其后面显示白色背景
由于某种原因,我的敌舰和敌弹出现在白色背景下,尽管它们是透明的 电源csC# 即使图像是透明的,某些图像仅在其后面显示白色背景,c#,xna,xna-4.0,C#,Xna,Xna 4.0,由于某种原因,我的敌舰和敌弹出现在白色背景下,尽管它们是透明的 电源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.GamerServ
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
{
//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 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;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
命名空间示例\u空间\u游戏
{
公共级Starfield
{
公共纹理2D纹理;
公共向量2 bgPos1,bgPos2;
公共交通速度;
//建造师
公共星场()
{
纹理=空;
bgPos1=新向量2(0,0);
bgPos2=新向量2(0,-950);
速度=5;
}
//装载量
公共void加载内容(ContentManager内容)
{
纹理=内容。加载(“空间”);
}
//画
公共作废抽签(SpriteBatch SpriteBatch)
{
绘制(纹理,bgPos1,颜色,白色);
绘制(纹理,bgPos2,颜色,白色);
}
//更新
公开作废更新(游戏时间游戏时间)
{
//设置背景滚动的速度
bgPos1.Y=bgPos1.Y+速度;
bgPos2.Y=bgPos2.Y+速度;
//滚动背景(重复)
如果(bgPos1.Y>=950)
{
bgPos1.Y=0;
bgPos2.Y=-950;
}
}
}
}
以上都是我的代码,不太清楚如何截屏我的游戏,这样你就可以看到发生了什么。但出于某种原因,它只是我的敌人子弹和飞船,显示了白色背景。我已经在photoshop中检查过了,它们和其他所有的图像一样是透明的
如果有人能帮上忙,我会非常感激的,谢谢你而不是使用
spriteBatch.Begin();
试用
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
AlphaBlend是“内置状态对象,具有alpha blend的设置,使用alpha混合源数据和目标数据。”所有图像都是.png,我想您需要为图形设备/精灵批处理设置BlendMode,才能使用AlphaBlend
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;
}
}
}
}
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);