To learn and of course for fun I'm building a snake imitation, the only difference should be that instead of an endless chain, a lot of enemies or objects appear on the map and the game should be made more and more difficult.
The code snippet is supposed to show how I create my controls, but I have the problem that only the last created control is actually captured by my "IntersectsWith", all those created before that, as well as the very first one, are then no longer any opponents for me and my player just goes through them.
What is the best way to do this so that all created objects can be found under one definition so that I don't have to write hundreds of IntersectsWith If statements.
I would like to learn how to dynamically create controls and also dynamically have direct access to them, in which my player can interact with them through collision.
I really thought I could solve this with a random character generator and different control names can solve this, but alas - that's not the reality ^^
Random rnd = new Random();
PictureBox Enemy = new PictureBox();
private async void timer1_Tick(object sender, EventArgs e)
{
if (Person.Bounds.IntersectsWith(Food.Bounds))
{
int ascii_index2 = rnd.Next(97, 123);
char char1 = Convert.ToChar(ascii_index2);
char char2 = Convert.ToChar(ascii_index2);
string myletter = Convert.ToString(char1) Convert.ToString(char2);
Enemy = new PictureBox
{
Location = new Point(rnd.Next(20, playground.Right - 20), rnd.Next(20, playground.Bottom - 20)),
Name = "Enemy" myletter,
Size = new Size(24,24),
BackColor = Color.Red,
};
this.Controls.Add(Enemy);
Enemy.BringToFront();
Food.Location = new Point(rnd.Next(20, playground.Right - 20), rnd.Next(20, playground.Bottom - 20));
score ;
lbScore.Text = "Score: " score.ToString();
}
if(Person.Bounds.IntersectsWith(Enemy.Bounds))
{
timer1.Stop();
}
if (Person.Left <= playground.Left && !changed)
{
Person.Location = new Point(playground.Right, Person.Location.Y);
changed = true;
}
else
if (Person.Right >= playground.Right && !changed)
{
Person.Location = new Point(playground.Left, Person.Location.Y);
changed = true;
}
else
if (Person.Top <= playground.Top && !changed)
{
Person.Location = new Point(Person.Location.X, playground.Bottom);
changed = true;
}
else
if (Person.Bottom >= playground.Bottom && !changed)
{
Person.Location = new Point(Person.Location.X, playground.Top);
changed = true;
}
Here a picture from what i have : Player = BlackDot, Food = GreenDot, Enemy = RedDot It moves auto in the direction you clicked and you can move faster if you hold the Key. Move Keys: W,A,S,D
CodePudding user response:
You need to create a list and store all the created enemies in there.
First you need to create a List:
List<PictureBox> enemyList = new List<PictureBox>();
and after you've created a new Enemy add it to your List
Enemy = new PictureBox{};
enemieList.add(Enemy);
Because then you can check for every PictureBox in this List and whether it Intersects with your Player or not.
foreach(PictureBox p in enemyList)
{
if(Person.Bounds.IntersectsWith(p.Bounds))
{
timer1.Stop();
}
}
That would be my fast solution to your problem.
CodePudding user response:
Assuming that Person
is your player, instead of having an enemy
field of type PictureBox
, have an enemies
field of type List<PictureBox>
. You then add each new enemy PictureBox
to that list and then you can do this:
if (enemies.Any(enemy => person.Bounds.IntersectsWith(enemy.Bounds))