C# 坦克大战经典杰作之战。向鬼子开炮吧!

C# 坦克大战经典杰作之战。向鬼子开炮吧!

首页战争策略开炮吧坦克更新时间:2024-05-09

添加时间控件:

private void timer1_Tick(object sender, EventArgs e) { if (_isMoving) { MoveTank(); Invalidate(); } if (_isFiring) { if (!_isBulletFlying) { _bulletX = _tankX 15; _bulletY = _tankY 15; _bulletDirection = _tankDirection; _isBulletFlying = true; } MoveBullet(); Invalidate(); } }控制方向键:

private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: _tankDirection = 0; _isMoving = true; break; case Keys.Down: _tankDirection = 1; _isMoving = true; break; case Keys.Left: _tankDirection = 2; _isMoving = true; break; case Keys.Right: _tankDirection = 3; _isMoving = true; break; case Keys.Space: _isFiring = true; break; } }引用方法1:

private void MoveBullet() { switch (_bulletDirection) { case 0: _bulletY -= BulletSpeed; break; case 1: _bulletY = BulletSpeed; break; case 2: _bulletX -= BulletSpeed; break; case 3: _bulletX = BulletSpeed; break; } if (_bulletX < 0 || _bulletX > ClientSize.Width || _bulletY < 0 || _bulletY > ClientSize.Height) { _isBulletFlying = false; } }引用方法2:

private void MoveTank() { switch (_tankDirection) { case 0: _tankY -= TankSpeed; break; case 1: _tankY = TankSpeed; break; case 2: _tankX -= TankSpeed; break; case 3: _tankX = TankSpeed; break; } if (_tankX < 0) _tankX = 0; if (_tankX > ClientSize.Width - 40) _tankX = ClientSize.Width - 40; if (_tankY < 0) _tankY = 0; if (_tankY > ClientSize.Height - 40) _tankY = ClientSize.Height - 40; }制作生成子弹:

private void DrawBullet(Graphics g) { g.FillEllipse(Brushes.Red, _bulletX, _bulletY, 20, 20); }制作坦克图案:

private void DrawTank(Graphics g) { var tankRect = new Rectangle(_tankX, _tankY, 40, 40); g.FillRectangle(Brushes.Pink, tankRect); switch (_tankDirection) { case 0: g.FillRectangle(Brushes.Black, _tankX 18, _tankY, 4, 20); break; case 1: g.FillRectangle(Brushes.Black, _tankX 18, _tankY 20, 4, 20); break; case 2: g.FillRectangle(Brushes.Black, _tankX, _tankY 18, 20, 4); break; case 3: g.FillRectangle(Brushes.Black, _tankX 20, _tankY 18, 20, 4); break; } }窗体初始化:

private const int TankSpeed = 5; private const int BulletSpeed = 10; private bool _isMoving; private bool _isFiring; private bool _isBulletFlying; private int _bulletX; private int _bulletY; private int _bulletDirection; private int _tankX; private int _tankY; private int _tankDirection; public Form1() { InitializeComponent(); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); _tankX = ClientSize.Width / 2; _tankY = ClientSize.Height / 2; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawTank(e.Graphics); if (_isBulletFlying) { DrawBullet(e.Graphics); } }



查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved