public static void Init() { isActive = true; snake = new Snake(); food = new Food(); wall = new Wall(); snake.body.Add(new Point { x = 15, y = 14 }); food.body.Add(new Point { x = 14, y = 15 }); food.color = ConsoleColor.Blue; wall.color = ConsoleColor.Green; snake.color = ConsoleColor.Yellow; Console.SetWindowSize(48, 19); }
static void Main(string[] args) { Console.SetBufferSize(80, 25); //Отрисовка рамочки HorizontalLine upLine = new HorizontalLine(0, 78, 0, '+'); HorizontalLine downLine = new HorizontalLine(0, 78, 24, '+'); VerticalLine leftLine = new VerticalLine(0,24,0,'+'); VerticalLine rightLine = new VerticalLine(0, 24, 78, '+'); upLine.Draw(); downLine.Draw(); leftLine.Draw(); rightLine.Draw(); //HorizontalLine line = new HorizontalLine(5,10,8,'+'); //line.Draw(); //VerticalLine vLine = new VerticalLine(4,20,5,'*'); //vLine.Draw(); Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Diraction.RIGHT); snake.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.SetWindowSize(1, 1); Console.SetBufferSize(80, 25); Console.SetWindowSize(80, 25); // Отрисовка рамочки HorizontalLine upLine = new HorizontalLine(0, 78, 0, '+'); upLine.Draw(); HorizontalLine downLine = new HorizontalLine(0, 78, 24, '+'); downLine.Draw(); VerticalLine leftLine = new VerticalLine(0, 0, 24, '+'); leftLine.Draw(); VerticalLine rightLine = new VerticalLine(78, 0, 24, '+'); rightLine.Draw(); //отрисовка точек Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); while(true) { if(Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } Thread.Sleep(100); snake.Move(); } // Console.ReadLine(); }
static void Main(string[] args) { var x = Console.WindowWidth; //Console.SetBufferSize(80, 25); HorizontalLine upLine = new HorizontalLine(0, 78, 0, '+'); HorizontalLine downLine = new HorizontalLine(0, 78, 24, '+'); VerticalLine leftLine = new VerticalLine(0, 24, 0, '+'); VerticalLine rightLine = new VerticalLine(0, 24, 78, '+'); upLine.DrawLine(); downLine.DrawLine(); leftLine.DrawLine(); rightLine.DrawLine(); Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.DrawLine(); while (true) { if(Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } Thread.Sleep(100); snake.Move(); } Console.ReadKey(); }
static void Main(string[] args) { Console.SetBufferSize(80, 25); Walls walls = new Walls(80, 25); walls.Draw(); Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(80,25, '$'); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } } }
/// <summary> /// A controller method that receives and extracts world information /// from the server. /// </summary> /// <param name="state"></param> private void ReceiveWorld(SocketState state) { for (int i = 0; i < state.DataLength; i++) { JObject obj = JObject.Parse(state.Messages[i]); JToken jAttribute = obj["vertices"]; if (jAttribute != null) { lock (this) { // If the received message deserializes to a snake, update the world with a new snake. Snake.Snake snake = JsonConvert.DeserializeObject <Snake.Snake>(state.Messages[i]); if (snake._ID == playerID && snake.Vertices.Last.Value._x == -1 && snake.Vertices.Last.Value._y == -1) { isConnected = false; Invoke(new MethodInvoker(AllowReconnect)); } world.Update(snake); } } else // else update the world with a food object. { lock (this) { Food food = JsonConvert.DeserializeObject <Food>(state.Messages[i]); world.Update(food); } } } UpdateFrame(); BeginInvoke(new MethodInvoker(UpdateScoreboard)); Network.GetData(state); }
static void Main(string[] args) { Console.SetBufferSize(80, 25); HorizontalLine upLine = new HorizontalLine(0, 78, 0, '+'); HorizontalLine downLine = new HorizontalLine(0, 78, 24, '+'); VerticalLine leftLine = new VerticalLine(1, 24, 0, '+'); VerticalLine rightLine = new VerticalLine(1, 24, 78, '+'); upLine.Draw(); downLine.Draw(); leftLine.Draw(); rightLine.Draw(); Point p = new Point(1, 4, '*'); Snake snake = new Snake(p, 5, Direction.RIGHT); snake.Draw(); snake.Move(); Thread.Sleep(300); snake.Move(); Thread.Sleep(300); snake.Move(); Thread.Sleep(300); snake.Move(); Thread.Sleep(300); snake.Move(); Thread.Sleep(300); snake.Move(); Thread.Sleep(300); snake.Move(); Thread.Sleep(300); snake.Move(); Thread.Sleep(300); Console.ReadLine(); }
public static List<GameObject> Add(this List<GameObject> list, Snake snk) { foreach(var block in snk.Blocks) { list.Add(block); } return list; }
public formMain() { InitializeComponent(); snake = new Snake(); food = new Food(rand); gameLoop.Interval = 55; gameLoop.Tick += Update; }
protected override void Initialize() { // Create the snake with 1 piece. // If you're modifying the game, you can set this to anything over 0. _snake = new Snake(1); base.Initialize(); }
public static void Main(string[] args) { // size of map int MaxMapWidth = 80; int MaxMapHeight = 25; Console.SetBufferSize(MaxMapWidth, MaxMapHeight); // drow frame Walls walls = new Walls(MaxMapWidth, MaxMapHeight); walls.Draw(); // drow point draw snake Point point = new Point(5, 10, '*'); Snake snake = new Snake (point, 5, Direction.RIGHT); snake.Draw (); // draw food FoodCreater foodCreator = new FoodCreater (MaxMapWidth, MaxMapHeight, '$'); Point food = foodCreator.CreateFood (); //Console.ForegroundColor = ConsoleColor.Cyan; food.Draw (); while (true) { // data de perete if (walls.IsHit (snake) || snake.IsHitTail()) break; // daca maninca if(snake.Eat(food)){ food = foodCreator.CreateFood (); food.Draw (); } else snake.Move (); Thread.Sleep (200); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey (); snake.Hadlekey (key.Key); if (key.Key == ConsoleKey.Escape) break; } } // END Console.ForegroundColor = ConsoleColor.Red; Console.SetCursorPosition(MaxMapWidth/3 , 10); Console.WriteLine ("--=***=-- E N D --=***=--"); Console.SetCursorPosition(MaxMapWidth/3 , 12); //Thread.Sleep (300); //Console.ReadLine (); }
public GameInfoDTO initiate(int snakeSize, int inScrnWidth, int inScrnHeight) { screenWidth = inScrnWidth; screenHeight = inScrnHeight; snake = new Snake(snakeSize); setApplePos(); gameInfo = new GameInfoDTO(snake.getSnake(), apple); return gameInfo; }
private void Initialize() { _direction = Snake.EDirection.E; var size = new Size(70, 50); _snake = new Snake(3, size); picturebox.Size = Visualizer.MapSize = new Size(size.Width * Visualizer.CellSize, size.Height * Visualizer.CellSize); picturebox.Image = Visualizer.Paint(_snake.GetCoords()); timer.Start(); }
public Point createFood(Snake avoidsnake) { Point foodPoint; do { foodPoint = new Point(rand.Next(4, winW - 2), rand.Next(4, winH - 2), sym); } while (avoidsnake.IsHit(foodPoint)); return foodPoint; }
public Form1() { InitializeComponent(); LinkedList<Rectangle> body = new LinkedList<Rectangle>(); body.AddFirst(new Rectangle(4, 5, 1, 1)); body.AddFirst(new Rectangle(5, 5, 1, 1)); body.AddFirst(new Rectangle(6, 5, 1, 1)); snake = new Snake(body); }
static void Main(string[] args) { // Рисуем границы Walls wall = new Walls(80, 25); wall.Draw(); //рисуем змею Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 5, Direction.RIGHT); snake.Draw(); //создаём еду FoodCreator foodCeator = new FoodCreator(78, 23, '$'); Point food = foodCeator.CreateFood(); food.Draw(); while (true) { if (snake.crossingWall() || snake.crossingBody()) // проверка столкновения { Console.Clear(); Console.WriteLine("Ты проиграл!"); Console.ReadLine(); break; } if (snake.Eat(food)) { food = foodCeator.CreateFood(); while (true) { if (snake.goodEat(food)) { food.Draw(); break; } else food = foodCeator.CreateFood(); } } else { snake.Move(); } Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.iMoveTo(key.Key); } } }
static void Main(string[] args) { Console.SetBufferSize(80, 25); int speed = 100; GameObjects gameObjects = new GameObjects(); char sym = '$'; // Создадим и отобразим стены. Так же занесём их в список объектов Walls walls = new Walls('+'); gameObjects.Add(walls); // Создадим змейку Snake snake = new Snake(new Point(2, 3, '*'), 3, Direction.RIGHT); gameObjects.Add(snake); // Создадим еду FoodCreator foodCreator = new FoodCreator('$'); foodCreator.CreateFood(gameObjects, 1); gameObjects.Add(foodCreator); while(true) { if (gameObjects.Intersect(snake)) { Figure intersectFigure = new Figure(); int intersectObjectNum = gameObjects.GetIntersectObjectNum(snake); intersectFigure = gameObjects.GetObject(intersectObjectNum); if(intersectFigure.GetObjectType() == ObjectTypes.FOOD) { gameObjects.DeleteObject(intersectObjectNum); snake.Eat(); FoodCreator foodCreator2 = new FoodCreator('$'); foodCreator2.CreateFood(gameObjects, 2); gameObjects.Add(foodCreator2); } else { Console.SetCursorPosition(10, 10); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Game Over"); Console.ReadLine(); break; } } // Проверим нажата ли кнопка if (Console.KeyAvailable) { // Если таки нажата, то изменим направление в соответсвии с нажатием ConsoleKeyInfo key = Console.ReadKey(); snake.HandelKey(key.Key); } Thread.Sleep(speed); snake.Move(); } }
/// <summary> /// Takes the list of snakes and updates the world with dead versions. /// </summary> /// <param name="ls">The ls.</param> /// <returns></returns> private List <Snake.Snake> KillSnakes(List <Snake.Snake> ls) { List <Snake.Snake> deadSnakes = new List <Snake.Snake>(); foreach (Snake.Snake s in ls) { Snake.Snake newDeadSnake = new Snake.Snake(new Point(-1, -1), new Point(-1, -1), s._ID, s._name); world.Update(newDeadSnake); deadSnakes.Add(newDeadSnake); } return(deadSnakes); }
static void Main(string[] args) { Game.Init(); Snake.Snake snake = Game.snake; while (true) { if (Game.food.location.x < Game.snake.body[0].x) { Game.snake.direction = 1; } Game.snake.Move(Game.snake.direction); } }
public SnakeBot(Form1 caller, Map map, int food) { this.Parent = caller; this.Food = food; this.map = map; foreach (Land l in map.Locations) { if (l.OnIt == Occupition.Snake) { throw new ArgumentException("The map needs to be empty!"); } } snake = new Snake.Snake(map.Locations[0, 0]); }
static void Main(string[] args) { Console.SetBufferSize(80, 25); Walls walls = new Walls(80, 25); walls.Draw(); Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(80, 25, '$'); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } Thread.Sleep(100); } Console.ForegroundColor = ConsoleColor.Yellow; int y = 9; WriteLine("============================", 26, y++); WriteLine("Game over", 35, y++); WriteLine("", 26, y += 2); WriteLine("Created by: Oleg Glytenko", 28, y++); WriteLine("My first game!!!", 32, y++); WriteLine("============================", 26, y++); Console.SetCursorPosition(0, 0); Thread.Sleep(1000); Console.ReadKey(); }
static void Main(string[] args) { Console.SetBufferSize(80,25); // Отрисовка рамки HorizontalLine upLine = new HorizontalLine(0, 78, 0, '+'); HorizontalLine downLine = new HorizontalLine(0, 78, 24, '+'); VerticalLine leftLine = new VerticalLine(0, 24, 0, '+'); VerticalLine rightLine = new VerticalLine(0, 24, 78, '+'); upLine.Drow(); downLine.Drow(); leftLine.Drow(); rightLine.Drow(); // Отрисовка точек Point p = new Point(4,5,'*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Drow(); // Еда FoodCreator foodCreator = new FoodCreator(80,25,'$'); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } Thread.Sleep(100); snake.Move(); } }
public Engine(int snakeBodyParts, int snakeStartX, int snakeStartY, int snakeSize, int numberOfBites) { Snake = new Snake(snakeBodyParts, snakeStartX, snakeStartY, snakeSize); Bites = new List<Food>(); CurrentDirection = Directions.Right; var random = new Random(); for (int i = 0; i < numberOfBites; i++) { var bounty = random.Next(1, 4); var randomX = random.Next(1, 30); var randomY = random.Next(1, 30); Bites.Add(new Food(randomX * 10, randomY * 10, bounty, 10)); } }
static void Main(string[] args) { Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight); // Рамка Walls walls = new Walls(Console.WindowWidth, Console.WindowHeight); walls.Draw(); //Змея Point p = new Point(4,5); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); //Еда FoodCreator foodCreator = new FoodCreator(Console.WindowWidth, Console.WindowHeight, '$'); Point food = foodCreator.CreateFood(); Console.ForegroundColor = ConsoleColor.Cyan; food.Draw(); Console.ResetColor(); //Двигаем while(true) { if (walls.IsHit(snake) || snake.IsHitTail()) break; if (snake.Eat(food)) { food = foodCreator.CreateFood(); Console.ForegroundColor = ConsoleColor.Cyan; food.Draw(); Console.ResetColor(); } else snake.Move(); Thread.Sleep(200); if (Console.KeyAvailable) { snake.HandleKey(Console.ReadKey().Key); } } Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2); snake.Clear(); food.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Good Bye!"); Console.ResetColor(); Console.ReadLine(); }
static void Main(string[] args) { Walls walls = new Walls(80,25); walls.Draw(); Point tale = new Point(2, 12, '*'); Snake snake = new Snake(tale,7,Direction.right); snake.Draw(); FoodCreator foodCreator = new FoodCreator(78,25,'$'); Point food = foodCreator.CreateFood(); food.Show(); Console.SetCursorPosition(0, 0); Console.Write("Score: 0"); int s = 0; while (true) { if (walls.isHit(snake.points.Last()) || snake.IsHit()) { break; } if (snake.Eat(food)) { s++; Console.SetCursorPosition(0, 0); Console.Write("Score: "+s); Thread.Sleep(10); food = foodCreator.CreateFood(); food.Show(); } if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } Thread.Sleep(150); snake.Move(); } Console.SetCursorPosition(30, 12); Console.WriteLine("OLOLO!!!!!!!!!!!!!!!!!"); Console.ReadLine(); }
static void Main(string[] args) { Console.SetBufferSize(80, 25); Wall wall = new Wall(80, 25, '%'); wall.Drow(); /*VerticalLine vert_line_left = new VerticalLine(24, 0, 79, '%'); vert_line_left.Drow(); VerticalLine vert_line_right = new VerticalLine(24, 0, 0, '%'); vert_line_right.Drow(); HorizontalLine hor_line_bottom = new HorizontalLine(0, 78, 24, '%'); hor_line_bottom.Drow(); HorizontalLine hor_line_top = new HorizontalLine(0, 78, 0, '%'); hor_line_top.Drow();*/ Point start = new Point(15, 5, '*'); Snake snake = new Snake(start, 8, Direction.RIGHT); snake.Drow(); FoodCreator createfood = new FoodCreator(80, 25, '$'); Point food = createfood.CreateFood(); food.Drow(); while(true) { if(wall.IsHit(snake) || snake.IsHitTail() ) { break; } if(snake.Eat(food)) { food = createfood.CreateFood(); food.Drow(); } if(Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key); } Thread.Sleep(100); snake.Move(); } }
// xử lí chơi lại public void reset() { timer1.Enabled = false; MessageBox.Show("Điểm: " + score.ToString(), "GAME OVER"); timer1.Interval = 150; // đưa tốc độ về lúc ban đầu score = 0; // đưa điểm về lại bằng 0 toolStripStatusLabelScore.Text = "0"; newgame.Text = "PLAY AGAIN"; panel1.Show(); newgame.Show(); High.Show(); Exit_btn.Show(); btn_About.Show(); lbSnake.Show(); level1.Hide(); level2.Hide(); snake = new Snake.Snake(); }
static void Main(string[] args) { Snake snake = new Snake(); ConsoleKeyInfo key = new ConsoleKeyInfo(); Console.BackgroundColor = ConsoleColor.DarkGreen; Console.ForegroundColor = ConsoleColor.Black; while (true) { System.Threading.ThreadPool.QueueUserWorkItem(delegate { key = Console.ReadKey(); }, null); snake.testKeys(key); Location.printAtLocation("*",snake.getLoc()); System.Threading.Thread.Sleep(25); } }
static void Main(string[] args) { Console.SetBufferSize(120, 30); //The frame is drawn HorizontalLine upLine = new HorizontalLine(0, 118, 0, '+'); HorizontalLine downLine = new HorizontalLine(0, 118, 29, '+'); VerticalLine leftLine = new VerticalLine(0, 29, 0, '+'); VerticalLine rightLine = new VerticalLine(0, 29, 118, '+'); upLine.Draw(); downLine.Draw(); leftLine.Draw(); rightLine.Draw(); //The points are drawn Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(129, 30, '$'); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandlKey(key.Key); } } }
static void Main(string[] args) { Console.SetBufferSize(80, 25); HorizontalLine topLine = new HorizontalLine(0, 78, 0, '+'); HorizontalLine bottomLine = new HorizontalLine(0, 78, 24, '+'); VerticalLine leftLine = new VerticalLine(0, 24, 0, '+'); VerticalLine rightLine = new VerticalLine(0, 24, 78, '+'); topLine.LineDraw(); bottomLine.LineDraw(); leftLine.LineDraw(); rightLine.LineDraw(); Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.LineDraw(); Console.ReadLine(); }
/// <summary> /// Initialize a new game. /// </summary> /// <param name="Caller"></param> /// <param name="map"></param> public static void Initialize(Form Caller, Map map, Func <int, int>[] foodChanger = null) { if (GameRuning) { throw new InvalidOperationException("The Game Handle cant initialize when the game is running!"); } FC = foodChanger; GameUI = Caller; GameHandle.map = map; snake = new Snake.Snake(map.GetRandomLand(1)); mapSize = new Size(map.Width, map.Height); GameRuning = true; GameT = new Thread(() => { Game_Handle(); }); GameT.Start(); snake.OnSnakeMoves += (x, y) => { OnSnakeMoves?.Invoke(snake, y); }; snake.OnSnakeEats += (s, l) => { OnSnakeEats?.Invoke(snake, l); }; snake.SnakeHasCapturedTheWholeMap += (s, e) => { GameFinished?.Invoke(snake, e); }; OnGameInitialize?.Invoke(Caller, map); }
static void Main(string[] args) { int Width = 80; int Height = 45; Console.SetBufferSize(Width, Height); Console.SetWindowSize(Width - 1, Height - 1); Obstacles border = new Obstacles(); border.setBorder(Width, Height); border.setObstacles(Width, Height); Point p1 = new Point(2, 2, '*'); Snake snake = new Snake(p1, 3, Direction.Right); snake.Drow(); FoodCreator foodCreator = new FoodCreator(Width - 3, Height - 3, '#'); Point food = foodCreator.CreateFood(); food.Draw(); int k = 150; while (true) { if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); k -= 5; } if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.Pressure(key.Key); } Thread.Sleep(k); snake.Move(); Console.SetCursorPosition(0, 0); } }
public static void Main(string[] args) { Walls walls = new Walls( 79, 24 ); walls.Draw(); // Отрисовка точек Point p = new Point( 4, 5, '*' ); Snake snake = new Snake( p, 4, Direction.RIGHT ); snake.Draw(); FoodCreator foodCreator = new FoodCreator( 77, 22, '$' ); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if ( walls.IsHit(snake) || snake.IsHitTail() ) { break; } if(snake.Eat( food ) ) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep( 100 ); if ( Console.KeyAvailable ) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey( key.Key ); } } WriteGameOver(); Console.ReadLine(); }
static void Main(string[] args) { int t; Console.WriteLine(" Выберите свой уровень игры:\nвысокий - 1, средний - 2 или низкий - 3"); ConsoleKeyInfo cinf= Console.ReadKey(); t = (cinf.KeyChar - 48) * 100; Console.Clear(); Console.SetBufferSize(80, 25); Walls wall = new Walls(80, 25); wall.Drow(); Point p1 = new Point(3, 3, '*',ColorSnake.WHITE); Snake sn = new Snake(p1, 5, Direction.RIGHT); sn.Draw(); FoodCreator fc = new FoodCreator(80, 25, '$',ColorSnake.YELLOW); Point food = fc.CreateFood(); food.Draw(); while (true) { if (wall.IsHit(sn) || sn.IsHitTail()) break; if (sn.Eat(food)) { food = fc.CreateFood(); food.Draw(); } else sn.Move(); Thread.Sleep(t); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); sn.HandleKey(key.Key); } } Console.Clear(); Console.WriteLine(" Игра окончена! \nСпасибо за внимание."); Console.ReadLine(); }
public void clearLvl() { int level = game.gameLvl + 1; gameTimer.Enabled = false; if (level > game.endLvl) { level = 1; game.gameDone(); pictureBox.Refresh(); game.status = 3; panel2.Visible = true; } else { game.lvlClear(); panel3.Visible = true; pictureBox.Refresh(); game = new Snake(user_ID); game.status = 5; game.gameLvl = level; } }
static void Main( string[] args ) { Console.SetWindowSize(1, 1); Console.SetBufferSize(80, 25); Console.SetWindowSize(80, 25); Point lefttop = new Point(0, 0, ' '); Point rightbottom = new Point(79, 23, ' '); // Рисуем рамочку char sym = '+'; Horizontalline topline = new Horizontalline(lefttop.x, rightbottom.x, lefttop.y, sym); Horizontalline bottomline = new Horizontalline(lefttop.x, rightbottom.x, rightbottom.y, sym); Verticalline leftline = new Verticalline(lefttop.x, lefttop.y++, rightbottom.y--, sym); Verticalline rightline = new Verticalline(rightbottom.x, lefttop.y++, rightbottom.y--, sym); topline.Draw(); bottomline.Draw(); leftline.Draw(); rightline.Draw(); Point p = new Point(3, 5, '█'); Snake snake = new Snake(p, 10, Direction.RIGHT); while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); snake.HandleKey(key.Key); } System.Threading.Thread.Sleep(100); snake.Move(); } Console.ReadLine(); }
public override void UpdateSnakeHead(Snake snake) { snake.SetSnakeHead("^"); }
/// <summary> /// Detects whether a snake has collided with the border or another snake. /// If yes, it adds the snake to the given list of dead snakes. /// </summary> /// <param name="s">The s.</param> /// <param name="p">The p.</param> /// <param name="deadSnakeList">The dead snake list.</param> /// <param name="eatenFood">The eaten food.</param> private void DetectCollision(Snake.Snake s, Point p, List <Snake.Snake> deadSnakeList, List <Food> eatenFood) { if (p._x == 0 || p._x == boardWidth || p._y == 0 || p._y == boardHeight) { deadSnakeList.Add(s); } else { if (world.WorldMap[p._x, p._y].ID != -1) { if (world.Snakes.ContainsKey(world.WorldMap[p._x, p._y].ID)) { deadSnakeList.Add(s); } else//Eat food command. { if (altGamePlayEnable) { int randomFoodValue = randomGenerator.Next(0, 3); eatenFood.Add(new Food(new Point(-1, -1), world.WorldMap[p._x, p._y].ID)); world.Food.Remove(world.WorldMap[p._x, p._y].ID); for (int i = 0; i <= randomFoodValue; i++) { world.WorldMap[p._x, p._y].ID = s._ID; world.WorldMap[p._x, p._y].direction = world.WorldMap[world.Snakes[s._ID].Vertices.Last.Value._x, world.Snakes[s._ID].Vertices.Last.Value._y].direction; world.Snakes[s._ID].Vertices.AddAfter(s.Vertices.Last, new Point(p._x, p._y)); if (world.WorldMap[p._x, p._y].direction == 1) { p = new Point(p._x, p._y - 1); } else if (world.WorldMap[p._x, p._y].direction == 2) { p = new Point(p._x + 1, p._y); } else if (world.WorldMap[p._x, p._y].direction == 3) { p = new Point(p._x, p._y + 1); } else if (world.WorldMap[p._x, p._y].direction == 4) { p = new Point(p._x - 1, p._y); } if (world.Snakes.ContainsKey(world.WorldMap[p._x, p._y].ID)) { deadSnakeList.Add(s); break; } else if (world.Food.ContainsKey(world.WorldMap[p._x, p._y].ID) || (p._x == 0 || p._x == boardWidth || p._y == 0 || p._y == boardHeight)) { DetectCollision(s, p, deadSnakeList, eatenFood); } } } else { world.Food.Remove(world.WorldMap[p._x, p._y].ID); eatenFood.Add(new Food(new Point(-1, -1), world.WorldMap[p._x, p._y].ID)); world.WorldMap[p._x, p._y].ID = s._ID; world.WorldMap[p._x, p._y].direction = world.WorldMap[world.Snakes[s._ID].Vertices.Last.Value._x, world.Snakes[s._ID].Vertices.Last.Value._y].direction; world.Snakes[s._ID].Vertices.AddAfter(s.Vertices.Last, new Point(p._x, p._y)); } } } else {//need to move the tail/remove old one world.WorldMap[p._x, p._y].direction = world.WorldMap[world.Snakes[s._ID].Vertices.Last.Value._x, world.Snakes[s._ID].Vertices.Last.Value._y].direction; world.Snakes[s._ID].Vertices.AddAfter(s.Vertices.Last, new Point(p._x, p._y)); int tailX = s.Vertices.First.Value._x; int tailY = s.Vertices.First.Value._y; int nextVX = s.Vertices.First.Next.Value._x; int nextVY = s.Vertices.First.Next.Value._y; if ((tailX - nextVX == 1 || nextVX - tailX == 1) && (tailY == nextVY)) { world.WorldMap[world.Snakes[s._ID].Vertices.First.Value._x, world.Snakes[s._ID].Vertices.First.Value._y].ID = -1; world.Snakes[s._ID].Vertices.RemoveFirst(); } else if ((tailY - nextVY == 1 || nextVY - tailY == 1) && (tailX == nextVX)) { world.WorldMap[world.Snakes[s._ID].Vertices.First.Value._x, world.Snakes[s._ID].Vertices.First.Value._y].ID = -1; world.Snakes[s._ID].Vertices.RemoveFirst(); } else { world.WorldMap[world.Snakes[s._ID].Vertices.First.Value._x, world.Snakes[s._ID].Vertices.First.Value._y].ID = -1; world.Snakes[s._ID].Vertices.RemoveFirst(); if (tailX < nextVX) { world.Snakes[s._ID].Vertices.AddFirst(new Point(tailX + 1, tailY)); } else if (tailX > nextVX) { world.Snakes[s._ID].Vertices.AddFirst(new Point(tailX - 1, tailY)); } else if (tailY < nextVY) { world.Snakes[s._ID].Vertices.AddFirst(new Point(tailX, tailY + 1)); } else if (tailY > nextVY) { world.Snakes[s._ID].Vertices.AddFirst(new Point(tailX, tailY - 1)); } } world.WorldMap[p._x, p._y].ID = s._ID; } } }
public void LaunchGame(Game newGame) { Console.SetWindowSize(mapWidth, mapHeight + 6); newGame.GenerateMap(mapHeight, mapWidth, stage); Snake snake = newGame.Map.Snake; builder = new ConsoleMapBuilder(mapWidth, mapHeight); Console.BackgroundColor = ConsoleColor.Cyan; Console.ForegroundColor = ConsoleColor.Yellow; Console.SetCursorPosition(0, mapHeight); for (int i = 0; i < 6; i++) { Console.WriteLine(new string(' ', mapWidth)); } while (true) { if (Console.KeyAvailable) { switch (Console.ReadKey().Key) { case ConsoleKey.DownArrow: snake.ChangeDirection(MovingEntity.TDirection.Down); break; case ConsoleKey.UpArrow: snake.ChangeDirection(MovingEntity.TDirection.Up); break; case ConsoleKey.LeftArrow: snake.ChangeDirection(MovingEntity.TDirection.Left); break; case ConsoleKey.RightArrow: snake.ChangeDirection(MovingEntity.TDirection.Right); break; } } while (!Console.KeyAvailable) { if (newGame.Map.GameOver) { Console.SetCursorPosition(mapWidth / 2 - 10, mapHeight / 2 - 2); Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine(new string(' ', 20)); Console.SetCursorPosition(mapWidth / 2 - 10, mapHeight / 2 - 1); Console.WriteLine(" KONIEC GRY "); Console.SetCursorPosition(mapWidth / 2 - 10, mapHeight / 2); Console.WriteLine(new string(' ', 20)); string wynik = "WYNIK: " + newGame.Map.Snake.Score.ToString(), output = ""; output = new string(' ', (20 - wynik.Length) / 2) + wynik + new string(' ', (20 - wynik.Length) / 2 + (20 - wynik.Length) % 2); Console.SetCursorPosition(mapWidth / 2 - 10, mapHeight / 2 + 1); Console.WriteLine(output); Console.SetCursorPosition(mapWidth / 2 - 10, mapHeight / 2 + 2); Console.WriteLine(new string(' ', 20)); Console.ReadKey(); return; } Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.DarkMagenta; List <Entity> entities = newGame.Map.Entities; foreach (Entity entity in entities) { if (entity is Fruit) { builder.BuildFruit((Fruit)entity); } if (entity is Powerup) { builder.BuildPowerup((Powerup)entity); } if (entity is Obstacle) { builder.BuildObstacle((Obstacle)entity); } if (entity is Mouse) { builder.BuildMouse((Mouse)entity); } } builder.BuildSnake(snake); foreach (Point position in newGame.Map.ClearedPositions) { System.Diagnostics.Debug.WriteLine(position.ToString()); } builder.ClearMap(newGame.Map.ClearedPositions); Console.SetCursorPosition(2, mapHeight + 1); Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Cyan; Console.Write(new string(' ', mapWidth)); Console.SetCursorPosition(2, mapHeight + 1); Console.Write(newGame.Map.Snake.Score.ToString()); Console.SetCursorPosition(mapWidth - 6, mapHeight + 1); Console.ForegroundColor = ConsoleColor.Red; Console.Write(Convert.ToChar(9829)); Console.ForegroundColor = ConsoleColor.Black; Console.Write(" x " + newGame.Map.Snake.Lives.ToString()); if (newGame.Map.Snake.Effect.Variant != Effect.EffectVariant.None) { Console.SetCursorPosition(0, mapHeight + 3); Console.WriteLine(new string(' ', mapWidth)); if (newGame.Map.Snake.Effect.Duration > 0) { Console.SetCursorPosition(2, mapHeight + 3); Console.Write(newGame.Map.Snake.Effect.ToString()); Console.SetCursorPosition(mapWidth - 6, mapHeight + 3); Console.Write(newGame.Map.Snake.Effect.Duration.ToString()); } } newGame.Map.Update(); Thread.Sleep(wait); } } }
static void AddLength(ref Snake snake) { snake.Length += 1; snake.SnakePositions.Add(snake.SnakePositions[snake.SnakePositions.Count - 1]); }
static void Main(string[] args) { Console.SetWindowSize(80, 25); Walls walls = new Walls(80, 25); walls.Draw(); // Отрисовка точек Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(80, 25, '$'); Point food = foodCreator.CreateFood(); FoodCreator foodCreator2 = new FoodCreator(80, 25, 'R'); Point food2 = foodCreator2.CreateFood(); food.Draw("Blue"); Param settings = new Param(); Sound sound = new Sound(settings.GetResourceFolder()); sound.Play(); Sound sound1 = new Sound(settings.GetResourceFolder()); Scores score = new Scores(settings.GetResourceFolder()); Timer time = new Timer(); time.Counter(); while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { food = foodCreator.CreateFood(); food2 = foodCreator2.CreateFood(); sound1.PlayEat(); int check = score.currentFood(); int n; if (check > 0 && ((check + 10) % 100) == 0) { food2.Draw("Grey"); n = 1; } else if (check > 0 && check % 100 == 0) { food.Draw("Blue"); n = 2; } else { food.Draw("Blue"); n = 1; } score.UpCurrentPoints(n); score.ShowCurrentPoints(); } else { snake.Move(); } Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } } sound.Play("gameover"); time.CounterStatus(false); score.WriteGameOver(); score.userName(); score.WriteBestResult(); Console.ReadLine(); }
public abstract void UpdateSnakeHead(Snake snake);
public abstract void UpdateSnakeDirection(Snake snake);
static void Main(string[] args) { int TARGET_SNAKE_LENGTH; int result; TARGET_SNAKE_LENGTH = 10; Console.SetBufferSize(80, 25); Walls walls = new Walls(80, 25); walls.Draw(); ShowMenu(); while (true) { ConsoleKeyInfo action = Console.ReadKey(); if (action.Key == ConsoleKey.S) { Console.Clear(); walls.Draw(); result = 0; Point p = new Point(40, 12, '*'); Snake snake = new Snake(p, 5, Direction.LEFT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(80, 25, '&'); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { result = 0; break; } if (snake.GetLength() >= TARGET_SNAKE_LENGTH) { result = 1; break; } if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep(300); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } } Console.Clear(); walls.Draw(); ShowMenu(); if (result == 1) { ShowMessage("ПОЗДРАВЛЯЕМ! ВЫ ВЫИГРАЛИ."); } else { ShowMessage("ВЫ ПРОИГРАЛИ. ПОПРОБУЙТЕ ЕЩЁ РАЗ."); } } else if (action.Key == ConsoleKey.E) { break; } } /* * Console.SetCursorPosition(32, 12); * Console.ForegroundColor = ConsoleColor.Green; * Console.WriteLine("==============="); * Console.SetCursorPosition(33, 13); * Console.WriteLine("ИГРА ОКОНЧЕНА"); * Console.SetCursorPosition(32, 14); * Console.WriteLine("==============="); * Console.ReadLine(); */ }
static void Main(string[] args) { record = F2(); Console.CursorVisible = false; Console.SetWindowSize(60, 30); Console.SetCursorPosition(5, 10); Console.WriteLine("Enter your name : "); Console.ForegroundColor = ConsoleColor.Yellow; Console.SetCursorPosition(25, 10); string name = Console.ReadLine(); Console.Clear(); Console.WriteLine("If you want to continue type 2"); ConsoleKeyInfo ki = Console.ReadKey(); Console.Clear(); if (ki.Key == ConsoleKey.NumPad2) { snake = F4(); wall = F6(); } Thread thread = new Thread(func); thread.Start(); Console.SetCursorPosition(t, s); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Q"); while (true) { Console.SetCursorPosition(1, 27); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine(name); Console.SetCursorPosition(name.Length + 2, 27); record = Math.Max(record, score); Console.WriteLine("Score : " + score); Console.WriteLine("Record: " + record); ConsoleKeyInfo k = Console.ReadKey(); if (k.Key == ConsoleKey.UpArrow && direction != 2) { direction = 8; } if (k.Key == ConsoleKey.DownArrow && direction != 8) { direction = 2; } if (k.Key == ConsoleKey.RightArrow && direction != 4) { direction = 6; } if (k.Key == ConsoleKey.LeftArrow && direction != 6) { direction = 4; } if (k.Key == ConsoleKey.S) { F3(snake); F5(wall); } } }
public static void func() { while (true) { if ((t == snake.body[0].x && s == snake.body[0].y)) { snake.body.Add(new Point(t, s)); score++; speed = Math.Max(50, speed - 25); // t = rdm.Next(0, 54); //s = rdm.Next(0, 24); CreateFood(); if (score % 3 == 0) { level++; Console.Clear(); for (int i = 0; i < snake.body.Count; ++i) { snake.body[i].x = i + 10; snake.body[i].y = 15; } wall = new Wall(level); } } if (direction == 8) { snake.Move(0, -1); } if (direction == 2) { snake.Move(0, 1); } if (direction == 6) { snake.Move(1, 0); } if (direction == 4) { snake.Move(-1, 0); } if (snake.ColllisionWithWall(wall) || snake.Collision()) { Console.Clear(); Console.SetCursorPosition(20, 10); Console.WriteLine("GAME OVER!!!"); speed = 300; F1(record); score = 0; Console.ReadKey(); Console.Clear(); snake = new Snake(); level = 1; wall = new Wall(level); } Console.SetCursorPosition(t, s); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Q"); snake.Draw(); wall.Draw(); Thread.Sleep(speed); } }
static void Main(string[] args) { Console.SetWindowSize(100, 25); Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.DarkMagenta; Console.Clear(); Walls walls = new Walls(80, 25); walls.Draw(); //Otrisovka tochek Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(80, 25, '$'); Point food = foodCreator.CreateFood(); food.Draw(); Params settings = new Params(); Sounds sound = new Sounds(settings.GetResourceFolder()); sound.Play(); Sounds sound1 = new Sounds(settings.GetResourceFolder()); Sounds sound2 = new Sounds(settings.GetResourceFolder()); Score score = new Score(0, 1); score.ScoreWrite(); while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { score.ScoreUp(); score.ScoreWrite(); food = foodCreator.CreateFood(); food.Draw(); sound1.PlayEat(); } else { snake.Move(); } new Speed(score.GetScore()); if (Console.KeyAvailable) { snake.HandeKey(Console.ReadKey(true).Key); } } Console.Clear(); new WriteGameOver(); sound2.PlayGameOver(); Thread.Sleep(2000); new Results(score.GetScore(), settings.GetResourceFolder()); Console.Clear(); Best best = new Best(settings.GetResourceFolder()); Thread.Sleep(10000); }
public void OpenMenu() { int cursor = 0; List <string> menu = new List <string>(); menu.Add("Start game"); menu.Add("Exit"); while (true) { for (int i = 0; i < 2; i++) { if (i == cursor) { Console.ForegroundColor = ConsoleColor.DarkCyan; } else { Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(menu[i]); } ConsoleKeyInfo key = Console.ReadKey(); if (key.Key == ConsoleKey.UpArrow) { cursor--; } if (key.Key == ConsoleKey.DownArrow) { cursor++; } if (cursor == -1) { cursor = 1; } if (cursor == 2) { cursor = 0; } Console.Clear(); for (int i = 0; i < 2; i++) { if (i == cursor) { Console.ForegroundColor = ConsoleColor.DarkCyan; } else { Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(menu[i]); } Console.Clear(); if (key.Key == ConsoleKey.Enter) { if (cursor == 1) { break; } if (cursor == 0) { x = 1; y = 0; sc = 0; levelCount = 1; score = 0; wall = new Wall(); snake = new Snake(); playGame = true; moovable = false; food = new Food(); Thread t = new Thread(MoveSnakeThread); t.Start(); while (true) { ConsoleKeyInfo k = new ConsoleKeyInfo(); k = Console.ReadKey(); if (k.Key == ConsoleKey.Escape) { break; } if ((k.Key == ConsoleKey.UpArrow || k.Key == ConsoleKey.W)) { y = -1; x = 0; } if ((k.Key == ConsoleKey.DownArrow || k.Key == ConsoleKey.S)) { y = 1; x = 0; } if ((k.Key == ConsoleKey.LeftArrow || k.Key == ConsoleKey.A)) { x = -1; y = 0; } if (k.Key == ConsoleKey.RightArrow || k.Key == ConsoleKey.D) { x = 1; y = 0; } } Console.Clear(); playGame = false; } } } }
public static void Main(string[] args) { Console.Title = "Snake"; Console.SetWindowSize(60, 23); Console.SetCursorPosition(10, 10); Console.WriteLine("Hello!"); Wall wall = new Wall("lvl1.txt"); Snake snake = new Snake(); Food food = new Food(snake, wall, 1); int cnt = 1, cnt2 = 1, Max = 1, lvl = 1, Cl = 1; while (true) { snake.Move(); food.Erase(); snake.Game_Over(wall); if (snake.GM == 1) { Console.Clear(); Game_Over(cnt, Max); snake = new Snake(); food = new Food(snake, wall, 1); cnt = 1; cnt2 = 1; Cl = 1; } else if (food.body[0].x == snake.body[0].x && food.body[0].y == snake.body[0].y) { snake.body.Add(new Point(food.body[0].x, food.body[0].y)); food = new Food(snake, wall, 1); ++cnt; ++cnt2; } if (Max < cnt) { Max = cnt; } if (cnt2 % 10 == 0 && cnt2 <= 40) { if (lvl == 1) { wall = new Wall("lvl2.txt"); } else if (lvl == 2) { wall = new Wall("lvl3.txt"); } lvl++; if (lvl < 4) { snake = new Snake(); food = new Food(snake, wall, 1); } cnt2 = 1; Cl = 1; } // -> Print Snake if (Cl == 1) { Console.Clear(); wall.Draw(); Cl = 0; } snake.Draw(); food.Draw(); Console.SetCursorPosition(40, 10); Console.Write("Score: "); Console.WriteLine(cnt); Console.SetCursorPosition(40, 11); Console.Write("Maximum score: "); Console.WriteLine(Max); Console.SetCursorPosition(50, 20); // <- } }
public override void UpdatePreviousCoordinates(Snake snake) { snake.SetPreviousPositions(snake.GetXPosition(), snake.GetYPosition()); }
public override void Handle(Snake snake) { snake.State = this; snake.MoveUp(); }
static void Main(string[] args) { ConsoleKeyInfo cki = new ConsoleKeyInfo(); Field theField = new Field(); Fruit theFruit = new Apple(theField.GetFieldSize()); Game theGame = new Game(theField.GetFieldSize()); Snake snake = new Snake(new ConcreteStateDown()); theGame.DisplayWelcome(); snake.Request(); theField.SetField(); theFruit.CreateFruit(snake.GetXPosition(), snake.GetYPosition(), snake.GetSnakeBodyX(), snake.GetSnakeBodyY()); //main game play loop while (theGame.GetGameStatus() == true && cki.Key != ConsoleKey.Q) //Q can quit while game is in play { //if the Fruit has been eaten access this if block if (theFruit.GetIsEaten()) { theGame.UpdateGameSpeed(theFruit.GetUpdateSpeed()); snake.AddSnakeBody(); snake.UpdateAddToBody(); theGame.UpdateBiteCount(); theGame.SetGameScore(theFruit.GetFruitPoints()); if (theGame.GetBiteCount() % 5 == 0) { theFruit = new Berry(theField.GetFieldSize()); } else if (theGame.GetBiteCount() % 3 == 0) { theFruit = new Banana(theField.GetFieldSize()); } else { theFruit = new Apple(theField.GetFieldSize()); } theFruit.CreateFruit(snake.GetXPosition(), snake.GetYPosition(), snake.GetSnakeBodyX(), snake.GetSnakeBodyY()); theFruit.SetIsEaten(false); } //meta data theGame.DisplayGameScore(); theGame.DisplaySnakeBodyCount(snake.GetSnakeBodyCount()); //this while loop runs while a key HAS not been pressed && //the game status is still true meaning the snake has not died && //the fruit has not been eaten yet while (Console.KeyAvailable == false && theGame.GetGameStatus() == true && theFruit.GetIsEaten() == false) { //the game sleeps depending on the game speed //this is what produces the speed of the snake as it gets faster Thread.Sleep(theGame.GetGameSpeed()); snake.Request(); theGame.SetGameStatus(theField.DetectWallCollisions(snake.GetXPosition(), snake.GetYPosition()), snake.DetectBodyCollision()); theFruit.DetectFruitCollision(snake.GetXPosition(), snake.GetYPosition()); theGame.DisplaySnakePosition(snake.GetXPosition(), snake.GetYPosition()); //The Berry fruit has a time limit on it while the other fruits do not //therefore if it is a Berrt run this if block if (theFruit.GetIsBerry()) { theFruit.UpdateBerryMoves(); if (theFruit.GetBerryMoves() <= 0) { theFruit.RemoveOldFruit(); theFruit = new Apple(theField.GetFieldSize()); theFruit.CreateFruit(snake.GetXPosition(), snake.GetYPosition(), snake.GetSnakeBodyX(), snake.GetSnakeBodyY()); } } //with each iteration in this loop the snake must be drawn snake.DrawSnake(); } //when the while loop is exited due to a key press, that key needs to be read if (!theFruit.GetIsEaten() && theGame.GetGameStatus()) { cki = Console.ReadKey(true); snake.ReadKeyInput(cki); } //if the Game Status is false meaning the snake has died //give the user the choice of replaying //if they choose to the game is reset if (!theGame.GetGameStatus()) { theGame.DisplayReplayMessage(); theGame.GetReplayResponse(); if (theGame.GetReset()) { theFruit.RemoveOldFruit(); snake.RemoveSnakeBody(); theField.FixWall(snake.GetYPosition(), snake.GetXPosition()); snake.ResetSnake(new ConcreteStateDown(), theGame.GetReset()); theFruit = new Apple(theField.GetFieldSize()); theFruit.CreateFruit(snake.GetXPosition(), snake.GetYPosition(), snake.GetSnakeBodyX(), snake.GetSnakeBodyY()); } } } theGame.DisplayEnd(); }
public abstract void UpdatePreviousCoordinates(Snake snake);
public override void UpdateSnakeDirection(Snake snake) { snake.SetSnakeDirection("UP"); }
public abstract void Handle(Snake snake);
public static void Main(string[] args) { Console.SetBufferSize(80, 25); int score = 0; Walls walls = new Walls(80, 25); walls.Draw(); // Отрисовка точек Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); int speed = 500; FoodCreator foodCreator = new FoodCreator(80, 25, '$'); Point food = foodCreator.CreateFood(); while (walls.isHitFood(food)) { food = foodCreator.CreateFood(); break; } food.Draw(); /*FoodCreator foodCreator = new FoodCreator(80, 25, '$'); * Point food = foodCreator.CreateFood(); * food.Draw();*/ while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { food = foodCreator.CreateFood(); while (walls.isHitFood(food)) { food = foodCreator.CreateFood(); break; } food.Draw(); //food = foodCreator.CreateFood(); //food.Draw(); score++; speed = speed - 30; } else { snake.Move(); } System.Threading.Thread.Sleep(speed); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } } string str_score = Convert.ToString(score); WriteGameOver(str_score); Console.ReadLine(); }
public Game() { Snake = new Snake(); _rnd = new Random(); }
/// <summary> /// This method is called after the initial handshake. Its purpose is to create a /// snake object for the new client. It finds a random location that is sufficiently /// removed from the border and orients the snake in a random direction. /// </summary> /// <param name="playerName">Name of the player.</param> private void CreateNewPlayerSnake(string playerName) { lock (world) { int headX = randomGenerator.Next((int)0.12 * boardWidth, (int)(boardWidth - (boardWidth * 0.12))); int headY = randomGenerator.Next((int)0.12 * boardHeight, (int)(boardHeight - (boardHeight * 0.12))); int tailDirection = randomGenerator.Next(1, 4); bool done = false; while (!done) { done = true; //Sometimes throws exception for (int i = 0; i < 16; i++) { if (tailDirection == 1) { if (world.WorldMap[headX, headY - i].ID != -1) { done = false; } } else if (tailDirection == 2) { if (world.WorldMap[headX + i, headY].ID != -1) { done = false; } } else if (tailDirection == 3) { if (world.WorldMap[headX, headY + i].ID != -1) { done = false; } } else if (tailDirection == 4) { if (world.WorldMap[headX - i, headY].ID != -1) { done = false; } } } if (!done) { headX = randomGenerator.Next((int)0.12 * boardWidth, (int)(boardWidth - (boardWidth * 0.12))); headY = randomGenerator.Next((int)0.12 * boardHeight, (int)(boardHeight - (boardHeight * 0.12))); tailDirection = randomGenerator.Next(1, 4); } } Snake.Snake newPlayerSnake; if (tailDirection == 1) { newPlayerSnake = new Snake.Snake(new Point(headX, headY), new Point(headX, headY - 16), playerCount, playerName); } else if (tailDirection == 2) { newPlayerSnake = new Snake.Snake(new Point(headX, headY), new Point(headX + 16, headY), playerCount, playerName); } else if (tailDirection == 3) { newPlayerSnake = new Snake.Snake(new Point(headX, headY), new Point(headX, headY + 16), playerCount, playerName); } else { newPlayerSnake = new Snake.Snake(new Point(headX, headY), new Point(headX - 16, headY), playerCount, playerName); } world.Update(newPlayerSnake); } }
private void GenerateSnake() { Snake.GenerateSnake(); }
static void Main(string[] args) { Console.WindowHeight = 40; Console.WindowWidth = 70; Snake snake = new Snake(); int lvl = 1; Wall wall = new Wall(lvl); Console.CursorVisible = false; Thread thread = new Thread(func); thread.Start(); Random rnd = new Random(); int _x = rnd.Next(1, 70); int _y = rnd.Next(1, 20); Food food = new Food(_x, _y); while (true) { foreach (Point p in wall.body) { if (p.x == food.body[0].x && p.y == food.body[0].y) { rnd = new Random(); _x = rnd.Next(1, 70); _y = rnd.Next(1, 20); food = new Food(_x, _y); continue; } } for (int p = 1; p <= snake.body_cnt; p++) { if (snake.body[p].x == food.body[0].x && snake.body[p].y == food.body[0].y) { rnd = new Random(); _x = rnd.Next(1, 70); _y = rnd.Next(1, 20); food = new Food(_x, _y); continue; } } break; } int k = 1; while (!gameOver) { while (true) { ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.DownArrow && direction != 4) { direction = 3; } if (keyInfo.Key == ConsoleKey.UpArrow && direction != 3) { direction = 4; } if (keyInfo.Key == ConsoleKey.LeftArrow && direction != 1) { direction = 2; } if (keyInfo.Key == ConsoleKey.RightArrow && direction != 2) { direction = 1; } if (keyInfo.Key == ConsoleKey.R) { level = 1; snake = new Snake(); wall = new Wall(level); } if (keyInfo.Key == ConsoleKey.Escape) { gameOver = true; } foreach (Point p in wall.body) { if (p.x == snake.body[0].x && p.y == snake.body[0].y) { gameOver = true; } } for (int p = 1; p <= snake.body_cnt; p++) { if (snake.body[p].x == snake.body[0].x && snake.body[p].y == snake.body[0].y) { gameOver = true; } } if (food.body[0].x == snake.body[0].x && food.body[0].y == snake.body[0].y) { k++; snake.Add(); _x = rnd.Next(1, 70); _y = rnd.Next(1, 20); food = new Food(_x, _y); bool ok = true; while (ok) { foreach (Point p in wall.body) { if (p.x == food.body[0].x && p.y == food.body[0].y) { _x = rnd.Next(1, 70); _y = rnd.Next(1, 20); food = new Food(_x, _y); } } for (int p = 1; p <= snake.body_cnt; p++) { if (snake.body[p].x == food.body[0].x && snake.body[p].y == food.body[0].y) { _x = rnd.Next(1, 70); _y = rnd.Next(1, 20); food = new Food(_x, _y); } } ok = false; } } if (k % 5 == 0) { k = 1; lvl++; wall = new Wall(lvl); } } Console.Clear(); Console.ForegroundColor = ConsoleColor.White; Console.SetCursorPosition(35, 12); Console.WriteLine("GAME OVER!"); Console.ReadKey(); } }