Archive Browser
Download aitest.zip, last updated 19/06/2010 (11.27 KB)
Download- md5: 9764f053887f43bd6b0b57f7db7496fa
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace AiTest
{
public partial class MainForm : Form
{
#region Private Member Declarations
private Map _map;
private Random _seed;
private List<Sprite> _sprites;
private Font _symbolFont;
#endregion Private Member Declarations
#region Public Constructors
public MainForm()
{
InitializeComponent();
}
#endregion Public Constructors
#region Protected Overridden Methods
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
mapPictureBox.Font = new Font("Small Fonts", 6);
newMapButton.PerformClick();
}
#endregion Protected Overridden Methods
#region Event Handlers
private void addButterflyButton_Click(object sender, EventArgs e)
{
this.AddSprite(new Butterfly());
}
private void addFireflyButton_Click(object sender, EventArgs e)
{
this.AddSprite(new Firefly());
}
private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void mapPictureBox_Paint(object sender, PaintEventArgs e)
{
int tileWidth;
int tileHeight;
int mapHeight;
int mapWidth;
tileWidth = mapPictureBox.ClientSize.Width / _map.Size.Width;
tileHeight = mapPictureBox.ClientSize.Height / _map.Size.Height;
mapWidth = (tileWidth * _map.Size.Width) - 1;
mapHeight = (tileHeight * _map.Size.Height) - 1;
e.Graphics.FillRectangle(SystemBrushes.Control, mapPictureBox.ClientRectangle);
e.Graphics.FillRectangle(Brushes.Black, new Rectangle(0, 0, mapWidth, mapHeight));
for (int x = 0; x < _map.Size.Width; x++)
{
// draw a horizontal grid line
e.Graphics.DrawLine(Pens.Red, new Point((x * tileWidth) - 1, 0), new Point((x * tileWidth) - 1, mapHeight));
for (int y = 0; y < _map.Size.Height; y++)
{
Rectangle cell;
cell = new Rectangle((x * tileWidth), (y * tileHeight), tileWidth - 1, tileHeight - 1);
// draw a vertical grid line
e.Graphics.DrawLine(Pens.Red, new Point(0, (y * tileHeight) - 1), new Point(mapWidth, (y * tileHeight) - 1));
// indicate the cell type
if (_map.Tiles[x, y].Solid)
e.Graphics.FillRectangle(Brushes.Green, cell);
// draw the cell coordinates
//e.Graphics.DrawString(string.Format("{0},{1}", x + 1, y + 1), mapPictureBox.Font, Brushes.White, cell.Location);
}
}
// draw the spires
foreach (Sprite sprite in _sprites)
{
RectangleF cell;
string arrowText;
cell = new RectangleF((sprite.Location.X * tileWidth), (sprite.Location.Y * tileHeight), tileWidth - 1, tileHeight - 1);
using (SolidBrush brush = new SolidBrush(sprite.Color))
e.Graphics.FillRectangle(brush, cell);
switch (sprite.Direction)
{
case Direction.Up:
arrowText = "á";
break;
case Direction.Left:
arrowText = "à";
break;
case Direction.Down:
arrowText = "â";
break;
case Direction.Right:
arrowText = "ß";
break;
default:
throw new ApplicationException();
}
e.Graphics.DrawString(arrowText, _symbolFont, Brushes.White, cell, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}
}
private void mapPictureBox_Resize(object sender, EventArgs e)
{
this.ResetFonts();
}
private void newMapButton_Click(object sender, EventArgs e)
{
// create a new map
_seed = new Random();
_map = new Map(new Size(24, 12));
for (int x = 0; x < _map.Size.Width; x++)
{
for (int y = 0; y < _map.Size.Height; y++)
{
Tile tile;
tile = new Tile();
tile.Location = new Point(x, y);
tile.Solid = (x == 0 || y == 0 || x == _map.Size.Width - 1 || y == _map.Size.Height - 1 || _seed.Next(10) == 5);
_map.Tiles[x, y] = tile;
}
}
_sprites = new List<Sprite>();
// make sure we refresh our display
this.ResetFonts();
}
private void nextMoveButton_Click(object sender, EventArgs e)
{
this.NextMove();
}
private void speedTrackBar_Scroll(object sender, EventArgs e)
{
if (speedTrackBar.Value != 0)
{
spriteTimer.Interval = speedTrackBar.Value;
spriteTimer.Start();
}
else
spriteTimer.Stop();
}
private void spriteTimer_Tick(object sender, EventArgs e)
{
this.NextMove();
}
#endregion Event Handlers
#region Private Methods
private void AddSprite(Sprite sprite)
{
sprite.Location = new Point(_seed.Next(_map.Size.Width - 2) + 1, _seed.Next(_map.Size.Height - 2) + 1);
sprite.Map = _map;
_sprites.Add(sprite);
mapPictureBox.Invalidate();
}
private void NextMove()
{
foreach (Sprite sprite in _sprites)
sprite.Move();
mapPictureBox.Invalidate();
}
private void ResetFonts()
{
if (_symbolFont != null)
_symbolFont.Dispose();
if (_map != null)
_symbolFont = new Font("Wingdings", (mapPictureBox.ClientSize.Height / _map.Size.Height) / 2);
mapPictureBox.Invalidate();
}
#endregion Private Methods
}
}
Donate
This software may be used free of charge, but as with all free software there are costs involved to develop and maintain.
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Donate