gpt4 book ai didi

c# - 狗以相同的速度前进

转载 作者:太空狗 更新时间:2023-10-29 23:23:01 25 4
gpt4 key购买 nike

<分区>

我正在从事 headfirst C# 的第一个实验室。我在我的程序中遇到了一个问题,即狗以相同的速度前进。我不明白他们为什么这样做,因为在我看来,每个对象实例都会随机添加到其 X 位置的 1 到 5 个像素。这种随机性应该会产生足够的影响。

因此,因为我不想发布我的实验 1 的整个类(class)集,所以我重新创建了一个小型的独立版本,其中只有两只狗在比赛,而且没有投注方面。

Form1.Designer 包含:- 两个画框,里面有一只猎犬。-一个开始按钮

Greyhound.cs 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace test
{
public class Greyhound
{
public int DogID;
public PictureBox myPictureBox;
public Point StartingPosition;
public Point CurrentPosition;
public Random Randomizer;

public bool Run()
{
int AddDistance = Randomizer.Next(1, 7);
CurrentPosition.X += AddDistance;
myPictureBox.Location = CurrentPosition;

if (CurrentPosition.X > 600)
{
return true;
}
else
{
return false;
}
}

public void ReturnToStart()
{
CurrentPosition = StartingPosition;
myPictureBox.Location = StartingPosition;
}
}
}

Form1.cs 类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
Greyhound One = new Greyhound() { DogID = 1, myPictureBox = pictureBox1, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox1.Location, Randomizer = new Random() };
Greyhound Two = new Greyhound() { DogID = 2, myPictureBox = pictureBox2, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox2.Location, Randomizer = new Random() };
if (One.Run())
{
timer1.Enabled = false;
MessageBox.Show("Dog One WON!");

}
else if (Two.Run())
{
timer1.Enabled = false;
MessageBox.Show("Dog Two WON!");
}


}

private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
}
}

有人能看出这种创建方式有什么问题吗?狗会以相同的速度而不是每次随机运行 1 到 5 个像素的原因。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com