前言
该程序主要对原始的坦克大战游戏进行了简单的还原。目前程序可以做到自动生成敌方坦克且敌方能够随机发射子弹,我方坦克也能做到边发射子弹边移动。唯一的不足之处就是还没有完整通关的设置以及障碍的设置。
界面效果图
图1
部分代码
using System
;
using System
.Collections
.Generic
;
using System
.ComponentModel
;
using System
.Data
;
using System
.Drawing
;
using System
.Linq
;
using System
.Text
;
using System
.Windows
.Forms
;
using System
.Runtime
.InteropServices
;
namespace MyTankWar
{
public partial class FormMain
: Form
{
private GameState _gameState
= GameState
.Close
;
private Tank _myTank
= new
Tank(Side
.Me
);
private List
<Tank
> _listEnemyTank
= new List
<Tank
>();
private List
<Bullet
> _listBullet
= new List
<Bullet
>();
[DllImport("user32.dll", CharSet
= CharSet
.Auto
, ExactSpelling
= true
, CallingConvention
= CallingConvention
.Winapi
)]
public
static extern short GetAsyncKeyState(int keyCode
);
public
FormMain()
{
InitializeComponent();
}
private
void BeginToolStripMenuItem_Click(object sender
, EventArgs e
)
{
_gameState
= GameState
.Open
;
timer1
.Enabled
= true
;
timer2
.Enabled
= true
;
timer3
.Enabled
= true
;
timer4
.Enabled
= true
;
}
private
void EndToolStripMenuItem_Click(object sender
, EventArgs e
)
{
_gameState
= GameState
.Close
;
timer1
.Enabled
= false
;
timer2
.Enabled
= false
;
timer3
.Enabled
= false
;
timer4
.Enabled
= false
;
}
private
void pictureBox1_Paint(object sender
, PaintEventArgs e
)
{
_myTank
.DrawMe(e
.Graphics
);
for (int i
= 0; i
<= _listEnemyTank
.Count
- 1; i
++)
_listEnemyTank
[i
].DrawMe(e
.Graphics
);
foreach
(Bullet myBullet in _listBullet
)
{
myBullet
.DrawMe(e
.Graphics
);
}
}
private
void FormMain_KeyDown(object sender
, KeyEventArgs e
)
{
if (_gameState
== GameState
.Open
)
{
if (e
.KeyCode
== Keys
.Space
)
{
Bullet myBullet
= _myTank
.Fire();
_listBullet
.Add(myBullet
);
}
pictureBox1
.Invalidate();
}
}
private
void timer1_Tick(object sender
, EventArgs e
)
{
if (_gameState
== GameState
.Open
)
{
Tank enemyTank
= new
Tank(Side
.Enemy
);
_listEnemyTank
.Add(enemyTank
);
pictureBox1
.Invalidate();
}
}
private
void timer2_Tick(object sender
, EventArgs e
)
{
if (_gameState
== GameState
.Open
)
{
Random myRand
= new
Random(DateTime
.Now
.Second
);
for (int i
= 0; i
<= _listEnemyTank
.Count
- 1; i
++)
{
int newDirection
= myRand
.Next(1, 10);
if (newDirection
<= 4)
_listEnemyTank
[i
].Move((Direction
)newDirection
);
else
_listEnemyTank
[i
].Move(_listEnemyTank
[i
]._Direction
);
}
foreach
(Bullet Bullet in _listBullet
)
Bullet
.Move();
pictureBox1
.Invalidate();
}
}
private
void timer3_Tick(object sender
, EventArgs e
)
{
if (_gameState
== GameState
.Open
)
{
Random myRand
= new
Random(DateTime
.Now
.Second
);
foreach
(Tank enemyTank in _listEnemyTank
)
{
int fireFlag
= myRand
.Next(1, 10);
if (fireFlag
<= 4)
{
Bullet enemyBullet
= enemyTank
.Fire();
_listBullet
.Add(enemyBullet
);
}
}
}
}
private
void timer4_Tick(object sender
, EventArgs e
)
{
if (_gameState
== GameState
.Open
)
{
bool keyDown
= (((ushort
)GetAsyncKeyState((int)Keys
.Down
)) & 0xffff) != 0;
if (keyDown
== true
)
_myTank
.Move(Direction
.Down
);
bool keyUp
= (((ushort
)GetAsyncKeyState((int)Keys
.Up
)) & 0xffff) != 0;
if (keyUp
== true
)
_myTank
.Move(Direction
.Up
);
bool keyLeft
= (((ushort
)GetAsyncKeyState((int)Keys
.Left
)) & 0xffff) != 0;
if (keyLeft
== true
)
_myTank
.Move(Direction
.Left
);
bool keyRight
= (((ushort
)GetAsyncKeyState((int)Keys
.Right
)) & 0xffff) != 0;
if (keyRight
== true
)
_myTank
.Move(Direction
.Right
);
pictureBox1
.Invalidate();
}
}
}
}
完整文件
坦克大战