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 WindowsGame3
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ship, bullet, alien;
Texture2D[] aliens = new Texture2D[20];
Texture2D[] bullets = new Texture2D[10];
Vector2 shipPos = new Vector2(200, 540);
Vector2 alienPos = new Vector2(200, 400);
Vector2 bulletPos = new Vector2(200, 450);
Vector2[] aliensPos = new Vector2[20];
Vector2[] bulletsPos = new Vector2[10];
int alienCount = 0;
int bulletCount = 0;
KeyboardState state = Keyboard.GetState();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 400;
graphics.PreferredBackBufferHeight = 600;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
alien = Content.Load<Texture2D>("alien");
ship = Content.Load<Texture2D>("ship");
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 4; y++)
{
aliens[alienCount] = Content.Load<Texture2D>("alien");
aliensPos[alienCount].X = 100 + (x * 40);
aliensPos[alienCount].Y = 20 + (y * 40);
alienCount++;
}
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (state.IsKeyDown(Keys.Right) && shipPos.X < 360)
{
shipPos.X += 5;
}
if (state.IsKeyDown(Keys.Left) && shipPos.X > 0)
{
shipPos.X -= 5;
}
if (state.IsKeyDown(Keys.Space))
{
bullets[bulletCount] = Content.Load<Texture2D>("bullet");
bulletsPos[bulletCount].X = shipPos.X;
bulletsPos[bulletCount].Y = shipPos.Y;
bulletCount++;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ship, shipPos, Color.White);
spriteBatch.Draw(alien, alienPos, Color.White);
for (int alienCount = 0; alienCount < 20; alienCount++)
{
spriteBatch.Draw(aliens[alienCount], aliensPos[alienCount], Color.White);
}
for (int bulletCount = 0; bulletCount < 10; bulletCount++)
{
spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
我想在船舶位置创建一个子弹并将该子弹存储在一个数组中,但是在我的 draw 方法中,当我从数组中绘制子弹时,我收到错误消息 This method does not accept null for this parameter。参数名称:纹理