I could find a set of java 2D game tutorials and android game tutorials which uses only native graphics libraries.
I'm looking for something similar in C# ( without DirectX or XNA)
I found this game loop skeleton , but it doesn't tell how to Render the Graphics.
Target is to simulate a simple Electronic device.
I need to show some graphical output as user "presses" some keys on the keyboard quickly.Hence it looks like an arcade game.
For example As the user presses one of the arrow keys a pointer(an image) will move accordingly.
I guess I can't do this with typical Windows Forms Application can I?
For example use a PictureBox
control and move it in KeyPress
event of the Form
.
-
There's no reason why you can't do this with WinForms, though it wouldn't necessarily be very quick. – ChrisF Oct 20 '11 at 10:48
-
2Winforms and a timer would be a very simple way of doing things. – George Duckett Oct 20 '11 at 10:54
-
Like Can I handle "5 key presses" per second?And would usage of PictureBox be ok? – Dinushan Oct 20 '11 at 10:55
-
Would OpenTK be native enough? – Marnix Oct 20 '11 at 10:56
-
1To render graphics you could use a picturebox. I'd create a Bitmap, and draw to that using Graphics.FromImage(backbuffer), then handle the Paint event of the picturebox and draw the backbuffer on. In the timer, invalidate the picturebox so it gets re-drawn once something changes. – George Duckett Oct 20 '11 at 10:56
-
1And what about the WPF Canvas? – Marnix Oct 20 '11 at 10:57
-
@Marnix thanks for WPF Canvas.I better use it – Dinushan Oct 20 '11 at 11:01
-
WinForms doesn't know anything about rendering - the technology you're looking for is GDI+ - I'm sure there are many many tutorials on google on how to draw using GDI+ :) – MattDavey Oct 20 '11 at 11:14
3 Answers
Here's a simple game using WinForms
and a Timer
, using Graphics
to draw (encapsulates GDI+).
It adds a timer that 'ticks' every 10 milliseconds. Each tick it performs game logic, then draws to an off screen bitmap. This is as opposed to using a continual loop as in the example in the link.
The form handles key events separately (as opposed to doing something like GetKeyState
)
When the form is resized, and when it first loads it'll create the backbuffer bitmap of the right size.
Create a new form and replace all code with below. Control the ball using arrow keys. There's no notion of dying.
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)
}
}
}

- 31,770
- 9
- 95
- 162
-
thanks.I was confused with the comments and wonder which way to go.And I was going to keep the project itself aside and struggle with new stuff – Dinushan Oct 20 '11 at 11:39
-
I implemented this code. The idea is nice, but the refresh rate is really awful, I can see the speed ball being redrawn which is just ugly from a users's perspective. I too am trying to achieve a demo 2D game using plain c# (without DirectX or XNA) for education purpose. Still digging so far – Mehdi LAMRANI Feb 02 '17 at 02:26
-
I solved it by adding Double buffering to the Form, using "DoubleBuffered = true" – Mehdi LAMRANI Feb 02 '17 at 04:21
-
@MehdiLAMRANI Interesting, the `SetStyle` lines should have the same effect as adding `DoubleBuffered = true`. – George Duckett Feb 02 '17 at 07:18
If you want to make dynamic drawings, you can use the WPF Canvas for these purposes. It doesn't support gaming or such, but it is a simple way of drawing primitive forms and images as you would do in a website.

- 6,384
- 4
- 43
- 78
The native rendering framework for .NET winforms apps is GDI+. There are many tutorials online for drawing simple shapes/graphics using GDI+

- 8,897
- 3
- 31
- 54