-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
122 lines (111 loc) · 3.76 KB
/
Player.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Windows.Forms;
namespace SpaceInvaders
{
public class Player : GameEntity
{
//field
bool U = false, D = false, L = false, R = false, EnableShoot = false;
//constructor for player class
public Player(Form Form)
{
this.Form = Form;
//create picturebox for player
picEntity = new PictureBox();
picEntity.Size = new System.Drawing.Size(40, 40);
picEntity.SizeMode = PictureBoxSizeMode.StretchImage;
picEntity.Image = SpaceInvaders.Properties.Resources.ship;
picEntity.Location = new System.Drawing.Point(300, this.Form.ClientSize.Height - 75);
//add to form
this.Form.Controls.Add(picEntity);
//set player velocity to 20
Velocity = 27;
}
public void InputDown(KeyEventArgs Key)
{
//for each key check if they are clicked
if (Key.KeyCode == Keys.W) U = true;
if (Key.KeyCode == Keys.S) D = true;
if (Key.KeyCode == Keys.A) L = true;
if (Key.KeyCode == Keys.D) R = true;
if (Key.KeyCode == Keys.Space)
//player shoots a maximum of 5 bullets at a time
if (List.Bullets.Count < 5 && EnableShoot)
{
List.Bullets.Add(new Bullet(Form));
Sound.ChangeSoundURL("Fire.mp3");
Sound.PlaySound();
}
}
public void InputUp(KeyEventArgs Key)
{
//check if each key is released
if (Key.KeyCode == Keys.W) U = false;
if (Key.KeyCode == Keys.S) D = false;
if (Key.KeyCode == Keys.D) R = false;
if (Key.KeyCode == Keys.A) L = false;
}
public override void Update()
{
//depending on which key is clicked, player will move in that direction
//if player's movement exceeds form's dimensions, player would location would be set to specific locations
if (L)
{
if (picEntity.Left < Velocity)
picEntity.Left = 0;
else
picEntity.Left -= Velocity;
}
if (R)
{
if (picEntity.Left > 870 - Velocity)
picEntity.Left = 870;
else
picEntity.Left += Velocity;
}
if (U)
{
if (picEntity.Top < 415 + Velocity)
picEntity.Top = 415;
else
picEntity.Top -= Velocity;
}
if (D)
{
if (picEntity.Top > 570 - Velocity)
picEntity.Top = 570;
else
picEntity.Top += Velocity;
}
//loop through each invader bullet to check if they intersect with player
foreach (InvaderBullet bull in List.InvaderBullets)
{
if (bull.Bounds.IntersectsWith(this.Bounds))
{
Destroy();
return;
}
}
foreach (Enemy Invader in List.Enemies)
{
if (Invader.Bounds.IntersectsWith(this.Bounds))
{
Destroy();
Invader.Destroy();
return;
}
}
}
public override void Destroy()
{
//dispose player
picEntity.Dispose();
picEntity.Visible = false;
}
//property
public bool CanShoot
{
get { return EnableShoot; }
set { EnableShoot = value; }
}
}
}