<原创>最简易的纯代码坦克大战

wartim 2009-10-12 04:58:43
加精
效果图

不到350行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication87
{
/// <summary>
/// Made by wartim 2009.10.12
/// var 1.0.0.0
/// </summary>
public partial class Form1 : Form
{
static int HEIGHT = 300;
static int WIDTH = 300;

Bitmap OrgBmp = new Bitmap(WIDTH, HEIGHT);
List<Tank> Tanks = new List<Tank>();
UserTank UTank = null;
List<Bullet> Bullets = new List<Bullet>();
PictureBox PB = new PictureBox();

public Form1()
{
InitializeComponent();

this.Size = new Size(WIDTH, HEIGHT);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);

using (Graphics G = Graphics.FromImage(OrgBmp))
G.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);

PB.Parent = this;
PB.Dock = DockStyle.Fill;
PB.Image = OrgBmp;

for (int i = 0; i < 5; i++)
{
Tanks.Add(new Tank(Color.Blue, this.BackColor));
Thread.Sleep(100);
}

UTank = new UserTank(Color.Red, this.BackColor);

Thread T = new Thread(new ThreadStart(RunThread));
T.IsBackground = true;
T.Start();
}

void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up: UTank.ChangeDirection(Direction.UP); UTank.Move(); break;
case Keys.Down: UTank.ChangeDirection(Direction.DOWN); UTank.Move(); break;
case Keys.Left: UTank.ChangeDirection(Direction.LEFT); UTank.Move(); break;
case Keys.Right: UTank.ChangeDirection(Direction.RIGHT); UTank.Move(); break;
case Keys.Space: Bullets.Add(new Bullet(Color.Black, UTank)); break; // 发射子弹
}
}

void RunThread()
{
try
{
int Start = Environment.TickCount;
Random R = new Random();
int KillCount = 0, DeathCount = 0;

while (true)
if (Environment.TickCount - Start > 100)
{
Bitmap CacheBmp = new Bitmap(OrgBmp);

for (int i = 0; i < Tanks.Count; i++)
{
Tanks[i].Move();
Tanks[i].Draw(ref CacheBmp);
if (R.Next(10) == 0) // 电脑发子弹是10分之一的可能
Bullets.Add(new Bullet(Color.Red, Tanks[i]));
}
UTank.Draw(ref CacheBmp);
List<Bullet> TempBullets = new List<Bullet>();
for (int i = 0; i < Bullets.Count; i++)
{
if (Bullets[i].ObjStep != -1)
{
Rectangle Test = new Rectangle(Bullets[i].Postion.X - 10, Bullets[i].Postion.Y - 10, 20, 20);
bool IsKilled = false;
for (int j = 0; j < Tanks.Count; j++)
if (Test.Contains(Tanks[j].Postion))
{
if (Bullets[i].Owner == UTank)
{
KillCount++;
IsKilled = true;
Tanks[j] = new Tank(Color.Blue, this.BackColor);
}
break;
}
if (!IsKilled)
if (Test.Contains(UTank.Postion))
if (Bullets[i].Owner != UTank)
{
DeathCount++;
IsKilled = true;
break;
}
if (!IsKilled)
TempBullets.Add(Bullets[i]);
}
}
Bullets = new List<Bullet>(TempBullets);
for (int i = 0; i < Bullets.Count; i++)
{
Bullets[i].Move();
Bullets[i].Draw(ref CacheBmp);
}
Monitor.Enter(CacheBmp);
using (Graphics G = Graphics.FromImage(CacheBmp))
{
G.DrawString("杀敌数: " + KillCount.ToString() + " 死亡数: " + DeathCount.ToString(), this.Font,
Brushes.Black, new PointF(0, 0));
}
Monitor.Exit(CacheBmp);
this.Invoke(new SetImage(DoSetImage), new Object[] { CacheBmp });
Start = Environment.TickCount;
}
}
catch
{
// 忽略程序退出异常
}
}
[/code]
...全文
11574 398 打赏 收藏 转发到动态 举报
写回复
用AI写文章
398 条回复
切换为时间正序
请发表友善的回复…
发表回复
a979475580 2012-10-15
  • 打赏
  • 举报
回复
好 顶起
wwb562883398 2012-07-26
  • 打赏
  • 举报
回复
不错 学习了 很强大的源码
SpecialName8 2011-12-30
  • 打赏
  • 举报
回复
WindowsApplication87

这个87亮瞎了
my328420969 2011-12-06
  • 打赏
  • 举报
回复
源文件打包求。。328420969@qq.com
ananonon 2011-11-20
  • 打赏
  • 举报
回复
坦克大战,刚学。不懂,动手做做!
liuxiang1471 2011-09-26
  • 打赏
  • 举报
回复
为什么我运行的有问题啊
错误 1 “坦克大战.Form1”不包含“Form1_Load”的定义,并且找不到可接受类型为“坦克大战.Form1”的第一个参数的扩展方法“Form1_Load”(是否缺少 using 指令或程序集引用?) d:\My Documents\Visual Studio 2008\Projects\坦克大战\坦克大战\Form1.Designer.cs 40 55 坦克大战
Mr_Liu_ 2011-09-06
  • 打赏
  • 举报
回复
UP 学习了
Indifferent_Wind 2011-09-06
  • 打赏
  • 举报
回复
顶啊
回家我也去研究下。。!
星小野 2011-09-02
  • 打赏
  • 举报
回复
学习先[Quote=引用 3 楼 zgke 的回复:]
顶了...
[/Quote]
liruo000 2011-08-31
  • 打赏
  • 举报
回复
不错,很NB啊
一把秋刀鱼 2011-08-29
  • 打赏
  • 举报
回复
我也在学。。。。。
horizonjhlee147 2011-06-28
  • 打赏
  • 举报
回复
好厉害
fenghua8133 2011-06-24
  • 打赏
  • 举报
回复
学习了,牛人
wleexi 2011-06-23
  • 打赏
  • 举报
回复
强大了。。
jiawenbo89 2011-06-20
  • 打赏
  • 举报
回复
下了看看 呵呵 作业 参考下啊
realmaker24 2011-05-12
  • 打赏
  • 举报
回复
赞 写的很好
cashingcashing 2011-05-12
  • 打赏
  • 举报
回复
太牛了 要我写都不知道多少行
kunx609 2010-12-13
  • 打赏
  • 举报
回复
收下了,慢慢学习;
strompanda 2010-12-09
  • 打赏
  • 举报
回复
楼主啊 我的目标啊
wangqi0324 2010-10-24
  • 打赏
  • 举报
回复
顶个先,下下来看看
加载更多回复(378)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