C#绘制随机矩形
我写了一个小代码,比如《Flappy Bird》(游戏)中的一个,在我的一个计时器中,我写了下面的代码,但当我启动这个计时器时,它只是向上和向下显示了3次管道,然后画面变黑,不再显示管道。如果你们能告诉我问题出在哪里,那就太感谢了C#绘制随机矩形,c#,timer,drawing,flappy-bird-clone,C#,Timer,Drawing,Flappy Bird Clone,我写了一个小代码,比如《Flappy Bird》(游戏)中的一个,在我的一个计时器中,我写了下面的代码,但当我启动这个计时器时,它只是向上和向下显示了3次管道,然后画面变黑,不再显示管道。如果你们能告诉我问题出在哪里,那就太感谢了 private void timer2_Tick(object sender, EventArgs e) { if (Pipe1[0] + PipeWidth <= 0 | start == true) {
private void timer2_Tick(object sender, EventArgs e)
{
if (Pipe1[0] + PipeWidth <= 0 | start == true)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
int[] p1 = { px, py, p2x, p2y };
Pipe1 = p1;
}
else
{
Pipe1[0] = Pipe1[0] - 2;
Pipe1[2] = Pipe1[2] - 2;
}
if (Pipe2[0] + PipeWidth <= 0)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
int[] p1 = { px, py, p2x, p2y };
Pipe1 = p1;
}
else
{
Pipe2[0] = Pipe2[0] - 2;
Pipe2[2] = Pipe2[2] - 2;
}
if (start == true)
{
start = false;
}
}
以下是荷载形式部分:
Random rnd = new Random();
int py = rnd.Next(40, (this.Height - PipeDifferentY));
int py2 = py + PipeDifferentY;
int[] p1 = { this.Width, py, this.Width, py2 };
Pipe1 = p1;
py = rnd.Next(40, (this.Height - PipeDifferentY));
py2 = py + PipeDifferentY;
int[] p2 = { this.Width + PipeDifferentX, py, this.Width + PipeDifferentX, py2 };
Pipe2 = p2;
以下是绘画部分:
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[0], 0, PipeWidth, Pipe1[1]));
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[2], Pipe1[3], PipeWidth, this.Height - Pipe1[3]));
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[0], 0, PipeWidth, Pipe2[1]));
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[2], Pipe2[3], PipeWidth, this.Height - Pipe2[3]));
第一个计时器刚刚:
this.Invalidate();
在
Pipe2
if语句的底部有一个杂散的Pipe1
更新:
尝试更改为:
List<int> Pipe1 = new List<int>();
List<int> Pipe2 = new List<int>();
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;
绘制部分应该正常这可能与您的问题无关,但您应该将
随机
对象作为类中的字段,并在构造函数中创建它。不要在计时器处理程序中创建它。否则,如果计时器滴答声间隔很短,它可能会继续产生相同的值。No仍在这样做..,你能告诉我如何使用代码吗?仅此代码很难看出有什么问题。还有一件不相关的事情:你似乎在使用dynamic
而你应该使用var
。但是它一直在给我同样的'error',var'tooYes,这是不相关的,但是你不应该使用dynamic
而不是var
。是的,因为我给了pipe1,它是一个int数组,来自p1的新值,因为我不能给它更容易。有什么帮助解决这个问题吗?我在Begging有这个:int[]Pipe1={0,0,0,0};我用这些值生成int[]p1,因为我不能直接将它放入Pipe1,你能告诉我如何直接将值放入int[]Pipe1吗?你可以使用通用列表而不是数组来直接动态添加值:将Pipe1列为新列表代码>
List<int> Pipe1 = new List<int>();
List<int> Pipe2 = new List<int>();
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;
private void timer2_Tick(object sender, EventArgs e)
{
if (Pipe1[0] + PipeWidth <= 0 | start == true)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
Pipe1.Clear();
Pipe1.Add(px);
Pipe1.Add(py);
Pipe1.Add(p2x);
Pipe1.Add(p2y);
}
else
{
Pipe1[0] = Pipe1[0] - 2;
Pipe1[2] = Pipe1[2] - 2;
}
if (Pipe2[0] + PipeWidth <= 0)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
int[] p1 = { px, py, p2x, p2y };
Pipe2.Clear();
Pipe2.Add(px);
Pipe2.Add(py);
Pipe2.Add(p2x);
Pipe2.Add(p2y);
}
else
{
Pipe2[0] = Pipe2[0] - 2;
Pipe2[2] = Pipe2[2] - 2;
}
if (start == true)
{
start = false;
}
}
Random rnd = new Random();
int py = rnd.Next(40, (this.Height - PipeDifferentY));
int py2 = py + PipeDifferentY;
Pipe1.Clear();
Pipe1.Add(this.Width);
Pipe1.Add(py);
Pipe1.Add(this.Width);
Pipe1.Add(p2y);
py = rnd.Next(40, (this.Height - PipeDifferentY));
py2 = py + PipeDifferentY;
Pipe2.Clear();
Pipe2.Add(this.Width + PipeDifferentX);
Pipe2.Add(py);
Pipe2.Add(this.Width + PipeDifferentX);
Pipe2.Add(p2y);