生平第一个C#程序,处男作,学习深入点再弄完善
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 猎狗追兔
{
public partial class Form1 : Form
{
private Coord origin = new Coord { X = 400, Y = 20 }; // 坐标原点位置
private const double tininess = 0.00001; // 微小步进量
private Animal rabbit = new Animal { Name = "兔子", Speed = 5.00, X = -200.00, Y = 0.00 };
private Animal hounter = new Animal { Name = "猎狗", Speed = -15.00, X = -200.00, Y = 400.00 };
public Form1()
{
InitializeComponent();
Text = "猎狗追兔";
MaximizeBox = false;
}
// 兔子定位
private void SetRabbit(double x, double y)
{
Rabbit.Top = Convert.ToInt32(origin.Y) + Convert.ToInt32(y) - Rabbit.Height / 2;
Rabbit.Left = Convert.ToInt32(origin.X) + Convert.ToInt32(x) - Rabbit.Width / 2;
}
// 猎狗定位
private void SetHounter(double x, double y)
{
Hounter.Top = Convert.ToInt32(origin.Y) + Convert.ToInt32(y) - Hounter.Height / 2;
Hounter.Left = Convert.ToInt32(origin.X) + Convert.ToInt32(x) - Hounter.Width / 2;
}
private void Arrange()
{
Home.Text = "兔子窝";
Home.AutoSize = false;
Home.TextAlign = ContentAlignment.MiddleCenter;
Home.Width = 50;
Home.Height = 30;
Home.BorderStyle = BorderStyle.FixedSingle;
Home.Top = Convert.ToInt32(origin.Y) - Home.Height / 2;
Home.Left = Convert.ToInt32(origin.X) - Home.Width / 2;
Hounter.Text = hounter.Name;
SetHounter(hounter.X, hounter.Y);
label1.Text = "猎狗速率:";
label1.Top = this.Height - label1.Height - 50;
label1.Left = 20;
textBox1.Text = Convert.ToString(hounter.Speed);
textBox1.Height = label1.Height;
textBox1.Width = 40;
textBox1.Top = label1.Top;
textBox1.Left = label1.Left + label1.Width;
Rabbit.Text = rabbit.Name;
SetRabbit(rabbit.X, rabbit.Y);
label2.Text = "兔子速率:";
label2.Top = label1.Top - label2.Height - 10;
label2.Left = label1.Left;
textBox2.Text = Convert.ToString(rabbit.Speed);
textBox2.Height = label2.Height;
textBox2.Width = 40;
textBox2.Top = label2.Top;
textBox2.Left = label2.Left + label2.Width;
Reset.Text = "重置";
Reset.Top = this.Height - Reset.Height - 50;
Reset.Left = this.Width - Reset.Width - 50;
StartRun.Text = "开始";
StartRun.Top = Reset.Top;
StartRun.Left = Reset.Left - StartRun.Width;
}
private void Form1_Shown(object sender, EventArgs e)
{
Arrange();
}
private void StartRun_Click(object sender, EventArgs e)
{
rabbit.Speed = Convert.ToDouble(textBox2.Text);
hounter.Speed = Convert.ToDouble(textBox1.Text);
Run();
}
private void Run()
{
double angle; // 猎狗速度方向角
Boolean success = false; // 是否抓到兔子
while ((rabbit.X < 0.00) && !success)
{
angle = Math.Atan((rabbit.X - hounter.X) / (hounter.Y - rabbit.Y));
rabbit.X += rabbit.Speed * tininess;
SetRabbit(rabbit.X, rabbit.Y);
hounter.X -= hounter.Speed * Math.Sin(angle) * tininess;
hounter.Y += hounter.Speed * Math.Cos(angle) * tininess;
SetHounter(hounter.X, hounter.Y);
if (hounter.Y <= 0.0)
{
success = true;
}
}
if (success)
{
MessageBox.Show("逮住兔子了!");
}
else
{
MessageBox.Show("兔子逃脱了!");
}
}
private void Reset_Click(object sender, EventArgs e)
{
rabbit.X = -200.00;
rabbit.Y = 0.00;
hounter.X = -200.00;
hounter.Y = 400.00;
Arrange();
}
}
public class Coord : Object
{
private double x;
private double y;
public double X
{
get { return this.x; }
set { this.x = value; }
}
public double Y
{
get { return this.y; }
set { this.y = value; }
}
}
// 动物类
public class Animal : Object
{
private String name;
private double x;
private double y;
private double speed;
public String Name
{
get { return this.name; }
set { this.name = value; }
}
public double X
{
get { return this.x; }
set { this.x = value; }
}
public double Y
{
get { return this.y; }
set { this.y = value; }
}
public double Speed
{
get { return this.speed; }
set { this.speed = value; }
}
}
}
[
本帖最后由 TonyDeng 于 2011-10-4 03:35 编辑 ]