0

我有 2 个图片对象。我希望他们两个都从右向左移动。如果一个人走出可见面板,我将其位置替换为起点。所以屏幕上总是有 2 张图片在移动。

如果我不使用计时器,则绘制 2 张图片。但是,如果我使用带有滴答事件的计时器来更新它们的位置以使它们移动,则只显示一张图片并且它一直在闪烁,滞后......

以下是我到目前为止的代码。我对 C# 不熟悉。感谢任何帮助。谢谢你。

定时器间隔 = 30;表格一:

public partial class Form1 : Form
{
    Background bg1 = new Background();
    Background bg2 = new Background(800);

    public Form1()
    {
        InitializeComponent();
    }

    private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
    {
        bg1.paint(e);
        bg2.paint(e);
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        bg1.updatePosition();
        bg2.updatePosition();

        this.Refresh();
    }
}

背景:

class Background
{
    int bg_width = 800;
    int bg_height = 500;

    Image bg;
    Rectangle wb;
    private static int x = 0;

    public Background()
    {
        bg = Properties.Resources.bg;
        wb = new Rectangle(x, 0, bg_width, bg_height);
    }

    public Background(int custom_x)
    {
        x = custom_x;
        bg = Properties.Resources.bg;
        wb = new Rectangle(x, 0, bg_width, bg_height);
    }

    public void paint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawImage(bg, wb);
    }

    public void updatePosition()
    {
        x--;
        if (x == -800)
        {
            x = 801;
        }
        wb.Location = new Point(x, 0);
    }
}
4

0 回答 0