对所有代码感到抱歉,但我对Visual C#和XNA完全不熟悉。
该程序不属于Aaron Reed的学习XNA 4.0。我的教授修改了它应该做的一些东西(这本书涵盖了一些但不是全部)。大多数代码都是从书中复制的。
这些是成品的最低要求:
我试图让敌人(骷髅球)躲避玩家(骚扰),但敌人要么四面都没有反应(玩家在右边或左边,敌人不会逃避直到玩家当敌人躲避时,它们会在屏幕外抖动消失(它们突然直接向上或向下移动,并且它们会迅速摇晃。
当新敌人从左上角开始10秒后产生时,它从底部墙壁反弹,然后在它重新上升时从屏幕上消失。
10秒钟后,颅骨也不会在屏幕周围随机产生。
此外,我不知道如何在没有碰撞的情况下按下A按钮时再产生2个敌人。
SpriteManager.cs
namespace Assignment_2
{
public class SpriteManager : Microsoft.Xna.Framework.DrawableGameComponent
{
//SpriteBatch for drawing
SpriteBatch spriteBatch;
//A sprite for the player and a list of automated sprites
UserControlledSprite player;
List<Sprite> spriteList = new List<Sprite>();
int enemySpawnMinMilliseconds = 10000;
int enemySpawnMaxMilliseconds = 10000;
int enemyMinSpeed = 10;
int enemyMaxSpeed = 10;
int nextSpawnTime = 0;
public SpriteManager(Game game)
: base(game)
{
// TODO: Construct any child components here
}
public override void Initialize()
{
// TODO: Add your initialization code here
ResetSpawnTime();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(Game.GraphicsDevice);
//Load the player sprite
player = new UserControlledSprite(
Game.Content.Load<Texture2D>(@"Images/threerings"),
Vector2.Zero, new Point(75, 75), 10, new Point(0, 0),
new Point(6, 8), new Vector2(6, 6));
//Load several different automated sprites into the list to test
//spriteList.Add(new AutomatedSprite(
// Game.Content.Load<Texture2D>(@"Images/skullball"),
// new Vector2(150, 150), new Point(75, 75), 10, new Point(0, 0),
// new Point(6, 8), new Vector2(3, 0), "skullcollision"));
//spriteList.Add(new AutomatedSprite(
// Game.Content.Load<Texture2D>(@"Images/skullball"),
// new Vector2(300, 150), new Point(75, 75), 10, new Point(0, 0),
// new Point(6, 8), new Vector2(-2, 0), "skullcollision"));
////spriteList.Add(new AutomatedSprite(
//// Game.Content.Load<Texture2D>(@"Images/skullball"),
//// new Vector2(150, 300), new Point(75, 75), 10, new Point(0, 0),
//// new Point(6, 8), new Vector2(3, 4), "skullcollision"));
////spriteList.Add(new AutomatedSprite(
//// Game.Content.Load<Texture2D>(@"Images/skullball"),
//// new Vector2(300, 400), new Point(75, 75), 10, new Point(0, 0),
//// new Point(6, 8), new Vector2(0, -3), "skullcollision"));
////spriteList.Add(new AutomatedSprite(
//// Game.Content.Load<Texture2D>(@"Images/skullball"),
//// new Vector2(200, 300), new Point(75, 75), 10, new Point(0, 0),
//// new Point(6, 8), new Vector2(-3, 7), "skullcollision"));
spriteList.Add(new EvadingSprite(
Game.Content.Load<Texture2D>(@"Images/skullball"),
new Vector2(150, 150), new Point(75, 75), 10, new Point(0, 0),
new Point(6, 8), new Vector2(3, 0), "skullcollision", this, .75f, 150));
spriteList.Add(new EvadingSprite(
Game.Content.Load<Texture2D>(@"Images/skullball"),
new Vector2(300, 150), new Point(75, 75), 10, new Point(0, 0),
new Point(6, 8), new Vector2(-2, 0), "skullcollision", this, .75f, 150));
spriteList.Add(new EvadingSprite(
Game.Content.Load<Texture2D>(@"Images/skullball"),
new Vector2(150, 300), new Point(75, 75), 10, new Point(0, 0),
new Point(6, 8), new Vector2(3, 4), "skullcollision", this, .75f, 150));
spriteList.Add(new EvadingSprite(
Game.Content.Load<Texture2D>(@"Images/skullball"),
new Vector2(300, 400), new Point(75, 75), 10, new Point(0, 0),
new Point(6, 8), new Vector2(0, -3), "skullcollision", this, .75f, 150));
spriteList.Add(new EvadingSprite(
Game.Content.Load<Texture2D>(@"Images/skullball"),
new Vector2(200, 300), new Point(75, 75), 10, new Point(0, 0),
new Point(6, 8), new Vector2(-3, 7), "skullcollision", this, .75f, 150));
base.LoadContent();
}
public override void Update(GameTime gameTime)
{
nextSpawnTime -= gameTime.ElapsedGameTime.Milliseconds;
if (nextSpawnTime < 0)
{
SpawnEnemy();
//reset spawn timer
ResetSpawnTime();
}
// Update player
player.Update(gameTime, Game.Window.ClientBounds);
// Update all sprites
for (int i = 0; i < spriteList.Count; ++i)
{
Sprite s = spriteList[i];
s.Update(gameTime, Game.Window.ClientBounds);
// Check for collisions
if (s.collisionRect.Intersects(player.collisionRect) && (Keyboard.GetState().IsKeyDown(Keys.A)))
{
// Play collision sound
if (s.collisionCueName != null)
((Game1)Game).PlayCue(s.collisionCueName);
// Remove collided sprite from the game
spriteList.RemoveAt(i);
--i;
}
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
// Draw the player
player.Draw(gameTime, spriteBatch);
// Draw all sprites
foreach (Sprite s in spriteList)
s.Draw(gameTime, spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
// Return current position of the player sprite
public Vector2 GetPlayerPosition()
{
return player.GetPosition;
}
private void SpawnEnemy()
{
Vector2 speed = Vector2.Zero;
Vector2 position = Vector2.Zero;
// Default frame size
Point frameSize = new Point(75, 75);
// Create the sprite. NOTE: This sprite bounces off the bottom wall then goes back and disappears offscreen
spriteList.Add(
new EvadingSprite(Game.Content.Load<Texture2D>(@"images\skullball"),
position, new Point(75, 75), 10, new Point(0, 0),
new Point(6, 8), new Vector2(3, 4), "skullcollision", this, .75f, 150));
}
private void ResetSpawnTime()
{
nextSpawnTime = ((Game1)Game).rnd.Next(
enemySpawnMinMilliseconds,
enemySpawnMaxMilliseconds);
}
}
}
EvadingSprite
class EvadingSprite : Sprite
{
// Save a reference to the sprite manager to
// use to get the player position
SpriteManager spriteManager;
// Variables to delay evasion until player is close
float evasionSpeedModifier;
int evasionRange;
bool evade = false;
public EvadingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, string collisionCueName,
SpriteManager spriteManager, float evasionSpeedModifier,
int evasionRange)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, collisionCueName)
{
this.spriteManager = spriteManager;
this.evasionSpeedModifier = evasionSpeedModifier;
this.evasionRange = evasionRange;
}
public EvadingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, int millisecondsPerFrame,
string collisionCueName, SpriteManager spriteManager,
float evasionSpeedModifier, int evasionRange)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, millisecondsPerFrame,
collisionCueName)
{
this.spriteManager = spriteManager;
this.evasionSpeedModifier = evasionSpeedModifier;
this.evasionRange = evasionRange;
}
public override Vector2 direction
{
get { return speed; }
}
public override void Update(GameTime gameTime, Rectangle clientBounds)
{
// First, move the sprite along its direction vector
position += speed;
// Use the player position to move the sprite closer in
// the X and/or Y directions
Vector2 player = spriteManager.GetPlayerPosition();
if (evade)
{
// Move away from the player horizontally
if (player.X < position.Y)
position.X += Math.Abs(speed.Y);
else if (player.X > position.X)
position.X -= Math.Abs(speed.Y);
// Move away from the player vertically
if (player.Y < position.Y)
position.Y += Math.Abs(speed.X);
else if (player.Y > position.Y)
position.Y -= Math.Abs(speed.X);
}
else
{
if (Vector2.Distance(position, player) < evasionRange)
{
// Player is within evasion range,
// reverse direction and modify speed
speed *= -evasionSpeedModifier;
evade = true;
}
}
//make them bounce off walls
if (position.X > clientBounds.Width - frameSize.X ||
position.X < 0)
speed *= -1;
if (position.Y > clientBounds.Height - frameSize.Y ||
position.Y < 0)
speed *= -1;
base.Update(gameTime, clientBounds);
}
}
答案 0 :(得分:1)
对于您的碰撞代码:
if (position.Y > clientBounds.Height - frameSize.Y ||
position.Y < 0)
speed *= -1;
你告诉精灵只去上去。基本上当它在y上击中-1时,它向正方向移动,直到它变为正向,在这种情况下它移动为负。因此,您可以在正面和负面屏幕之间切换。我的建议是将这些陈述分成两部分,这样当你不回避你的时候就会准确地从墙上蹦蹦跳跳。
if (position.X > clientBounds.Width - frameSize.X)
speed *= -1;
else if (position.X<=0)
speed*=1;
if (position.Y > ClientBounds.Height - frameSize.Y)
speed *= -1;
else if(position.Y > 0)
speed *= 1;