Simple game in C # with only native libraries

I could find a set of tutorials on java 2D games and tutorials on android games that use only integrated graphics libraries .
I am looking for something similar in C # (without DirectX or XNA)
I found this skeleton of the game loop , but it does not say how to render graphics.

The goal is to simulate a simple electronic device.
I need to show some graphical results as the user quickly presses the keys on the keyboard. It sounds like an arcade game.

For example, when the user presses one of the arrow keys, the pointer (image) will move accordingly.

I think I cannot do this with a typical Windows Forms application, can I?
For example, use the PictureBox control and move it to the KeyPress Form event.

+5
source share
3 answers

Here's a simple game using WinForms and Timer , using Graphics for draw (encapsulates GDI +).

It adds a timer that ticks every 10 milliseconds. Each tick it executes the logic of the game, and then refers to the bitmap image off the screen. This is in contrast to using a continuous loop, as in the example in the link.

The form processes key events separately (unlike the execution of GetKeyState )

When you resize the form and during the first load, a bitmap image of the return buffer of the desired size is created.


Create a new form and replace all the code below. Control the ball using the arrow keys. There is no concept of death.

 using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsGame { public partial class Form1 : Form { Bitmap Backbuffer; const int BallAxisSpeed = 2; Point BallPos = new Point(30, 30); Point BallSpeed = new Point(BallAxisSpeed, BallAxisSpeed); const int BallSize = 50; public Form1() { InitializeComponent(); this.SetStyle( ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); Timer GameTimer = new Timer(); GameTimer.Interval = 10; GameTimer.Tick += new EventHandler(GameTimer_Tick); GameTimer.Start(); this.ResizeEnd += new EventHandler(Form1_CreateBackBuffer); this.Load += new EventHandler(Form1_CreateBackBuffer); this.Paint += new PaintEventHandler(Form1_Paint); this.KeyDown += new KeyEventHandler(Form1_KeyDown); } void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) BallSpeed.X = -BallAxisSpeed; else if (e.KeyCode == Keys.Right) BallSpeed.X = BallAxisSpeed; else if (e.KeyCode == Keys.Up) BallSpeed.Y = -BallAxisSpeed; // Y axis is downwards so -ve is up. else if (e.KeyCode == Keys.Down) BallSpeed.Y = BallAxisSpeed; } void Form1_Paint(object sender, PaintEventArgs e) { if (Backbuffer != null) { e.Graphics.DrawImageUnscaled(Backbuffer, Point.Empty); } } void Form1_CreateBackBuffer(object sender, EventArgs e) { if (Backbuffer != null) Backbuffer.Dispose(); Backbuffer = new Bitmap(ClientSize.Width, ClientSize.Height); } void Draw() { if (Backbuffer != null) { using (var g = Graphics.FromImage(Backbuffer)) { g.Clear(Color.White); g.FillEllipse(Brushes.Black, BallPos.X - BallSize / 2, BallPos.Y - BallSize / 2, BallSize, BallSize); } Invalidate(); } } void GameTimer_Tick(object sender, EventArgs e) { BallPos.X += BallSpeed.X; BallPos.Y += BallSpeed.Y; Draw(); // TODO: Add the notion of dying (disable the timer and show a message box or something) } } } 
+15
source

If you want to create dynamic drawings, you can use WPF Canvas for these purposes. It does not support games, etc., but it is an easy way to draw primitive shapes and images, as it would on a website.

+1
source

Native rendering environment for .NET winforms GDI + applications. There are many online tutorials for drawing simple shapes / graphics using GDI +

+1
source

All Articles