这是一个简单的游戏,使用WinForms
and a Timer http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx, using Graphics http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx绘制(封装 GDI+)。
它添加了一个每 10 毫秒“滴答”一次的计时器。每次勾选它都会执行游戏逻辑,然后绘制到屏幕外的位图。这与链接示例中使用连续循环相反。
该表单单独处理关键事件(而不是执行类似的操作GetKeyState http://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx)
当调整表单大小并首次加载时,它将创建正确大小的后台缓冲区位图。
创建一个新表单并将所有代码替换为以下代码。使用箭头键控制球。没有死亡的念头。
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)
}
}
}