Beispiel #1
0
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var fps = 1000f / game.GameTime.ElapsedMilliseconds;

            await context.SetFillStyleAsync("green");

            await context.FillRectAsync(10, 50, 300, _height);

            await context.SetFillStyleAsync("#fff");

            await context.SetFontAsync("18px verdana");

            y = startY;

            await WriteLine($"Total game time (s): {game.GameTime.TotalMilliseconds / 1000}", context);
            await WriteLine($"Frame time (ms): {game.GameTime.ElapsedMilliseconds}", context);
            await WriteLine($"FPS: {fps:###}", context);

            if (AsteroidsSpawner is not null)
            {
                await WriteLine($"Asteroids alive: {AsteroidsSpawner.Alive:###}", context);
            }

            if (BulletSpawner is not null)
            {
                await WriteLine($"Bullets spawned: {BulletSpawner.Alive:###}", context);
            }
        }
        /// <summary>
        /// 画小时数字
        /// </summary>
        /// <param name="radius">半径</param>
        /// <returns></returns>
        private async Task DrawHourNumberAsync(double radius)
        {
            await _context.SaveAsync();

            //await _context.TranslateAsync(radius, radius);
            await _context.BeginPathAsync();

            await _context.SetFontAsync("18px Arial");

            await _context.SetFillStyleAsync("#000");

            await _context.SetTextAlignAsync(TextAlign.Center);

            await _context.SetTextBaselineAsync(TextBaseline.Middle);

            List <Task> tasks = new List <Task>();

            for (int i = 1; i <= 12; i++)
            {
                tasks.Add(DrawOneHourNumberAsync(i, radius));
            }
            await Task.WhenAll(tasks);

            await _context.RestoreAsync();
        }
        private async Task GameOver()
        {
            await _context.SetFontAsync("48px serif");

            await _context.SetTextAlignAsync(TextAlign.Center);

            await _context.FillTextAsync("GAME OVER", _gameWidth / 2, _gameHeight / 2 - 48);
        }
Beispiel #4
0
        public async ValueTask DrawText(string text,
                                        Point pos,
                                        Color color,
                                        CanvasTextFormat canvasTextFormat)
        {
            await _canvas2DContext.SetFontAsync(canvasTextFormat.FormattedString);

            await DrawText(text, pos, color);
        }
        private async Task DrawText(string text, Position position, int maxWidth, FontInfo?fontInfo)
        {
            var oldFont = _context.Font;

            if (fontInfo != null)
            {
                await _context.SetFontAsync(fontInfo.FontString);
            }

            text = await _nodeTextFormatter.ClampText(_context, text, maxWidth);

            await _context.FillTextAsync(text, position.X, position.Y);

            if (fontInfo != null)
            {
                await _context.SetFontAsync(oldFont);
            }
        }
Beispiel #6
0
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var y = (double)game.Display.Size.Height / 2;
            var x = (double)game.Display.Size.Width / 2;

            await context.SetFillStyleAsync("#fff").ConfigureAwait(false);

            await context.SetFontAsync("40px verdana").ConfigureAwait(false);

            await context.SetTextAlignAsync(TextAlign.Center).ConfigureAwait(false);

            await context.FillTextAsync("Blazeroids!", x, y).ConfigureAwait(false);

            await context.SetFontAsync("28px verdana").ConfigureAwait(false);

            await context.FillTextAsync("press Enter to start", x, y + 40)
            .ConfigureAwait(false);
        }
Beispiel #7
0
        public async ValueTask DrawText(int left, int top, string text, string font, string color = "", string fillColor = "")
        {
            await _canvas.SetFontAsync(font);

            await _canvas.SetStrokeStyleAsync(color);

            await _canvas.SetFillStyleAsync(fillColor);

            await _canvas.StrokeTextAsync(text, left, top);
        }
