我刚刚开始制作一款有点像太空入侵者的游戏。
在编程方面,我是一个完全的新手,而且经验很少(我只是在尝试这个,因为我需要在下周末完成一个软件设计项目)。
无论如何,我一直在关注如何发射子弹的教程。它似乎没有工作。我已经复制了本教程的几乎所有方面,除了“子弹”类中的“速度”变量(我认为我不需要,因为我只使用左右移动而不是向前/向后)。
这是下面的代码。提前致谢。:)
主要的
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 Software_Design_Major_Project
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ship; // Declaring the sprite
Vector2 shipposition = Vector2.Zero;
List<bullets> bullets = new List<bullets>();
KeyboardState pastkey;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 600;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
ship = Content.Load<Texture2D>("ship"); // Loads the ship into the memory.
shipposition = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) -
(ship.Width / 2), 420);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && shipposition.X
>= 0)
{
shipposition.X -= 6;
}
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && shipposition.X <
((graphics.GraphicsDevice.Viewport.Width) - (ship.Width)))
{
shipposition.X += 6;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastkey.IsKeyUp(Keys.Space))
{
shoot();
}
pastkey = Keyboard.GetState();
updatebullets();
base.Update(gameTime);
}
public void updatebullets()
{
foreach(bullets bullet in bullets)
{
if (Vector2.Distance(bullet.position, shipposition) < 0)
bullet.isvisible = false;
}
for (int i = 0; i < bullets.Count; i++)
{
if (!bullets[i].isvisible)
bullets.RemoveAt(i);
i--;
}
}
public void shoot()
{
bullets newbullet = new bullets(Content.Load<Texture2D>("bullet"));
newbullet.position = shipposition;
newbullet.isvisible = true;
if (bullets.Count < 20)
bullets.Add(newbullet);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ship, shipposition, Color.White);
spriteBatch.End();
foreach (bullets bullet in bullets)
bullet.Draw(spriteBatch);
base.Draw(gameTime);
}
}
}
子弹
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Software_Design_Major_Project
{
class bullets // A new class needs to be created to allow for bullets.
{
public Texture2D texture;
public Vector2 position;
public Vector2 origin;
public bool isvisible;
public bullets(Texture2D newtexture)
{
texture = newtexture;
isvisible = false;
}
public void Draw(SpriteBatch spritebatch)
{
spritebatch.Draw(texture, position, null, Color.White, 0f, origin, 1f,
SpriteEffects.None, 0);
}
}
}
PS对不起,很长的帖子。