ネイティブ グラフィック ライブラリのみを使用するJava 2D ゲーム チュートリアルとAndroid ゲーム チュートリアルのセットを見つけることができました。
C# (DirectX または XNA なし) で似たようなものを探しています。このゲーム ループ スケルトン
を見つけましたが、グラフィックスをレンダリングする方法がわかりません。
ターゲットは、単純な電子デバイスをシミュレートすることです。ユーザーがキーボードのいくつかのキーをすばやく「押した」ときに、グラフィック出力を表示する必要があります。したがって、アーケードゲームのように見えます。たとえば、ユーザーが矢印キーの 1 つを押すと、それに応じてポインター (画像) が移動します。
典型的な Windows フォーム アプリケーションでこれを行うことはできないと思いますか?
たとえば、
PictureBox
KeyPress
のイベントでそれを制御して移動しForm
ます。
18962 次
3 に答える
16
WinForms
これは、とTimer
を使用Graphics
して描画する (GDI+ をカプセル化する)簡単なゲームです。
10ミリ秒ごとに「刻む」タイマーを追加します。ティックごとにゲーム ロジックが実行され、オフスクリーン ビットマップに描画されます。これは、リンクの例のように継続的なループを使用するのとは対照的です。
フォームはキー イベントを個別に処理します (のようなことを行うのではなくGetKeyState
) 。
フォームのサイズが変更され、最初にロードされると、正しいサイズのバックバッファ ビットマップが作成されます。
新しいフォームを作成し、すべてのコードを以下に置き換えます。矢印キーを使用してボールを制御します。死ぬという概念はありません。
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)
}
}
}
于 2011-10-20T11:19:22.513 に答える
1
.NETwinformsアプリのネイティブレンダリングフレームワークはGDI+です。GDI+を使用して単純な形状/グラフィックを描画するための多くのチュートリアルがオンラインにあります
于 2011-10-20T11:15:57.440 に答える
1
動的な描画を作成する場合は、これらの目的でWPF Canvasを使用できます。ゲームなどはサポートしていませんが、Web サイトで行うようにプリミティブなフォームやイメージを簡単に描画できます。
于 2011-10-20T11:05:37.813 に答える