This simple demo demonstrates some simple animation using C# and GDI+
All it does right now is bounce a 10 pixel sized "ball" around the screen ... the middle line is just for effect. Since every resource I've come across says that you should always start simple (pong, or tetris), and finish every game when learning game programming, I plan on finishing my implementation of PONG. Of course, full source code will be provided as I move along so keep an eye out all you budding game developers.
Here's the code:
GameForm.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CodeCube.Games
{
///<summary>
/// Base class for the game ... takes care of setting up the game loop
///</summary>
public abstract class GameForm : System.Windows.Forms.Form
{
Timer timer;
int TimerInterval = 25;
public GameForm()
{
init();
}
void init()
{
timer = new Timer();
timer.Enabled = true;
timer.Interval = TimerInterval;
timer.Tick += new EventHandler(game_loop);
this.Paint += new PaintEventHandler(render_loop);
// Double buffering
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer,true);
}
///<summary>
/// The game_loop function is called once for every this.TimerInterval
/// Use the game_loop to modify the state of your game ... rendering is taken
/// care of in the render_loop
///</summary>
public abstract void game_loop(Object ob, EventArgs ev);
///<symmary>
/// The render_loop will get called when you call this.Invalidate()
/// </symmary>
public abstract void render_loop(Object ob, PaintEventArgs ev);
}
}
PongForm.cs
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using CodeCube.Games;
namespace CodeCube.Games.Pong {
public class PongForm: GameForm
{
Rectangle rect;
enum Direction {Left,Right,Up, Down}
Direction xdir = Direction.Right;
Direction ydir = Direction.Down;
public PongForm() : base()
{
rect = new Rectangle(0,0,10,10);
this.BackColor = Color.Black;
}
public override void game_loop(Object ob, EventArgs ev)
{
//Handle Horizontal Movement
if (xdir == Direction.Right & rect.X < this.ClientRectangle.Width - rect.Width )
{
rect.X++;
} else {
xdir = Direction.Left;
if (rect.X > 0)
{
rect.X--;
} else {
xdir = Direction.Right;
}
}
//Handle Vertical Movement
if (ydir == Direction.Down & rect.Y < this.ClientRectangle.Height - rect.Height )
{
rect.Y++;
} else {
ydir = Direction.Up;
if (rect.Y > 0)
{
rect.Y--;
} else {
ydir = Direction.Down;
}
}
this.Invalidate(this.ClientRectangle);
}
public override void render_loop(Object ob, PaintEventArgs ev)
{
Graphics g = ev.Graphics;
SolidBrush whitebrush = new SolidBrush(Color.White);
//Draw the line in the middle
int linex = this.ClientRectangle.Width / 2 - 5;
ev.Graphics.FillRectangle(whitebrush, new Rectangle(linex,0,10,this.Height));
//Draw the "ball"
ev.Graphics.FillRectangle(whitebrush,rect);
}
[STAThread]
static void Main()
{
Application.Run(new PongForm());
}
}
}
Since I'm a complete amateur at game programming, I hope this can be a good resource for others just getting into this as well because they'll be able to track the path I took. A big thanks goes out to DJKNO3 for his help
pong.zip - 7k