Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            wall_pic = new Bitmap("pics/" + "Green" + "Box.png");
            InitializeComponent();
            this.Width = RES_WIDTH + 325;
            this.Height = RES_HEIGHT + 40;

            PopLabel.Text = "Population: " + POPULATION.ToString();
            mutationLabel.Text = "Mutation Rate: " + mutationRate;
            crossoverLabel.Text = "Crossover Rate: " + crossoverRate.ToString();
            eliteLabel.Text = "Elitism (%): " + populationElitism.ToString();

            START_POSITION = new Point(LEN + LEN / 2, LEN + LEN / 2);
            walls = new Wall[RES_HEIGHT / LEN + 1, RES_WIDTH / LEN + 1];
            stepThroughWalls((int i, int j) => { walls[j, i] = new Wall(i * LEN, j * LEN, LEN, LEN); });
            GA_ENGINE = new GeneticAlgorithm(BITS_PER_CHROM, NUMB_STEPS, POPULATION, crossoverRate, mutationRate, populationElitism);
            PAUSE = true;
            DELAY_PAUSE = 0;
            cheese = new Cheese(new Point(), 1, 1, 1);
            cheese.location.X = LEN * ((RES_WIDTH / LEN) / 2) + LEN / 2;
            cheese.location.Y = LEN / 2;
            windowsGraphics = this.CreateGraphics();
            screen = new Bitmap(this.Width, this.Height);
            onScreenGraphics = Graphics.FromImage(screen);
            this.Paint += new PaintEventHandler(DrawGame);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(cheese.KeyDown);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(MainFormKeyDown);
            //this.KeyUp += new System.Windows.Forms.KeyEventHandler(cheese.KeyUp);
        }
Beispiel #2
0
 /// <summary>
 /// Determines whether the rat is touching the wall, w and sets the collision point if it is.
 /// </summary>
 /// <param name="wall">The wall to be checked.</param>
 /// <param name="touchPoint">The touching point if the wall is touching the rat.</param>
 /// <returns>
 ///   <c>true</c> if [is touching wall]; otherwise, <c>false</c>.
 /// </returns>
 public bool isTouchingWall(Wall wall, ref PointF touchPoint)
 {
     PointF nearestPoint = wall.PointNearestTo(this.location);
     float distance = (float)Math.Sqrt((nearestPoint.X - location.X) * (nearestPoint.X - location.X) + (nearestPoint.Y - location.Y) * (nearestPoint.Y - location.Y));
     if (distance < this.radius)
     {
         touchPoint = nearestPoint;
         return true;
     }
     else
     {
         return false;
     }
 }