今天和女朋友分手,写一段技术博客以平静心态。
用于主界面显示各种信息
//统计
public struct t_statistics
{
public int generateenemyNum; //生成敌人数量
public int destroyenemyNum; //已经摧毁的敌人数量
public int GetcurrenemyNum() //当前敌人数量
{
return generateenemyNum - destroyenemyNum;
}
public int GetremainingenemyNum() //剩余的
{
return t_commpar.TANKNUMMAX - destroyenemyNum;
}
public void init()
{
generateenemyNum = 0;
destroyenemyNum = 0;
}
public t_statistics(int g ,int d)
{
generateenemyNum = g;
destroyenemyNum = d;
}
}
按一定的随机顺序生成各种坦克
//生成序列,之后按此序列生成坦克
void generaterandseq(int[] seq, int len )
{
int i = 0;
for (i = 0; i< len ;i++ )
{
seq[i] = (i%4)+1;
}
for (i = 0; i < len/2; i++)
{
int r = rn.Next(0, 9);
if (r != i)
{
mymath.swap(ref seq[i], ref seq[r]);
}
}
for (i = 0; i < len / 2; i++)
{
int r = rn.Next(0, 9);
if (r != i)
{
mymath.swap(ref seq[i + 10], ref seq[r + 10]);
}
}
}
///
/// 生成敌军坦克
///
/// 开始位置
void generateEnemyTanks(Point startLocation)
{
tanks.Add(new tank(startLocation, randseq[randseqidx] ));
randseqidx++;
if (randseqidx >= t_commpar.TANKNUMMAX) randseqidx = 0;
dg_showtankcars += tanks[tanks.Count - 1].showtank;
dg_showtankbullets += tanks[tanks.Count - 1].showbullet;
tankacts.Add(tanks[tanks.Count - 1].tankact);
statistics.generateenemyNum++;
label_currenemynum.Text = statistics.GetcurrenemyNum().ToString();
}
定义委托方便使用
//委托
internal delegate void _dg_showtank(ref Graphics gr);
internal delegate void _dg_tankact( );
_dg_showtank dg_showtankcars, dg_showtankbullets;
玩家坦克与保存所有坦克的列表。
///
/// 轮询所有敌军并摧毁需要的
///
void dowaitForDestroyTanks()
{
// list注意删除顺序
for (int i = tanks.Count - 1; i >= 0; i--)
{
if (tanks[i].aliveCnt > 0)
{
tanks[i].aliveCnt--;
if (tanks[i].aliveCnt == 0)
{
destroyDestroyTanks(i);
}
}
}
}
//摧毁某个敌军
void destroyDestroyTanks(int i)
{
statistics.destroyenemyNum++;
label_destroyenemynum.Text = statistics.destroyenemyNum.ToString();
label_currenemynum.Text = statistics.GetcurrenemyNum().ToString();
label_remainingenemynum.Text = statistics.GetremainingenemyNum().ToString();
tankacts.RemoveAt(i);
dg_showtankbullets -= tanks[i].showbullet;
dg_showtankcars -= tanks[i].showtank;
tanks.RemoveAt(i);
}
//显示敌军被摧毁画面
void showtankBedestroyed( tank tk )
{
tk.aliveCnt = t_commpar.TANKALIVECNTMAX;
using (
Graphics g = Graphics.FromImage(tk.currcarimg)
)
{
g.DrawImage(bongimg, new Rectangle(0, 0, t_commpar.TANKSIZEX, t_commpar.TANKSIZEY));
}
}
游戏需要使用键盘进行操作,单独使用窗体的键盘事件是无法进行坦克的流畅操作。所有需要window API进行键盘状态读取。
[DllImport("user32.dll", EntryPoint="GetKeyState")]
public static extern int GetKeyState(int nVirKey);
bool iskeySPACEPress()
{
return ((GetKeyState((int)(Keys.Space)) & 0x8000) != 0 );
}
bool iskeyUPPress()
{
return ((GetKeyState((int)(Keys.Up)) & 0x8000) != 0);
}
bool iskeyRIGHTPress()
{
return ((GetKeyState((int)(Keys.Right)) & 0x8000) != 0);
}
bool iskeyLEFTPress()
{
return ((GetKeyState((int)(Keys.Left)) & 0x8000) != 0);
}
在定时读取键盘状态,操作坦克。
//主流程15MS/per
private void maintimer_Tick(object sender, EventArgs e)
{
updatatanksinfolist();
playertank.tankact();
for (int j = 0; j < tanks.Count; j++)
{
tankacts[j]( );
}
map.mainimg = mainimg;
pbx_main.BackgroundImage = (Bitmap)mainimg.Clone();
gr = Graphics.FromImage(pbx_main.BackgroundImage);
int n = tanks.Count;
for (var i = 0; i 0)
{
showplayerBeAttacted();
}
else {
//END
showplayerBeAttacted();
playertank.isalive = false;
}
}
if ( tanks[i].isalive )
if ( playertank.bingo == tanks[i].uuid)//检测玩家是否打击中敌军
{
playertank.bingo = 0;
tanks[i].Hp -= playertank.Atk;
//每次重新加载避免音频丢失
if( ckb_shitmusic.Checked)
(new SoundPlayer(global::tank.Properties.Resources.tankbeattact)).Play();
if (tanks[i].Hp > 0)
{
showtankinjured(i);
}
else
{
tanks[i].isalive = false;
showtankinjured(i);
showtankBedestroyed(tanks[i]);
}
}
}
dg_showtankbullets(ref gr);
dg_showtankcars(ref gr);
}
1.
坦克大战A
2.
坦克大战B
3.
坦克大战C
4.
坦克大战D
心情很差,胡乱放些游戏效果。