即使启用了双重缓冲,为什么我的屏幕仍然闪烁?

时间:2019-01-18 16:17:56

标签: c# .net graphics

好吧,我对C#图形还很陌生,我正在尝试制作自上而下的冒险游戏类型的东西。问题是背景闪烁上方显示的所有内容。一切都是来自png文件的位图。背景不闪烁,所以我不知道我要去哪里。 这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AITS
{
    public partial class Form1 : Form
    {
        Background background;
        Foreground foreground;
        Character player;
        Graphics g;

        public Form1()
        {
            InitializeComponent();
            Console.WriteLine(Environment.CurrentDirectory);
            background = new Background(Properties.Resources.background, Width, Height);
            foreground = new Foreground(100, Width);
            player = new Character();
            DoubleBuffered = true;


            Paint += DrawScreen;
            KeyDown += KeyPressed;
            Shown += Form1_Shown;

            g = CreateGraphics();

        }

        private void Form1_Shown(Object sender, EventArgs e)
        {
            gameLoop();
        }

        private void DrawScreen(object sender, PaintEventArgs args)
        {
                background.Draw(g);
                player.Draw(g);
                foreground.Update(Height, Width);
                foreground.Draw(g);
        }

        private void KeyPressed(object sender, KeyEventArgs e)
        {
            Console.WriteLine(e.KeyData.ToString());
        }

        public void gameLoop()
        {
            while (this.Created)
            {
                Invalidate();
                Refresh();
                Application.DoEvents();

            }   
        }
    }
}

编辑: 好吧,我找到了答案,对于找不到像我这样的人: g应该= args.Graphics。不要使用CreateGraphics()!

2 个答案:

答案 0 :(得分:1)

屏幕闪烁,因为在允许您在其上绘画之前,表单首先重绘了其背景。

您可以通过覆盖my_parser=argparse.ArgumentParser(description='To sync files between source and destination') my_parser.add_argument('-s',"--source", nargs=2, help='Enter the source server detail and source folder name')Q my_parser.add_argument('-d', '--destination', nargs=2, help="Enter the destination server detail") if len(sys.argv)==1: my_parser.print_help(sys.stderr) sys.exit(1) clarg = my_parser.parse_args() 来暂停此行为:

WndProc

答案 1 :(得分:1)

好吧,我找到了答案,对于找不到像我这样的人:g应该= args.Graphics。不要使用CreateGraphics()!