想写个赛跑的应有程序,但是就是调不通。
程序代码:/*Dogrun.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Day_of_race
{
public class Dogrun
{
public int StartPosition;
public int RacetrackLength;
public PictureBox MypictureBox;
public int Location = 0;
public Random Randomizer;
public bool RUN()
{
Point startLocation = MypictureBox.Location;
Random Randomizer = new Random();
Location = Randomizer.Next(0, 10); // 每次移动的像素大小
RacetrackLength = 800; // 像素总大小
for (int i = StartPosition; i <= RacetrackLength; i += Location)
{
MypictureBox.Location = new Point(MypictureBox.Location.X + i, MypictureBox.Location.Y/* + i*/);
}
if (MypictureBox.Location.X >= RacetrackLength)
{
MessageBox.Show(MypictureBox.Name + "end the race");
MypictureBox.Location = startLocation;
}
return true;
}
/*
* 添加构造函数,实例化所有对象
*/
public Dogrun(PictureBox pb)
{
if (pb != null)
{
this.MypictureBox = pb;
}
else
{
throw new System.ArgumentNullException( "PictureBox");
}
}
}
}
/*From*/
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Day_of_race;
namespace Day_of_race
{
public partial class Form1 : Form
{
Dogrun Dog;
public Form1()
{
InitializeComponent();
Dog = new Dogrun( this.pictureBox2);
Dog.StartPosition = pictureBox2.Location.X;
Dog.RacetrackLength = pictureBox1.Location.Y;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Dog.RUN();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}