如何受到敌人(子弹)的伤害
How to take damage from enemies (bullets)
各位程序员大家好!很抱歉打扰你,但我有一个学校项目是创建一个游戏。我快完成了我所要做的就是让我的角色(玩家)受到敌人子弹的伤害。
这是我的敌人 class 我的子弹列表让敌人发射子弹。你可以看到我试图通过温度来跟踪子弹的位置。(我按照这些制作敌人的教程https://www.youtube.com/watch?v=_TlnUM-uhSI and https://www.youtube.com/watch?v=tfiKwOo_4xo)
在你指责我之前,我只想说我已经到处寻找答案,但由于 XNA 对我想要创建的内容非常有限,这让我很难做到。
class Enemies
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity;
public bool isVisible = true;
Random random = new Random();
int randX, randY;
float temp_bulletenemyX;
float temp_bulletenemyY;
// bullets
private List<Bullets> bullets = new List<Bullets>();
Texture2D bulletTexture;
public Enemies(Texture2D NewTexture, Vector2 NewPosition, Texture2D newBulletTexture)
{
texture = NewTexture;
position = NewPosition;
randY = random.Next(-4, 4);
randX = random.Next(-4, -1);
velocity = new Vector2(randX, randY);
bulletTexture = newBulletTexture;
}
float shoot = 0;
public void update(GraphicsDevice graphics, GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
position += velocity;
if (position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height)
velocity.Y = -velocity.Y;
if (position.X < 0 - texture.Width)
isVisible = false;
shoot += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (shoot > 1)
{
shoot = 0;
Shootbullet();
}
updateBullets();
}
public void updateBullets()
{
foreach (Bullets bullet in bullets)
{
bullet.position += bullet.velocity;
temp_bulletenemyX = bullet.position.X;
temp_bulletenemyY = bullet.position.Y;
if (bullet.position.X < 0)
bullet.isVisible = false;
}
for (int i = 0; i < bullets.Count; i++)
if(!bullets[i].isVisible)
{
bullets.RemoveAt(i);
i--;
}
}
public void Shootbullet()
{
Bullets newBullet = new Bullets(bulletTexture);
newBullet.velocity.X = velocity.X - 3f;
newBullet.position = new Vector2(position.X + newBullet.velocity.X, position.Y + (texture.Height / 2) - (bulletTexture.Height / 2));
newBullet.isVisible = true;
if (bullets.Count() < 3) // hur många skott den skall skjuta
bullets.Add(newBullet);
}
public void draw(SpriteBatch spriteBatch)
{
foreach (Bullets bullet in bullets)
bullet.Draw(spriteBatch);
spriteBatch.Draw(texture, position, Color.White);
}
public float PosX
{
get
{
return position.X;
}
}
public Vector2 Pos
{
get
{
return position;
}
}
public float PosY
{
get
{
return position.Y;
}
}
public List<Bullets> GetbulletList
{
get{
return bullets;
}
}
public Texture2D text
{
get
{
return texture;
}
}
public Texture2D BulletText
{
get
{
return bulletTexture;
}
这是我尝试编写的基本 Game1 代码,因此我的角色可以受到伤害。
player.rectangle = new Rectangle(Convert.ToInt32(player.PosX), Convert.ToInt32(player.PosY), player.text.Width, player.text.Height);
foreach (Enemies bullet in enemies.ToList())
{
rec_bullet = new Rectangle(Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.X), Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.Y), nyenemy.BulletText.Width, nyenemy.BulletText.Height);
hit = CheckCollision(rec_bullet, player.rectangle);
if (hit == true)
{
player.health -= 10;
hit = false;
}
i++;
如果一切都乱七八糟,我很抱歉,我通过遵循许多教程和我自己的一些编码将所有东西放在一起。如果我违反了论坛规则,我也很抱歉,我是新来的。
问候 Kiar。
我会勇敢地提出以下建议 - 查看提供的代码片段。
在 Game1 代码中 - 您正在努力解决敌人,但看起来您并没有在子弹列表中搜索每个敌人。您将获得带有 'i' 的 ElementAt(..) - 向其添加一个,然后转到下一个敌人,然后仅获得带有递增的 'i'.[=11= 的 ElementAt(..) ]
根据我的理解-代码的流程应该是:
for each enemy in enemy list
for each bullet in enemy bullet list
Check Collision with Player
if collision then adjust player health
您的代码有一些问题。对于初学者,您的 Bullet class 应该包含 bulletUpdate() 方法,该方法应该在 foreach 循环中调用。
foreach (Bullet b in bullets)
{
b.update(/*parameters here*/);
}
您的子弹列表应该是 public 列表,存储在您的 Game1.cs 或 EnemyManager class 中,而不是每个敌人中。向每个敌人传递对该列表的引用,以便他们开火时可以将子弹添加到列表中。
最重要的是,你的碰撞测试应该只是每颗子弹和你自己之间的相交测试。同样,另一个 foreach 循环。
foreach(Bullet b in bullets)
{
//where playerRect is the hitbox for your player and b.Rect is the hitbox for your bullet
if(playerRect.Intersects(b.Rect)
player.healh-=damage;
}
希望对您有所帮助。
各位程序员大家好!很抱歉打扰你,但我有一个学校项目是创建一个游戏。我快完成了我所要做的就是让我的角色(玩家)受到敌人子弹的伤害。
这是我的敌人 class 我的子弹列表让敌人发射子弹。你可以看到我试图通过温度来跟踪子弹的位置。(我按照这些制作敌人的教程https://www.youtube.com/watch?v=_TlnUM-uhSI and https://www.youtube.com/watch?v=tfiKwOo_4xo)
在你指责我之前,我只想说我已经到处寻找答案,但由于 XNA 对我想要创建的内容非常有限,这让我很难做到。
class Enemies
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity;
public bool isVisible = true;
Random random = new Random();
int randX, randY;
float temp_bulletenemyX;
float temp_bulletenemyY;
// bullets
private List<Bullets> bullets = new List<Bullets>();
Texture2D bulletTexture;
public Enemies(Texture2D NewTexture, Vector2 NewPosition, Texture2D newBulletTexture)
{
texture = NewTexture;
position = NewPosition;
randY = random.Next(-4, 4);
randX = random.Next(-4, -1);
velocity = new Vector2(randX, randY);
bulletTexture = newBulletTexture;
}
float shoot = 0;
public void update(GraphicsDevice graphics, GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
position += velocity;
if (position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height)
velocity.Y = -velocity.Y;
if (position.X < 0 - texture.Width)
isVisible = false;
shoot += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (shoot > 1)
{
shoot = 0;
Shootbullet();
}
updateBullets();
}
public void updateBullets()
{
foreach (Bullets bullet in bullets)
{
bullet.position += bullet.velocity;
temp_bulletenemyX = bullet.position.X;
temp_bulletenemyY = bullet.position.Y;
if (bullet.position.X < 0)
bullet.isVisible = false;
}
for (int i = 0; i < bullets.Count; i++)
if(!bullets[i].isVisible)
{
bullets.RemoveAt(i);
i--;
}
}
public void Shootbullet()
{
Bullets newBullet = new Bullets(bulletTexture);
newBullet.velocity.X = velocity.X - 3f;
newBullet.position = new Vector2(position.X + newBullet.velocity.X, position.Y + (texture.Height / 2) - (bulletTexture.Height / 2));
newBullet.isVisible = true;
if (bullets.Count() < 3) // hur många skott den skall skjuta
bullets.Add(newBullet);
}
public void draw(SpriteBatch spriteBatch)
{
foreach (Bullets bullet in bullets)
bullet.Draw(spriteBatch);
spriteBatch.Draw(texture, position, Color.White);
}
public float PosX
{
get
{
return position.X;
}
}
public Vector2 Pos
{
get
{
return position;
}
}
public float PosY
{
get
{
return position.Y;
}
}
public List<Bullets> GetbulletList
{
get{
return bullets;
}
}
public Texture2D text
{
get
{
return texture;
}
}
public Texture2D BulletText
{
get
{
return bulletTexture;
}
这是我尝试编写的基本 Game1 代码,因此我的角色可以受到伤害。
player.rectangle = new Rectangle(Convert.ToInt32(player.PosX), Convert.ToInt32(player.PosY), player.text.Width, player.text.Height);
foreach (Enemies bullet in enemies.ToList())
{
rec_bullet = new Rectangle(Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.X), Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.Y), nyenemy.BulletText.Width, nyenemy.BulletText.Height);
hit = CheckCollision(rec_bullet, player.rectangle);
if (hit == true)
{
player.health -= 10;
hit = false;
}
i++;
如果一切都乱七八糟,我很抱歉,我通过遵循许多教程和我自己的一些编码将所有东西放在一起。如果我违反了论坛规则,我也很抱歉,我是新来的。
问候 Kiar。
我会勇敢地提出以下建议 - 查看提供的代码片段。
在 Game1 代码中 - 您正在努力解决敌人,但看起来您并没有在子弹列表中搜索每个敌人。您将获得带有 'i' 的 ElementAt(..) - 向其添加一个,然后转到下一个敌人,然后仅获得带有递增的 'i'.[=11= 的 ElementAt(..) ]
根据我的理解-代码的流程应该是:
for each enemy in enemy list
for each bullet in enemy bullet list
Check Collision with Player
if collision then adjust player health
您的代码有一些问题。对于初学者,您的 Bullet class 应该包含 bulletUpdate() 方法,该方法应该在 foreach 循环中调用。
foreach (Bullet b in bullets)
{
b.update(/*parameters here*/);
}
您的子弹列表应该是 public 列表,存储在您的 Game1.cs 或 EnemyManager class 中,而不是每个敌人中。向每个敌人传递对该列表的引用,以便他们开火时可以将子弹添加到列表中。
最重要的是,你的碰撞测试应该只是每颗子弹和你自己之间的相交测试。同样,另一个 foreach 循环。
foreach(Bullet b in bullets)
{
//where playerRect is the hitbox for your player and b.Rect is the hitbox for your bullet
if(playerRect.Intersects(b.Rect)
player.healh-=damage;
}
希望对您有所帮助。