使用 GDI 绘制到我的 ClientRectangle 时出现问题+ & C#
我正在努力了解碰撞检测,并决定创建一个模拟 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次调用
Pacman
构造函数时,您都会为随机数生成器创建一个新种子。当您在紧密循环中执行此操作时,所有吃豆人最终都会出现在同一个地方。这是因为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 becauseRandom
uses the date/time as part of it's seed.Create the random number generator outside the loop and pass it into the constructor.
您需要考虑到您正在指定对象的左上角这一事实;您的
xpos
和ypos
只能达到width - 对象宽度
的最大值,以保持其完全可见。You need to account for the fact that you're specifying the top left of the object; your
xpos
andypos
can only go to a maximum ofwidth - object width
in order to keep it fully visible.