首页
社区
课程
招聘
[旧帖] [原创]简易的对战类游戏C#代码,包括画图发射等(申请邀请码) 0.00雪花
发表于: 2010-4-1 15:12 1228

[旧帖] [原创]简易的对战类游戏C#代码,包括画图发射等(申请邀请码) 0.00雪花

2010-4-1 15:12
1228
不到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
{
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
{
// 忽略程序退出异常
}
}

[课程]Android-CTF解题方法汇总!

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 55
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
试试好不好玩!
2010-4-1 16:44
0
雪    币: 55
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
代码不全啊!缺几个自定义的类!
2010-4-2 09:40
0
游客
登录 | 注册 方可回帖
返回
//