using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
abstract class GameObject
{
public int X { get; set; }
public int Y { get; set; }
public Color color { get; set; }
public Action<GameObject,GameObject> Tactics;
}
class Player : GameObject
{
}
class Enemy : GameObject
{
}
Player pl;
List<Enemy> enemies;
Bitmap bmp;
Graphics g;
public Form1()
{
InitializeComponent();
//描画用の設定あれこれ(ちらつかないようにするとか、描画用メモリを500x500pxで用意するとか)
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
bmp = new Bitmap(500, 500);
g = Graphics.FromImage(bmp);
this.ClientSize = bmp.Size;
//プレイヤー
pl = new Player() { color = Color.Aqua };
//敵1の戦術はプレイヤーに近づく
Enemy teki1;
teki1 = new Enemy() { X = 200, Y = 200 ,color = Color.Red};
teki1.Tactics = (me,other) =>
{
me.X += (other.X <= me.X) ? -1 : 1;
me.Y += (other.Y <= me.Y) ? -1 : 1;
};
//敵2の戦術はプレイヤーから逃げる
Enemy teki2;
teki2 = new Enemy() { X = 200, Y = 300 ,color = Color.Green};
teki2.Tactics = (me,other) =>
{
me.X += (other.X <= me.X) ? 1 : -1;
me.Y += (other.Y <= me.Y) ? 1 : -1;
};
enemies = new List<Enemy>();
enemies.Add(teki1);
enemies.Add(teki2);
//スレッド実行
Task.Factory.StartNew(GameMain);
}
private void GameMain()
{
while (true)
{
Thread.Sleep(1000 / 60);
//敵の戦術を実行する。(プレイヤーの方向に向かって、逃げるか、向かうか)
foreach (var enm in enemies)
{
enm.Tactics(enm,pl);
}
//画面更新
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
g.Clear(Color.Black);
//プレイヤ描画
g.FillRectangle(new SolidBrush(pl.color), pl.X, pl.Y, 20, 20);
//敵描画
foreach (var enm in enemies)
{
g.FillRectangle(new SolidBrush(enm.color), enm.X, enm.Y, 20, 20);
}
e.Graphics.DrawImage(bmp, 0, 0);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
pl.X = e.X;
pl.Y = e.Y;
}
}
}