-
Notifications
You must be signed in to change notification settings - Fork 1
/
Enemy.cs
102 lines (92 loc) · 3.2 KB
/
Enemy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Windows.Forms;
namespace SpaceInvaders
{
public class Enemy : GameEntity
{
//field
public enum Direction { Right = 0, Down = 1, Left = 2, DownAgain = 3 };
Direction thisDirection = Direction.Right;
Random randomShot;
int ShootRate;
//contstructor for enemy class
public Enemy(Form Form, int x, int y, int LevelCounter)
{
//create picturebox for each enemy
picEntity = new PictureBox();
picEntity.Size = new System.Drawing.Size(List.dimensions, List.dimensions);
picEntity.Location = new System.Drawing.Point(x, y);
picEntity.SizeMode = PictureBoxSizeMode.StretchImage;
picEntity.Image = List.enemyImage[LevelCounter];
//add enemy to game form
this.Form = Form;
this.Form.Controls.Add(picEntity);
//store values for enemy properties
ShootRate = List.shootRates[LevelCounter];
Velocity = List.enemySpeed[LevelCounter];
randomShot = new Random();
}
public override void Destroy()
{
//dispose enemy
picEntity.Dispose();
}
public override void Update()
{
//move enemy in pattern right, down, left, down and repeat
if (thisDirection == Direction.Right)
{
picEntity.Left += Velocity;
//change direction when furthermost enemy hits right wall
foreach(Enemy Invader in List.Enemies)
{
if(Invader.Right > Form.ClientSize.Width)
{
Invader.Direct = Direction.Down;
this.Direct = Direction.Down;
}
}
}
else if (thisDirection == Direction.Down)
{
picEntity.Top += Velocity;
thisDirection = Direction.Left;
}
else if (thisDirection == Direction.Left)
{
picEntity.Left -= Velocity;
//change direction when furthermost enemy hits left wall
foreach (Enemy Invader in List.Enemies)
{
if (Invader.Left < 0)
{
Invader.Direct = Direction.DownAgain;
this.Direct = Direction.DownAgain;
}
}
}
else if (thisDirection == Direction.DownAgain)
{
picEntity.Top += Velocity;
thisDirection = Direction.Right;
}
//enemy shoot random bullets downward
if (List.InvaderBullets.Count < 6)
{
if (randomShot.Next(0, ShootRate) == 1)
List.InvaderBullets.Add(new InvaderBullet(this.Form, this));
}
}
//properties
public int Probablity
{
get { return ShootRate; }
set { ShootRate = value; }
}
public Direction Direct
{
get { return thisDirection; }
set { thisDirection = value; }
}
}
}