private void Clone_OnDie(object sender, EventArgs e) { DividingStudentSnake died = sender as DividingStudentSnake; Result result = new Result(died.Brain, died.Body.Count + 1, died.TotalCreated); using (StreamWriter strw = new StreamWriter(Environment.CurrentDirectory + "/Natural/brains.txt", true)) strw.WriteLine(JsonConvert.SerializeObject(result) + ", "); }
private void Win_KeyReleased(object sender, KeyEventArgs e) { if (e.Code == Keyboard.Key.W) { LastTest?.Answer(Direction.TOP); } else if (e.Code == Keyboard.Key.A) { LastTest?.Answer(Direction.LEFT); } else if (e.Code == Keyboard.Key.D) { LastTest?.Answer(Direction.RIGHT); } else if (e.Code == Keyboard.Key.S) { LastTest?.Answer(Direction.BOTTOM); } else if (e.Code == Keyboard.Key.P) { /*WF.FileDialog dialog = new WF.SaveFileDialog(); * if (dialog.ShowDialog() == WF.DialogResult.OK) * Snake.Brain.Save(dialog.FileName);*/ } else if (e.Code == Keyboard.Key.L) { StudentSnake protoSnake = new StudentSnake(Color.Red, Color.Blue, BRAIN_HEIGHT, BRAIN_WIDTH, 0, 0, INIT_SIZE); WF.FileDialog dialog = new WF.OpenFileDialog(); if (dialog.ShowDialog() == WF.DialogResult.OK) { protoSnake.LoadBrainFromFile(dialog.FileName); } for (int i = 0; i < INIT_SNAKE_COUNT; ++i) { DividingStudentSnake clone = new DividingStudentSnake(protoSnake, INIT_STUDENT_NRG, CREATE_NRG, DIE_NRG, NRG_PER_FOOD); clone.MoveHeadTo(R.Next(INIT_SIZE, GameField.Height - INIT_SIZE - 1), R.Next(1, GameField.Width - 1)); clone.OnDevide += Clone_OnDevide; clone.OnDie += Clone_OnDie; Snakes.Add(clone); } Continue(); } else if (e.Code == Keyboard.Key.K) { Play(); } else if (e.Code == Keyboard.Key.R) { Continue(); } }
public void Start() { DateTime currentTime = DateTime.Now; while (Win.IsOpen) { Win.DispatchEvents(); double elapsed = (DateTime.Now - currentTime).TotalSeconds; if (elapsed < 1.0 / FPS) { continue; } currentTime = DateTime.Now; Win.Clear(BackColor); if (!Stopped) { Snakes.RemoveAll(snake => snake.Dead); GameField.Update(Snakes); Snakes.ForEach(snake => snake.Update(GameField, Snakes)); foreach (DividingStudentSnake created in CreatedSnakes) { Snakes.Add(created); } CreatedSnakes.Clear(); } Win.Draw(GameField); Snakes.ForEach(snake => snake.Draw(Win, RenderStates.Default, GameField.CellWidthPixel, GameField.CellHeightPixel)); for (int i = 0; i < Snakes.Count; ++i) { DividingStudentSnake snake = Snakes[i]; BRAIN_SHAPE.Size = new Vector2f( snake.Brain.BrainWidth * GameField.CellWidthPixel, snake.Brain.BrainHeight * GameField.CellHeightPixel ); BRAIN_SHAPE.Position = new Vector2f( (snake.Head.Position.X - snake.Brain.BrainWidth / 2) * GameField.CellWidthPixel, (snake.Head.Position.Y - snake.Brain.BrainHeight / 2) * GameField.CellHeightPixel ); Win.Draw(BRAIN_SHAPE); Text score = new Text($"{i + 1}) size = {snake.Body.Count + 1}; energy = {snake.CurrentEnergy}; " + $"daughters = {snake.TotalCreated}; mutations = {snake.Brain.Mutations}", FONT) { FillColor = Color.Black, CharacterSize = 24 }; score.Position = new Vector2f(GameField.Width * GameField.CellWidthPixel + 60, i * 25 + 20); Win.Draw(score); RectangleShape colorIndicator = new RectangleShape(new Vector2f(20, 20)) { OutlineColor = Color.Black, FillColor = snake.BodyColor }; colorIndicator.Position = new Vector2f(GameField.Width * GameField.CellWidthPixel + 30, i * 25 + 25); Win.Draw(colorIndicator); } DrawExtra(); Win.Display(); } }
public SnakeCreatedEventArgs(DividingStudentSnake created) => Created = created;