ゲーム用のループ処理を実装する方法
ゲームだと、普通にプログラムを作っていてはいけません。
描画速度をちゃんと管理しないとゲームとして成立しません。
そこでゲームループを実装する方法です。
プログラム
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loop {
static class Program {
private
const int waitTimes = (int)(1000.0 / 30.0);
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main() {
Form1 form = new Form1();
form.Show();
int targetTimes = System.Environment.TickCount & int.MaxValue;
targetTimes += waitTimes;
while (form.Created) {
int tickCount = System.Environment.TickCount & int.MaxValue;
if (targetTimes <= tickCount) {
// 描画処理本体
form.UpdateDisplay();
// 次回の描画予定時刻を更新する
targetTimes = (targetTimes + waitTimes) & int.MaxValue;
}
System.Threading.Thread.Sleep(1);
Application.DoEvents();
}
}
}
}これはプログラムクラスに書きます。
フォーム側にUpdateDisplay()を実装してください。
まとめ
ゲームを作ってみたかった。
作る技術を手に入れたときには、時間がなくなるもの。
若者には頑張って欲しい。

