释放钥匙时的延迟

时间:2019-01-18 04:34:55

标签: c# console-application sprite

我正在尝试使用C#中的控制台应用程序使用精灵创建赛车游戏。 Sprites系统似乎运行良好,支持图层并允许Sprite在屏幕上移动。

对于动画精灵(总共有4个,用于草的两个帧和用于道路的两个帧),草的第1帧和第2帧之间的初始时间(由变量sprAnimSpeed定义)为300毫秒。道路精灵。

我试图导致在键盘上按下一个键(在这种情况下为向上箭头)时,sprAnimSpeed的值(最初为300)将减少10,从而使精灵帧之间的时间更短,至少在理论上给人以更快的感觉。 当您快速按下该键时效果很好,但是当按下该键超过2秒时,sprAnimSpeed继续减小,并且几秒钟后该值稳定下来,释放键和该值之间存在巨大的延迟稳定变量的值。

我希望获得帮助,以查看是否有解决此问题的方法。 Printscreen here.

var handler = GetConsoleHandle();
            using (var graphics = Graphics.FromHwnd(handler))
            using (var sprSky = Properties.Resources.sky) // Set sky sprite (first layer);
            using (var sprBackgroundClouds = Properties.Resources.background_clouds) 
            using (var sprMountains = Properties.Resources.mountains)
            using (var sprCameraClouds = Properties.Resources.camera_clouds)
            using (var sprGrass1 = Properties.Resources.grass_1) // Set the frame 1 sprite of grass;
            using (var sprRoad1 = Properties.Resources.road_1) // Set the frame 1 sprite of road;
            using (var sprGrass2 = Properties.Resources.grass_2) // Set the frame 2 sprite of grass;
            using (var sprRoad2 = Properties.Resources.road_2) // Set the frame 2 sprite of road;
                while (true)
                {
                    Console.Title = Convert.ToString(sprAnimSpeed); // Show the delay time (in ms) between the frames of an animated sprite in the console title;
                    Cls(); // Clear console;
                    graphics.Clear(Color.Transparent); // Clear sprites;

                    xMountains = xMountains + 5; // x position of mountains sprite; for parallax effect;
                    xBackgroundClouds = xBackgroundClouds - 5; // x position of background clouds sprite; for parallax effect;
                    xCameraClouds = xCameraClouds - 2; // x position of camera clouds; for parallax effect;

                    // Draw/redaw the sprites:

                    graphics.DrawImage(sprSky, 0, 0, 800, 500); // (sprite, x, y, w, h);
                    graphics.DrawImage(sprBackgroundClouds, xBackgroundClouds, yBackgroundClouds, 800, 500);
                    graphics.DrawImage(sprMountains, xMountains, yMountains, 800, 500);
                    graphics.DrawImage(sprCameraClouds, xCameraClouds, yCameraClouds, 800, 500);

                    graphics.DrawImage(sprGrass1, 0, 0, 800, 500); // Frame 1;
                    graphics.DrawImage(sprRoad1, xRoad, yRoad, 800, 500); // Frame 1;
                    Delay(sprAnimSpeed); // Delay time between the last and the next frames;
                    graphics.DrawImage(sprGrass2, 0, 0, 800, 500); // Frame 2;
                    graphics.DrawImage(sprRoad2, xRoad, yRoad, 800, 500); // Frame 2
                    Delay(sprAnimSpeed); // Wait for redraw the sprites in new x positions;

                    ConsoleKeyInfo control = Console.ReadKey(true); // When the up arrow is pressed;
                    if (control.Key == ConsoleKey.UpArrow && sprAnimSpeed > 60) // Values for sprAnimSpeed less than 60 ms make it difficult to view due to flickers on the screen;
                    {
                        sprAnimSpeed = sprAnimSpeed - 10;
                    }

                }

0 个答案:

没有答案