SkiaSharp Gaming Sample: Interactive Starfield

Welcome to the SkiaSharp Gaming Sample section. This page showcases a dynamic and interactive starfield simulation built using SkiaSharp, a 2D graphics library for .NET. Explore the code and see how SkiaSharp can bring your game ideas to life with beautiful and performant graphics.

Live Demo

Interact with the starfield below. Click to add stars, and use the sliders to adjust the speed and density.

About SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET based on the Skia Graphics Engine. It provides a powerful and flexible way to draw text, shapes, and images with hardware acceleration. SkiaSharp is ideal for UI development, data visualization, and, as demonstrated here, game development.

Key features relevant to game development include:

Sample Code

Below is a simplified representation of the code used to create the interactive starfield. This example focuses on the core drawing and update logic.

import System; import System.Collections.Generic; import SkiaSharp; public class Star { public SKPoint Position { get; set; } public float Radius { get; set; } public SKColor Color { get; set; } public float Speed { get; set; } public Star(float x, float y, float radius, SKColor color, float speed) { Position = new SKPoint(x, y); Radius = radius; Color = color; Speed = speed; } public void Update(float canvasWidth, float canvasHeight) { Position = new SKPoint(Position.X, Position.Y + Speed); if (Position.Y > canvasHeight) { Position = new SKPoint(Random.Shared.NextSingle() * canvasWidth, -Radius); } } public void Draw(SKCanvas canvas) { using (var paint = new SKPaint { Color = Color, IsAntialias = true }) { canvas.DrawCircle(Position.X, Position.Y, Radius, paint); } } } public class StarfieldViewModel { private List stars = new List(); private SKSize canvasSize; private float currentSpeed = 5; private int currentDensity = 200; public void Initialize(int width, int height) { canvasSize = new SKSize(width, height); GenerateStars(); } private void GenerateStars() { stars.Clear(); int numStars = (int)(canvasSize.Width * canvasSize.Height / currentDensity); for (int i = 0; i < numStars; i++) { float x = Random.Shared.NextSingle() * canvasSize.Width; float y = Random.Shared.NextSingle() * canvasSize.Height; float radius = Random.Shared.NextSingle() * 2f + 0.5f; SKColor color = new SKColor(255, 255, 255, (byte)(Random.Shared.NextSingle() * 150 + 100)); // White with varying opacity float speed = Random.Shared.NextSingle() * currentSpeed + 1; stars.Add(new Star(x, y, radius, color, speed)); } } public void Update() { foreach (var star in stars) { star.Update(canvasSize.Width, canvasSize.Height); } } public void Draw(SKCanvas canvas) { canvas.Clear(SKColors.Black); // Black background foreach (var star in stars) { star.Draw(canvas); } } public void SetSpeed(float speed) { currentSpeed = speed; // Re-apply speed to existing stars or regenerate if needed for more complex effects foreach(var star in stars) { star.Speed = Random.Shared.NextSingle() * currentSpeed + 1; } } public void SetDensity(int density) { currentDensity = density; GenerateStars(); // Regenerate stars based on new density } public void AddStar(float x, float y) { float radius = Random.Shared.NextSingle() * 2f + 0.5f; SKColor color = new SKColor(255, 255, 255, (byte)(Random.Shared.NextSingle() * 150 + 100)); float speed = Random.Shared.NextSingle() * currentSpeed + 1; stars.Add(new Star(x, y, radius, color, speed)); } public void ResetStars() { GenerateStars(); } }

Next Steps

Ready to dive deeper? Explore the full source code for this sample. You can also find more resources on the SkiaSharp GitHub repository and the official SkiaSharp documentation.

Consider integrating SkiaSharp into your own .NET game projects to achieve stunning visual effects and smooth performance.