Beispiel #8
0
        private static async Task RenderText(Canvas2DContext context, string text, int x)
        {
            await context.SetFillStyleAsync("#fff");

            await context.SetFontAsync("18px verdana");

            await context.SetTextBaselineAsync(TextBaseline.Middle);

            await context.FillTextAsync(text, x, 0);
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            await context.SetFillStyleAsync("#fff").ConfigureAwait(false);

            await context.SetFontAsync("18px verdana").ConfigureAwait(false);

            var hiScore  = Math.Max(_score, _maxScore);
            var text     = $"Score: {_score:###} Hi Score: {hiScore:###}";
            var textSize = await context.MeasureTextAsync(text).ConfigureAwait(false);

            var x = game.Display.Size.Width - textSize.Width - 50;

            await context.FillTextAsync(text, x, 50).ConfigureAwait(false);
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var fps = 1000f / game.GameTime.ElapsedMilliseconds;

            await context.SetFillStyleAsync("green");

            await context.FillRectAsync(10, 50, 400, 100);

            await context.SetFontAsync("24px verdana");

            await context.StrokeTextAsync($"Total game time (s): {game.GameTime.TotalMilliseconds / 1000}", 20, 80);

            await context.StrokeTextAsync($"Frame time (ms): {game.GameTime.ElapsedMilliseconds}", 20, 110);

            await context.StrokeTextAsync($"FPS: {fps:###}", 20, 140);
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var fps = 1000f / game.GameTime.ElapsedMilliseconds;

            await context.SetFillStyleAsync("green");

            await context.FillRectAsync(10, 50, 300, _height);

            await context.SetFillStyleAsync("#fff");

            await context.SetFontAsync("18px verdana");

            y = startY;

            await WriteLine($"Total game time (s): {game.GameTime.TotalMilliseconds / 1000}", context);
            await WriteLine($"Frame time (ms): {game.GameTime.ElapsedMilliseconds}", context);
            await WriteLine($"FPS: {fps:###}", context);
        }
Beispiel #12
0
        public async ValueTask Render()
        {
            await _context.ClearRectAsync(0, 0, _width, _height);

            await _context.BeginBatchAsync();

            await _context.SetFillStyleAsync("white");

            await _context.SetFontAsync("32px consolas");

            var text   = $"Points: {_points.ToString("D3")} High score: {HighScore} {HighScoreName} Lives: {_lives.ToString("D3")}";
            var length = await _context.MeasureTextAsync(text);

            await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 25);

            if (GameOver)
            {
                await RenderGameOver();
            }
            else if (Won)
            {
                await RenderWonGame();
            }
            else if (Started)
            {
                await RenderGameFrame();
            }
            else if (!Started)
            {
                await _context.SetFillStyleAsync("white");

                text   = $"Use arrow keys to move, space to fire";
                length = await _context.MeasureTextAsync(text);

                await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 150);

                text   = $"Press space to start";
                length = await _context.MeasureTextAsync(text);

                await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 200);
            }

            await _context.EndBatchAsync();
        }
        async ValueTask drawPage(Page p)
        {
            await _outputCanvasContext.ClearRectAsync(0, 0, _canvasReference.Width, _canvasReference.Height);

            await _outputCanvasContext.SetFillStyleAsync("black");

            await _outputCanvasContext.FillRectAsync(0, 0, _canvasReference.Width, _canvasReference.Height);

            for (int i = 0; i < p.elements.Count; i++)
            {
                PageElement e = p.elements[i];
                await _outputCanvasContext.SetFontAsync(fontCode(e.font));

                await _outputCanvasContext.SetFillStyleAsync("#ffffff");

                await _outputCanvasContext.FillTextAsync(e.text, e.x *skala, e.y *skala);
            }
            await Task.Delay(p.time * 1000);
        }
Beispiel #14
0
 private async Task SetFontFormat(Font font)
 {
     var fontString = GetFontString(font);
     await ctx.SetFontAsync(fontString);
 }