C# 敌人总是开枪
如果nextShot的变量大于shot的变量频率,我希望敌人发射子弹。每个敌人都应该独立开火。 但就目前而言,每一个敌人都在不停地射击。他们的射门之间没有休息。但我希望他们在拍摄之间总是有一点休息。我肯定敌人的职业有问题,但我不知道该怎么改变。谁能帮帮我吗C# 敌人总是开枪,c#,xna,C#,Xna,如果nextShot的变量大于shot的变量频率,我希望敌人发射子弹。每个敌人都应该独立开火。 但就目前而言,每一个敌人都在不停地射击。他们的射门之间没有休息。但我希望他们在拍摄之间总是有一点休息。我肯定敌人的职业有问题,但我不知道该怎么改变。谁能帮帮我吗 public class Map { Texture2D myEnemy, myBullet; Player Player; List<Enemy> enemieslist = new List<En
public class Map
{
Texture2D myEnemy, myBullet;
Player Player;
List<Enemy> enemieslist = new List<Enemy>();
List<Bullet> bulletslist = new List<Bullet>();
float fNextEnemy = 0.0f;
float fEnemyFreq = 2.0f;
Vector2 Startposition = new Vector2(200, 200);
Vector2 CurrentEnemyPosition;
GraphicsDeviceManager graphicsDevice;
public Map(GraphicsDeviceManager device)
{
graphicsDevice = device;
}
public void Load(ContentManager content)
{
myEnemy = content.Load<Texture2D>("enemy");
myBullet = content.Load<Texture2D>("bullet");
Player = new Player(graphicsDevice);
Player.Load(content);
}
public void Update(GameTime gameTime)
{
Player.Update(gameTime);
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
for(int i = enemieslist.Count - 1; i >= 0; i--)
{
// Update Enemy
Enemy enemy = enemieslist[i];
enemy.Update(gameTime, this.graphicsDevice, Player.playershape.Position, delta);
CurrentEnemyPosition = enemy.Bulletstartposition;
// Does the enemy shot?
if (enemy.Shot == true)
// New bullet
{
Vector2 bulletDirection = Vector2.Normalize(Player.playershape.Position - CurrentEnemyPosition) * 200f;
bulletslist.Add(new Bullet(CurrentEnemyPosition, bulletDirection, Player.playershape.Position));
}
}
this.fNextEnemy += delta;
//New enemy
if (this.fNextEnemy >= fEnemyFreq)
{
Vector2 enemyDirection = Vector2.Normalize(Player.playershape.Position - Startposition) * 100f;
enemieslist.Add(new Enemy(Startposition, enemyDirection, Player.playershape.Position));
fNextEnemy = 0;
}
for(int i = bulletslist.Count - 1; i >= 0; i--)
{
// Update Bullet
Bullet bullets = bulletslist[i];
bullets.Update(gameTime, this.graphicsDevice, delta);
}
}
public void Draw(SpriteBatch batch)
{
Player.Draw(batch);
foreach (Enemy enemies in enemieslist)
{
enemies.Draw(batch, myEnemy);
}
foreach (Bullet bullets in bulletslist)
{
bullets.Draw(batch, myBullet);
}
}
}
公共类映射
{
我的敌人,我的子弹;
玩家;
List enemieslist=新列表();
列表公告列表=新列表();
浮点数fnextemy=0.0f;
浮动fEnemyFreq=2.0f;
矢量2起始位置=新矢量2(200200);
矢量2当前位置;
GraphicsDeviceManager图形设备;
公共地图(GraphicsDeviceManager设备)
{
图形设备=设备;
}
公共无效加载(ContentManager内容)
{
myEnemy=content.Load(“敌人”);
myBullet=content.Load(“bullet”);
玩家=新玩家(图形设备);
Player.Load(内容);
}
公开作废更新(游戏时间游戏时间)
{
玩家更新(游戏时间);
浮点增量=(浮点)gameTime.ElapsedGameTime.TotalSeconds;
对于(int i=enemieslist.Count-1;i>=0;i--)
{
//更新敌人
敌人=敌人[我];
更新(游戏时间,this.graphicsDevice,Player.playershape.Position,delta);
CurrentEnemyPosition=敌人。BullettStartPosition;
//敌人开枪了吗?
如果(敌方射击==真)
//新子弹
{
Vector2 bulletDirection=Vector2.规格化(Player.playershape.Position-currentEneyPosition)*200f;
bulletslist.Add(新的Bullet(当前我的位置,bulletDirection,Player.playershape.Position));
}
}
this.fnextemy+=delta;
//新敌人
如果(this.fnextemy>=fEnemyFreq)
{
Vector2 enemyDirection=Vector2.规范化(Player.playershape.Position-Startposition)*100f;
敌人列表。添加(新敌人(起始位置,敌人方向,玩家。玩家。位置));
fnextemy=0;
}
对于(int i=bulletslist.Count-1;i>=0;i--)
{
//更新项目符号
子弹=子弹列表[i];
更新(游戏时间,this.graphicsDevice,delta);
}
}
公共作废取款(SpriteBatch批量)
{
玩家。抽签(批量);
foreach(敌人列表中的敌人)
{
敌人。抽签(批,我的敌人);
}
foreach(bulletslist中的子弹)
{
项目符号。绘制(批次,myBullet);
}
}
}
敌人阶级:
public class Enemy
{
private float nextShot = 0;
private float shotFrequency = 1.0f;
Vector2 vPos;
Vector2 vMove;
Vector2 vPlayer;
public Vector2 Bulletstartposition;
public bool Remove;
public bool Shot;
public Enemy(Vector2 Pos, Vector2 Move, Vector2 Player)
{
this.vPos = Pos;
this.vMove = Move;
this.vPlayer = Player;
this.Remove = false;
this.Shot = false;
}
public void Update(GameTime gameTime, GraphicsDeviceManager graphics, Vector2 PlayerPos, float delta)
{
nextShot += delta;
if (nextShot >= shotFrequency)
{
this.Shot = true;
nextShot = 0;
}
if (!Remove)
{
float fMoveTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
this.vMove = Vector2.Normalize(PlayerPos - this.vPos) * 100f;
this.vPos += this.vMove * fMoveTime;
Bulletstartposition = this.vPos;
if (this.vPos.X > graphics.PreferredBackBufferWidth + 1)
{
this.Remove = true;
}
else if (this.vPos.X < -20)
{
this.Remove = true;
}
if (this.vPos.Y > graphics.PreferredBackBufferHeight + 1)
{
this.Remove = true;
}
else if (this.vPos.Y < -20)
{
this.Remove = true;
}
}
}
public void Draw(SpriteBatch spriteBatch, Texture2D myTexture)
{
if (!Remove)
{
spriteBatch.Draw(myTexture, this.vPos, Color.White);
}
}
}
公敌
{
私有浮动nextShot=0;
私人浮动频率=1.0f;
向量2 VPO;
矢量2运动;
向量2层;
公共矢量2 BullettStartPosition;
公共房屋拆迁;
公共枪杀;
公敌(矢量2位置,矢量2移动,矢量2玩家)
{
this.vPos=Pos;
this.vMove=移动;
this.vPlayer=播放器;
这个.Remove=false;
这个.Shot=false;
}
public void更新(游戏时间游戏时间、图形管理员图形、矢量2播放器、浮动增量)
{
nextShot+=delta;
如果(下一次拍摄>=拍摄频率)
{
这个.Shot=true;
nextShot=0;
}
如果(!删除)
{
float-fMoveTime=(float)gameTime.ElapsedGameTime.TotalSeconds;
this.vMove=Vector2.Normalize(PlayerPos-this.vPos)*100f;
this.vPos+=this.vMove*fMoveTime;
Bulletstartposition=this.vPos;
if(this.vPos.X>graphics.PreferredBackBufferWidth+1)
{
这个.Remove=true;
}
否则如果(此.vPos.X<-20)
{
这个.Remove=true;
}
if(this.vPos.Y>graphics.PreferredBackBufferHeight+1)
{
这个.Remove=true;
}
否则如果(此.vPos.Y<-20)
{
这个.Remove=true;
}
}
}
公共空白绘制(SpriteBatch SpriteBatch,纹理2D myTexture)
{
如果(!删除)
{
spriteBatch.Draw(myTexture,this.vPos,Color.White);
}
}
}
子弹级
public class Bullet
{
Vector2 vPos;
Vector2 vMove;
Vector2 vPlayer;
public bool Remove;
public Bullet(Vector2 Pos, Vector2 Move, Vector2 Player)
{
this.Remove = false;
this.vPos = Pos;
this.vMove = Move;
this.vPlayer = Player;
}
public void Update(GameTime gameTime, GraphicsDeviceManager graphics, float delta)
{
if (!Remove)
{
float fMoveTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
this.vPos.X += this.vMove.X * fMoveTime;
this.vPos.Y += this.vMove.Y * fMoveTime;
if (this.vPos.X > graphics.PreferredBackBufferWidth +1)
{
this.Remove = true;
}
else if (this.vPos.X < -20)
{
this.Remove = true;
}
if (this.vPos.Y > graphics.PreferredBackBufferHeight +1)
{
this.Remove = true;
}
else if (this.vPos.Y < -20)
{
this.Remove = true;
}
}
}
public void Draw(SpriteBatch spriteBatch, Texture2D myTexture)
{
if (!Remove)
{
spriteBatch.Draw(myTexture, this.vPos, Color.White);
}
}
}
公共类项目符号
{
向量2 VPO;
矢量2运动;
向量2层;
公共房屋拆迁;
公共子弹(矢量2位置,矢量2移动,矢量2播放器)
{
这个.Remove=false;
this.vPos=Pos;
this.vMove=移动;
this.vPlayer=播放器;
}
公共无效更新(游戏时间游戏时间,图形管理员图形,浮动增量)
{
如果(!删除)
{
float-fMoveTime=(float)gameTime.ElapsedGameTime.TotalSeconds;
this.vPos.X+=this.vMove.X*fMoveTime;
this.vPos.Y+=this.vMove.Y*fMoveTime;
if(this.vPos.X>graphics.PreferredBackBufferWidth+1)
{
这个.Remove=true;
}
否则如果(此.vPos.X<-20)
{
这个.Remove=true;
}
if(this.vPos.Y>graphics.PreferredBackBufferHeight+1)
{
这个.Remove=true;
}
否则如果(此.vPos.Y<-20)
{
这个.Remove=true;
}
}
}
公共空白绘制(SpriteBatch SpriteBatch,纹理2D myTexture)
{
如果(!删除)
{
spriteBatch.Draw(myTexture,this.vPos,Color.White);
}
}
}
您永远不会清除快照
标志。等你
nextShot -= shotFrequency;