我写了一个小代码,比如 Flappy Bird(游戏)中的一个,在我的一个计时器中,我写了以下代码,但是当我启动这个计时器时,它只是向我展示了一个上下管道 3 次,然后绘画就开始了黑色,它不再显示管道。如果各位大神能告诉我问题出在哪里,感激不尽。。
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;
}
}
这是声明:
int[] Pipe1 = { 0, 0, 0, 0 };
int[] Pipe2 = { 0, 0, 0, 0 };
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;
这是加载表单部分:
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();