//Detects if player has hit fruit static void HitDetection(Graphics g, DrawingPanel panel, int x, int y, int length) { for (int i = 0; i < length; i++) { if (x == snakePossibleLengthX[i]) { if (y == snakePossibleLengthY[i]) { panel.RefreshDisplay(); g.DrawString("Game Over", font1, Brushes.Black, 300, 165); g.DrawString($"Final Score: {fruitEaten - 1}", font1, Brushes.Black, 300, 200); panel.RefreshDisplay(); Thread.Sleep(5000); Environment.Exit(1); } } } if (x > fruitX - 20 && x < fruitX + 20) { Fruit(g, panel, false); if (y > fruitY - 20 && y < fruitY + 20) { Fruit(g, panel, true); fruitEaten++; } } else { Fruit(g, panel, false); } }
/// <summary> /// Allows the user to clearly know, which AI lost by pausing the program with a window. /// </summary> /// <param name="s">receives the name of the loser AI</param> public void EliminateLoser(string s) { using (Font f = new Font("Helvetica", 20)) { this.g.DrawString("Loser", f, Brushes.Black, this.width / 2 - 2 * f.Size, this.height / 2); } p.RefreshDisplay(); // Makes a window pop up DrawingPanel.Pause(s + " lost. Press OK to challenge the winner."); }
/// <summary> /// Reads the appropriate state file and converts the /// coordinate information into an array to be drawn. /// </summary> /// <param name="stateName">state name</param> /// <param name="electionYear">election year</param> static void Region(string stateName, int electionYear) { using (StreamReader sr = new StreamReader(FilePath + stateName + ".txt")) { string[] min = sr.ReadLine().Split (new char[0], StringSplitOptions.RemoveEmptyEntries); // removes empty spaces float minLong = float.Parse(min[0]); float minLat = float.Parse(min[1]); string[] max = sr.ReadLine().Split (new char[0], StringSplitOptions.RemoveEmptyEntries); float maxLong = float.Parse(max[0]); float maxLat = float.Parse(max[1]); // min is scaled down to coordinate (0,0), // and max is scaled down to an appropriate size int width = (int)(Scale * (maxLong - minLong)); int height = (int)(Scale * (maxLat - minLat)); // initializes the drawing panel according to the scaled size DrawingPanel dp = new DrawingPanel(width, height); Graphics g = dp.GetGraphics(); int numPolygons = int.Parse(sr.ReadLine()); sr.ReadLine(); while (!sr.EndOfStream) { string subregionName = sr.ReadLine(); string regionName = sr.ReadLine(); PointF[] pts = new PointF[int.Parse(sr.ReadLine())]; for (int i = 0; i < pts.Length; i++) { string[] loc = sr.ReadLine().Split (new char[0], StringSplitOptions.RemoveEmptyEntries); // scale algorithm float x = Scale * (float.Parse(loc[0]) - minLong); float y = Scale * (float.Parse(loc[1]) - minLat); pts[i].X = x; pts[i].Y = height - y; } // Initilizes the Region class to the get the fill color // based on the election result the user-specified year; // gets the color for each subregion at a time until // all data for the subregions of the region are read Region region = new Region(regionName, subregionName, electionYear); Draw(dp, g, region, pts); sr.ReadLine(); } dp.RefreshDisplay(); // updates the drawing panel } }
//Takes care of controlling and drawing snake(Player) static void MovingSnake(Graphics g, DrawingPanel panel, int x, int y, ConsoleKey keyData, int fruitX, int fruitY) { for (; ;) { if (x > 800) { x -= 1050; } if (x < 0) { x += 1050; } if (y > 500) { y -= 725; } if (y < 0) { y += 725; } if (panel.Input.KeyAvailable) { char key = panel.Input.ReadKey(); if (panel.Input.KeyDown('w')) { direction = 1; } if (panel.Input.KeyDown('s')) { direction = 2; } if (panel.Input.KeyDown('d')) { direction = 3; } if (panel.Input.KeyDown('a')) { direction = 4; } switch (direction) { case 1: g.Clear(Color.White); g.FillRectangle(Brushes.Red, x, y -= 25, 25, 25); HitDetection(g, panel, x, y, fruitEaten); SnakePossibleArray(x, y, fruitEaten); NumberOfSnakeLength(g, panel, fruitEaten); g.DrawString($"Score: {fruitEaten - 1}", font1, Brushes.Black, 660, 10); panel.RefreshDisplay(); Thread.Sleep(10); break; case 2: g.Clear(Color.White); g.FillRectangle(Brushes.Red, x, y += 25, 25, 25); HitDetection(g, panel, x, y, fruitEaten); SnakePossibleArray(x, y, fruitEaten); NumberOfSnakeLength(g, panel, fruitEaten); g.DrawString($"Score: {fruitEaten - 1}", font1, Brushes.Black, 660, 10); panel.RefreshDisplay(); Thread.Sleep(10); break; case 3: g.Clear(Color.White); g.FillRectangle(Brushes.Red, x += 25, y, 25, 25); HitDetection(g, panel, x, y, fruitEaten); SnakePossibleArray(x, y, fruitEaten); NumberOfSnakeLength(g, panel, fruitEaten); g.DrawString($"Score: {fruitEaten - 1}", font1, Brushes.Black, 660, 10); panel.RefreshDisplay(); Thread.Sleep(10); break; case 4: g.Clear(Color.White); g.FillRectangle(Brushes.Red, x -= 25, y, 25, 25); HitDetection(g, panel, x, y, fruitEaten); SnakePossibleArray(x, y, fruitEaten); NumberOfSnakeLength(g, panel, fruitEaten); g.DrawString($"Score: {fruitEaten - 1}", font1, Brushes.Black, 660, 10); panel.RefreshDisplay(); Thread.Sleep(10); break; default: g.Clear(Color.White); g.FillRectangle(Brushes.Red, x, y -= 25, 25, 25); HitDetection(g, panel, x, y, fruitEaten); SnakePossibleArray(x, y, fruitEaten); NumberOfSnakeLength(g, panel, fruitEaten); g.DrawString($"Score: {fruitEaten - 1}", font1, Brushes.Black, 660, 10); panel.RefreshDisplay(); Thread.Sleep(10); break; } } Thread.Sleep(100); panel.RefreshDisplay(); } }