-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2Player.cs
197 lines (185 loc) · 6.14 KB
/
P2Player.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using My90Tank.Properties;
using System.Windows.Forms;
using System.Media;
namespace My90Tank
{
public class P2Player:Member
{
public static Image[] images = new Image[] { Resources.p2tankU, Resources.p2tankD, Resources.p2tankL, Resources.p2tankR };
public static Image[] bornImages = new Image[] {Resources.born1,Resources.born2,Resources.born3,Resources.born4};
private bool isMove = false;
public const int myTankBornTime = 16;
// private int lifeNum = 3;
public P2Player(int x,int y, int life, int speed, int power,DIRECTION direct)
:base(x,y,life,speed,power,direct)
{
this.Width = 60;
this.Height = 60;
}
public void KeyDown(KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.J:
if (this.direct != DIRECTION.LEFT)
IsBlocked = false;
this.direct = DIRECTION.LEFT;
isMove = true;
break;
case Keys.I:
if (this.direct != DIRECTION.UP)
IsBlocked = false;
this.direct = DIRECTION.UP;
isMove = true;
break;
case Keys.K:
if (this.direct != DIRECTION.DOWN)
IsBlocked = false;
this.direct = DIRECTION.DOWN;
isMove = true;
break;
case Keys.L:
if (this.direct != DIRECTION.RIGHT)
IsBlocked = false;
this.direct = DIRECTION.RIGHT;
isMove = true;
break;
case Keys.X:
this.life = 9999999;
break;
default:
break;
}
//按下就开始根据按下的键移动
Scene.Instance.CheckBlock(this);
Move();
}
public void KeyUp(KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.J:
if(this.direct == DIRECTION.LEFT)
isMove = false;
break;
case Keys.I:
if(this.direct == DIRECTION.UP)
isMove = false;
break;
case Keys.K:
if(this.direct == DIRECTION.DOWN)
isMove = false;
break;
case Keys.L:
if(this.direct == DIRECTION.RIGHT)
isMove = false;
break;
case Keys.Enter:
Fire();break;
default:
break;
}
}
/// <summary>
/// 通过增加一个missile对象到singleton中
/// </summary>
public void Fire()
{
SoundPlayer firesound = new SoundPlayer(Resources.fire);
firesound.Play();
int xx = this.X, yy = this.Y;
switch (this.direct)
{
case DIRECTION.UP:
yy -= this.speed;
xx += this.Width / 2;
break;
case DIRECTION.DOWN:
yy += this.speed+this.Height;
xx += this.Width / 2;
break;
case DIRECTION.LEFT:
xx -= this.speed;
yy += this.Width / 2;
break;
case DIRECTION.RIGHT:
xx += this.speed+this.Width;
yy += this.Width/2;
break;
default:
break;
}
Scene.Instance.AddElement(new Missile(xx, yy, 1, 25, this.power, this.direct));
}
public override void Move()
{
if (!isMove || IsBlocked)
{
return;
}
else
{
base.ChangePosition();
}
}
public bool IsBorn()
{
if (bornTime < P1Player.myTankBornTime)
return true;
else
return false;
}
public override void Draw(Graphics g)
{
//如果是出生状态,则需要出生时的闪烁图像序列;
if (IsBorn())
{
switch (bornTime % 8)
{
case 0:
case 1:
g.DrawImage(bornImages[0], this.X, this.Y);
break;
case 2:
case 3:
g.DrawImage(bornImages[1], this.X, this.Y);
break;
case 4:
case 5:
g.DrawImage(bornImages[2], this.X, this.Y);
break;
case 6:
case 7:
g.DrawImage(bornImages[3], this.X, this.Y);
break;
}
bornTime++;
}
else //已经出生,就直接根据方向和位置话;
{
switch (direct)
{
case DIRECTION.UP:
g.DrawImage(images[0], this.X, this.Y);
break;
case DIRECTION.DOWN:
g.DrawImage(images[1], this.X, this.Y);
break;
case DIRECTION.LEFT:
g.DrawImage(images[2], this.X, this.Y);
break;
case DIRECTION.RIGHT:
g.DrawImage(images[3], this.X, this.Y);
break;
default:
break;
}
}
}
}
}