使用 GDI 绘制到我的 ClientRectangle 时出现问题+ & C#

发布于 2024-08-10 10:38:53 字数 1998 浏览 5 评论 0原文

我正在努力了解碰撞检测,并决定创建一个模拟 Pacman 的程序。简而言之,当吃豆人遇到幽灵时,它就会死亡。

我指定了我想要的 Pacmen 数量,并在 MainForm_Paint(当我的控件需要绘制时调用)中创建了一个由我创建的名为 Pacman 的类指定数量的数组。然后,我继续使用指定的参数构造数组中的每个 Pacman。

当一切都说完并完成后,我会继续尝试在循环内绘制所有吃豆人。无论我指定绘制多少个,都只在 ClientRectangle 内绘制一个。当我展开 ClientRectangle 时,我看到了一些小线条或看起来像是吃豆人的一小条东西。我已检查以确保起始位置位于 ClientRectangles x,y 系统内,而且确实如此。我只是很困惑为什么他们不在 ClientRectangle 内绘制。 :( 任何帮助将不胜感激。

这是我为 MainForm_Paint 编写的代码

private void MainForm_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;           
        Rectangle rect = this.ClientRectangle;
        Pacman[] pacmen = new Pacman[parameters[0]];            
        int whichPacman = 0;  

        for (int i = 0; i < parameters[0]; i++)
        {
            pacmen[i] = new Pacman(rect, parameters[1]);                
        }

        while (play)
        {
            pacmen[whichPacman].PacmanDraw(g, rect);
            // Must reset whichPac if whichPac + 1 == parameters[0]
            // Break out while testing
            if ((whichPacman + 1) == parameters[0])
            {
                break;
            }
            else
            {
                whichPacman++;
            }
        }
    }

这是我为 Pacman 类编写的代码:

    public Pacman(Rectangle rect, int radius)
    {
        Random rand = new Random();                    
        this.xpos = rand.Next(rect.Left, rect.Left + rect.Width);
        this.ypos = rand.Next(rect.Top, rect.Top + rect.Height);
        this.radius = radius;            
    }

    public void PacmanDraw(Graphics g, Rectangle rect)
    {                         
        GraphicsPath path = new GraphicsPath();          
        path.AddArc(this.xpos, this.ypos, (float) this.radius * 2, (float) this.radius * 2, 0, 360);            
        path.CloseFigure();
        g.FillPath(new SolidBrush(Color.AliceBlue), path);
        path.Dispose();
    }

感谢您的帮助,如果这是对 SOF 的不当使用,我们深表歉意。

I am working on understanding collision detection and have decided to create a program that simulates Pacman. Simply put when a Pacman runs into a Ghost it dies.

I specify how many Pacmen I would like and in my MainForm_Paint (called when my control needs painting) I create an array of the specified amount of a class I created called Pacman. I then go on to construct each Pacman in the array with parameters specified.

When all is said and done I then go on to just try and draw all the Pacmen while inside a loop. Only one drawn inside the ClientRectangle regardless of how many I specify to draw. When I expand my ClientRectangle I see little lines or what appears to be a sliver of a Pacman. I have checked to make sure that the start positions are within the ClientRectangles x,y system and they are. I am just stumped as to why they aren't drawing inside the ClientRectangle though. :( Any help would be much appreciated.

Here is what I have coded for MainForm_Paint

private void MainForm_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;           
        Rectangle rect = this.ClientRectangle;
        Pacman[] pacmen = new Pacman[parameters[0]];            
        int whichPacman = 0;  

        for (int i = 0; i < parameters[0]; i++)
        {
            pacmen[i] = new Pacman(rect, parameters[1]);                
        }

        while (play)
        {
            pacmen[whichPacman].PacmanDraw(g, rect);
            // Must reset whichPac if whichPac + 1 == parameters[0]
            // Break out while testing
            if ((whichPacman + 1) == parameters[0])
            {
                break;
            }
            else
            {
                whichPacman++;
            }
        }
    }

Here is what I have coded for my Pacman class:

    public Pacman(Rectangle rect, int radius)
    {
        Random rand = new Random();                    
        this.xpos = rand.Next(rect.Left, rect.Left + rect.Width);
        this.ypos = rand.Next(rect.Top, rect.Top + rect.Height);
        this.radius = radius;            
    }

    public void PacmanDraw(Graphics g, Rectangle rect)
    {                         
        GraphicsPath path = new GraphicsPath();          
        path.AddArc(this.xpos, this.ypos, (float) this.radius * 2, (float) this.radius * 2, 0, 360);            
        path.CloseFigure();
        g.FillPath(new SolidBrush(Color.AliceBlue), path);
        path.Dispose();
    }

Thanks for you help and sorry if this is an inappropriate use of SOF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失退 2024-08-17 10:38:53

每次调用 Pacman 构造函数时,您都会为随机数生成器创建一个新种子。当您在紧密循环中执行此操作时,所有吃豆人最终都会出现在同一个地方。这是因为 Random 使用日期/时间作为其种子的一部分。

随机数生成从种子值开始。如果重复使用相同的种子,则会生成相同的数字序列。生成不同序列的一种方法是使种子值与时间相关,从而为每个新的 Random 实例生成不同的序列。默认情况下,Random 类的无参数构造函数使用系统时钟生成其种子值,而其参数化构造函数可以根据当前时间的刻度数获取 Int32 值。但是,由于时钟的分辨率有限,因此使用无参数构造函数连续创建不同的 Random 对象会创建产生相同随机数序列的随机数生成器。下面的示例说明了两个连续实例化的 Random 对象生成一系列相同的随机数。

在循环外创建随机数生成器并将其传递到构造函数中。

You're creating a new seed for your random number generator every time you call the Pacman constructor. As you are doing this in a tight loop all the Pacmen will end up in the same place. This is because Random uses the date/time as part of it's seed.

The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers. The following example illustrates that two Random objects that are instantiated in close succession generate an identical series of random numbers.

Create the random number generator outside the loop and pass it into the constructor.

天涯离梦残月幽梦 2024-08-17 10:38:53

您需要考虑到您正在指定对象的左上角这一事实;您的 xposypos 只能达到 width - 对象宽度 的最大值,以保持其完全可见。

this.xpos = rand.Next(rect.Left, rect.Left + rect.Width - radius * 2.0);
this.ypos = rand.Next(rect.Top, rect.Top + rect.Height - radius * 2.0);

You need to account for the fact that you're specifying the top left of the object; your xpos and ypos can only go to a maximum of width - object width in order to keep it fully visible.

this.xpos = rand.Next(rect.Left, rect.Left + rect.Width - radius * 2.0);
this.ypos = rand.Next(rect.Top, rect.Top + rect.Height - radius * 2.0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文