public static void Main(string[] args) { Console.WindowWidth = ConsoleWidth; Console.WindowHeight = ConsoleHeight; Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.WindowWidth; Console.CursorVisible = false; KeyboardControls keyboard = new KeyboardControls(); keyboard.OnLeftPress += HandleOnLeftPress; keyboard.OnRightPress += HandleOnRightPress; keyboard.OnUpPress += HandleOnUpPress; keyboard.OnDownPress += HandleOnDownPress; keyboard.OnEscape += HandleOnEscape; bool placeFood = false; Food food = new Food(RandomNumber.Generate(0, ConsoleWidth), RandomNumber.Generate(0, ConsoleHeight), '+'); while (runGame) { Console.Clear(); keyboard.ProcessInput(); snake.Draw(); snake.Update(); food.Draw(); if (placeFood) { food = new Food(RandomNumber.Generate(0, ConsoleWidth), RandomNumber.Generate(0, ConsoleHeight), '+'); placeFood = false; } if (food.Position.X == snake.Body[0].Position.X && food.Position.Y == snake.Body[0].Position.Y) { Console.Beep(); snake.AddBodyPart(); placeFood = true; } if (snake.Body[0].Position.X < 0 || snake.Body[0].Position.X >= ConsoleWidth || snake.Body[0].Position.Y < 0 || snake.Body[0].Position.Y >= ConsoleHeight) { runGame = false; } for (int i = 1; i < snake.Body.Count; i++) { if (snake.Body[0].Position.X == snake.Body[i].Position.X && snake.Body[0].Position.Y == snake.Body[i].Position.Y) { runGame = false; break; } } Thread.Sleep(ThreadSpeed); } Console.WriteLine("Thanks for playing"); }
public void CreateFood(GameObjects gameObjects, int count) { int createdFood = 0; while(createdFood < count) { Food food = new Food(sym); food.CreatFood(); // Если еда не пересецается ни с одним из других объектов, то отрисуем их if (!gameObjects.Intersect(food) && !Intersect(food)) { this.gameObjects.Add(food); food.Draw(); createdFood++; } } }
public void CanEat() { if (worm.points[0].Equals(food.points[0])) { worm.points.Add(food.points[0]); food = new Food(); food.Generate(wall, worm); food.Draw(); score = score + 100; if (score == 500) { nextLevel = true; } } }
private void DrawAndMoveGameObjects() { while (true) { // move and other logic serpent.Clear(); serpent.Move(); CheckFoodCatch(); // draw serpent.Draw(); food.Draw(); // This parameter defines speed of movement Thread.Sleep(200); } }
static void Main(string[] args) { Worm worm = new Worm(); Food food = new Food(); Wall wall = new Wall(1); while (worm.isAlive) { Console.Clear(); worm.Draw(); food.Draw(); wall.Draw(); ConsoleKeyInfo pressedKey = Console.ReadKey(); switch (pressedKey.Key) { case ConsoleKey.UpArrow: worm.Move(0, -1); break; case ConsoleKey.DownArrow: worm.Move(0, 1); break; case ConsoleKey.LeftArrow: worm.Move(-1, 0); break; case ConsoleKey.RightArrow: worm.Move(1, 0); break; case ConsoleKey.Escape: worm.isAlive = false; break; } if (worm.CanEat(food)) { food = new Food(); } } }
public Game() { isAlive = true; gameLevel = GameLevel.First; worm = new Worm(new Point { X = 10, Y = 10 }, ConsoleColor.White, '*'); food = new Food(new Point { X = 20, Y = 10 }, ConsoleColor.White, '+'); food.Draw(); wall = new Wall(null, ConsoleColor.White, '#'); wall.LoadLevel(GameLevel.First); wall.Draw(); }
public void Countdown() { Console.Clear(); Snake.Draw("Green"); Food.Draw(); Board.Draw(); ShowLargeNumber(3, BoardWidth); Console.Clear(); Snake.Draw("Green"); Food.Draw(); Board.Draw(); ShowLargeNumber(2, BoardWidth); Console.Clear(); Snake.Draw("Green"); Food.Draw(); Board.Draw(); ShowLargeNumber(1, BoardWidth); Console.Clear(); Board.Draw(); }
protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); board.Draw(spriteBatch); player.Draw(spriteBatch); food.Draw(spriteBatch); spriteBatch.DrawString(font, "Score: " + score.ToString(), new Vector2(5, 5), Color.Black); //if game over draw reset text if (gameOver) { String resetStr = "Press SPACE to Reset"; Vector2 strSize = font.MeasureString(resetStr); spriteBatch.DrawString(font, resetStr, new Vector2((windowWidth - strSize.X) / 2, (windowHeight - strSize.Y) / 2), Color.Black); } spriteBatch.End(); base.Draw(gameTime); }
static void Main(string[] args) { FileStream fs = new FileStream("save.xml", FileMode.Create); fs.Close(); VeryBegin: Point p; int l = 1; Console.CursorVisible = false; /* while (true) { Console.Clear(); Console.WriteLine("Chose level (1-5)"); l = int.Parse(Console.ReadLine()); if (l > 0 && l <= 5) break; } */ Begin: dx = 0; dy = 0; Console.Clear(); int score = 0; Wall wall = new Wall(l); worm = new Worm(); Food food = new Food(); worm.Start(); way = "none"; bool f0 = true; while (f0 == true) { worm = new Worm(); food = new Food(); worm.Start(); f0 = false; for (int i = 0; i < wall.bricks.Count; i++) if (wall.bricks[i].Equals(worm.body[0])) f0 = true; } bool s0 = true; bool s1 = true; while (s0 == true || s1 == true) { worm = new Worm(); worm.Start(); s0 = false; s1 = false; for (int i = 0; i < wall.bricks.Count; i++) if (wall.bricks[i].Equals(worm.body[0])) s0 = true; if (worm.body[0].Equals(food.location)) s1 = true; } while (worm.isAlive) { p = new Point(worm.body[worm.body.Count - 1].x, worm.body[worm.body.Count - 1].y); worm.Draw(); food.Draw(); wall.Draw(); Console.WriteLine("\n\nScore: {0}", score); if (score >= 10) { Console.WriteLine("\n\n Next level availible"); } Thread.Sleep(200); worm.Move(dx, dy); Thread t = new Thread(new ThreadStart(Keys)); t.Start(); /*ConsoleKeyInfo pressedKey = Console.ReadKey(); switch (pressedKey.Key) { case ConsoleKey.UpArrow: if (way != "down" || worm.body.Count == 1) { worm.Move(0, -1); way = "up"; } break; case ConsoleKey.DownArrow: if (way != "up" || worm.body.Count == 1) { worm.Move(0, 1); way = "down"; } break; case ConsoleKey.LeftArrow: if (way != "right" || worm.body.Count == 1) { worm.Move(-1, 0); way = "left"; } break; case ConsoleKey.RightArrow: if (way != "left" || worm.body.Count == 1) { worm.Move(1, 0); way = "right"; } break; case ConsoleKey.Escape: worm.isAlive = false; Console.Clear(); goto End; case ConsoleKey.F5: Game game = new Game(food, wall, worm, score, l); Serialize(game); break; case ConsoleKey.F9: Game game2 = Deserialize(); food = game2.food; wall = game2.wall; worm = game2.worm; score = game2.score; l = game2.l; Console.Clear(); break; case ConsoleKey.OemPlus: if(score >=10) { l++; goto Begin; } break; case ConsoleKey.OemMinus: if (l > 1) { l--; goto Begin; } break; }*/ Console.SetCursorPosition(p.x, p.y); Console.Write(' '); for (int i = 0; i < wall.bricks.Count; i++) if (wall.bricks[i].Equals(worm.body[0])) { worm.isAlive = false; } for (int i = 1; i < worm.body.Count; i++) { if (worm.body[0].Equals(worm.body[i])) worm.isAlive = false; } if (worm.CanEat(food)) { score++; bool f1 = true; bool f2 = true; while (f1==true || f2==true) { f1 = false; f2 = false; food = new Food(); for (int i = 0; i < worm.body.Count; i++) if (worm.body[i].Equals(food.location)) f1=true; for (int i = 0; i < wall.bricks.Count; i++) if (wall.bricks[i].Equals(food.location)) f2 = true; } } } Console.Clear(); Console.WriteLine("GAME OVER\n\n Again?"); Console.ReadKey(); goto VeryBegin; End: Console.WriteLine("GAME OVER"); }
public static void Draw() { snake.Draw(); food.Draw(); Wall.Draw(); }
public void Draw() { w.Draw(); f.Draw(); b.Draw(); }
static void Main(string[] args) { Worm worm = new Worm(); Food food = new Food(); Wall wall = new Wall(1); worm.Start(); while (worm.isAlive) { Console.Clear(); worm.Draw(); food.Draw(); int currx = worm.Getheadx(); int curry = worm.Getheady(); int k = wall.Draw(currx, curry); if (k == 1) { break; } ConsoleKeyInfo pressedKey = Console.ReadKey(); switch (pressedKey.Key) { case ConsoleKey.F8: Game q = new Game(worm, wall, food); Serialize(q); break; case ConsoleKey.F9: Game h = Deserialize(); worm = h.worm; food = h.food; wall = h.wall; break; case ConsoleKey.UpArrow: worm.Move(0, -1); break; case ConsoleKey.DownArrow: worm.Move(0, 1); break; case ConsoleKey.LeftArrow: worm.Move(-1, 0); break; case ConsoleKey.RightArrow: worm.Move(1, 0); break; case ConsoleKey.Escape: worm.isAlive = false; break; } if (worm.CanEat(food)) { food = new Food(); } } Console.Clear(); Console.WriteLine("YOU LOSE"); }
static void Keys(Wall wall, Worm worm, Food food) { System system = new System(wall, worm); while (worm.isAlive) { Console.Clear(); worm.Draw(); food.Draw(); Console.ForegroundColor = ConsoleColor.Yellow; wall.Draw(); Console.WriteLine(" Your points:"); Console.WriteLine(Global.points); Console.WriteLine(" Your level:"); Console.WriteLine(Global.level); ConsoleKeyInfo pressedKey = Console.ReadKey(); switch (pressedKey.Key) { case ConsoleKey.UpArrow: worm.Move(0, -1); break; case ConsoleKey.DownArrow: worm.Move(0, 1); break; case ConsoleKey.LeftArrow: worm.Move(-1, 0); break; case ConsoleKey.RightArrow: worm.Move(1, 0); break; case ConsoleKey.Escape: Serialize(system); worm.isAlive = false; break; case ConsoleKey.Backspace: Console.Clear(); Console.WriteLine(Global.points); break; case ConsoleKey.Spacebar: Serialize(system); break; case ConsoleKey.F2: Console.Clear(); System system2 = Deserialize(); food.WhereisFood(system2.wall, system2.worm); Keys(system.wall, system.worm, food); break; } if (worm.IsDead(wall)) { worm.isAlive = false; } if (worm.body.Count > 3) { Global.level++; Global.points = +50; Worm newworm = new Worm(); Console.Clear(); Wall newwall = new Wall(Global.level); newwall.Draw(); Food newfood = new Food(); newfood.WhereisFood(newwall, newworm); worm.isAlive = true; Keys(newwall, newworm, newfood); } if (worm.CanEat(food)) { food = new Food(); food.WhereisFood(wall, worm); Global.points += 10; } Serialize(system); if (worm.IsDead(wall)) { Console.Clear(); Console.WriteLine(); Console.WriteLine("GAME OVER"); Console.WriteLine("Your points:"); Console.WriteLine(Global.points); Console.WriteLine("max level:"); Console.WriteLine(Global.level); } } }
public void DrawWorm(object sender, ElapsedEventArgs e) { if (points == 30) { points = 40; LoadLevel2(); } if (points == 70) { points = 80; LoadLevel3(); } if (points == 110) { points = 120; LoadLevelInf(); } DrawBar(); worm.Clear(); worm.Move(); worm.Draw(); if (worm.body[0].Equals(food.body[0])) { worm.body.Add(new Point { X = food.body[0].X, Y = food.body[0].Y }); points = points + 10; bool goodFood = false; while (!goodFood) { food = new Food(new Point { X = new Random().Next() % 57 + 1, Y = new Random().Next() % 15 + 6 }, ConsoleColor.Red, '+'); goodFood = true; foreach (Point p in worm.body) { if (food.body[0].Equals(p)) { goodFood = false; break; } } foreach (Point p in wall.body) { if (food.body[0].Equals(p)) { goodFood = false; break; } } } food.Draw(); } else { foreach (Point p in wall.body) { if (p.Equals(worm.body[0])) { Stop(); alive = false; DrawGameOver(); } } if (worm.body.Count > 1) { for (int i = 1; i < worm.body.Count; i++) { if (worm.body[0].Equals(worm.body[i])) { Stop(); alive = false; DrawGameOver(); } } } } if (points >= 110) { lev = "inf"; } else if (points >= 70) { lev = "3"; } else if (points >= 30) { lev = "2"; } }
public static void func() { while (!gameOver) { if (direction == 3) { snake.Move(0, 1); } if (direction == 4) { snake.Move(0, -1); } if (direction == 2) { snake.Move(-1, 0); } if (direction == 1) { snake.Move(1, 0); } 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, 30); food = new Food(_x, _y); continue; } } break; } 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); 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, 30); food = new Food(_x, _y); continue; } } break; } food = new Food(_x, _y); } if (k % 5 == 0) { k++; lvl++; wall = new Wall(lvl); } if (gameOver) { Console.Clear(); Console.ForegroundColor = ConsoleColor.White; Console.SetCursorPosition(screenwidth / 2, screenheight / 2); Console.WriteLine("GAME OVER!"); Console.SetCursorPosition(screenwidth / 2, screenheight / 2 + 1); Console.WriteLine("score:" + (k - lvl)); Console.ReadKey(); break; } Console.Clear(); snake.Draw(); wall.Draw(); food.Draw(_x, _y); Thread.Sleep(speed); } }
public void Run() { food.Generate(worm.body, wall.body); food.Draw(); }
static void Main(string[] args) { Thread t = new Thread(MoveSnakeThread); t.Start(); Snake snake = new Snake(); Food food = new Food(); Wall Wall = new Wall(); Console.CursorVisible = false; Console.SetWindowSize(103, 30); while (true) { snake.Draw(); food.Draw(); Wall.Draw(); Console.SetCursorPosition(0, 1); Console.WriteLine("Score :"); Console.WriteLine(snake.a); ConsoleKeyInfo btn = Console.ReadKey(); switch (btn.Key) { case ConsoleKey.UpArrow: direction = 4; break; case ConsoleKey.DownArrow: direction = 3; break; case ConsoleKey.LeftArrow: direction = 2; break; case ConsoleKey.RightArrow: direction = 1; break; } if (snake.body[0].x < 14) { snake.body[0].x = 96; } if (snake.body[0].x > 96) { snake.body[0].x = 14; } if (snake.body[0].y < 7) { snake.body[0].y = 21; } if (snake.body[0].y > 21) { snake.body[0].y = 7; } if (snake.GameOver(Wall)) { snake.a = 0; Console.Clear(); snake.body = new List <Point>(); snake.body.Add(new Point(16, 7)); snake.body.Add(new Point(15, 7)); snake.body.Add(new Point(14, 7)); } if (snake.CanEat(food)) { food.setRandomPosition(); } } }
private void timer1_Tick(object sender, EventArgs e) { if (snake.Head.data.HitBox.IntersectsWith(food.HitBox)) { x = rand.Next(30, canvas.Width - 30); y = rand.Next(30, canvas.Height - 30); if (x % 30 > 0) { while (x % 30 > 0) { x = rand.Next(30, canvas.Width - 30); } } if (y % 30 > 0) { while (y % 30 > 0) { y = rand.Next(30, canvas.Height - 30); } } food.X = x; food.Y = y; Snake Tail = snake.Tail.data; if (currentDirection == direction.left) { snakebody = new Snake(Tail.X + Tail.W, Tail.Y, 30, 30, 10, 3); } else if (currentDirection == direction.right) { snakebody = new Snake(Tail.X - Tail.W, Tail.Y, 30, 30, 10, 4); } else if (currentDirection == direction.up) { snakebody = new Snake(Tail.X, Tail.Y - Tail.W, 30, 30, 10, 1); } else if (currentDirection == direction.down) { snakebody = new Snake(Tail.X, Tail.Y + Tail.W, 30, 30, 10, 2); } snake.AddNodeToLast(snakebody); score += 10; label1.Text = $"Score: {score}"; } g.FillRectangle(Brushes.DimGray, 0, 0, canvas.Width, canvas.Height); pictureBox1.Image = canvas; CircularLinkedListNode <Snake> runner = snake.Head; for (int i = 0; i < snake.Count; i++) { runner.data.Draw(g); runner = runner.next; } food.Draw(g); if (snake.Head.data.X < 0 || snake.Head.data.Y < 0 || snake.Head.data.X + snake.Head.data.W > canvas.Width || snake.Head.data.Y + snake.Head.data.H > canvas.Height) { timer1.Enabled = false; restart = true; score = 0; MessageBox.Show("Game Over."); MessageBox.Show("Press any button to restart."); } else { runner = snake.Head; if (currentDirection == direction.left) { snake.Head.data.X -= snake.Head.data.S; } if (currentDirection == direction.right) { snake.Head.data.X += snake.Head.data.S; } if (currentDirection == direction.up) { snake.Head.data.Y -= snake.Head.data.S; } if (currentDirection == direction.down) { snake.Head.data.Y += snake.Head.data.S; } for (int i = snake.Count - 1; i > 0; i--) { if (i == 1) { runner.data.X = previousX; runner.data.Y = previousY; } if (snake.Count > 1) { runner.data.X = runner.prev.data.X; runner.data.Y = runner.prev.data.Y; } runner = runner.next; if (snake.Head.data.HitBox.IntersectsWith(runner.data.HitBox) && runner.data != snake.Head.next.data) { timer1.Enabled = false; restart = true; MessageBox.Show("Game Over."); MessageBox.Show("Press any button to restart."); } } previousX = snake.Head.data.X; previousY = snake.Head.data.Y; } }
static void Main(string[] args) { VeryBegin: int l = 1; /* * while (true) * { * Console.Clear(); * Console.WriteLine("Chose level (1-5)"); * l = int.Parse(Console.ReadLine()); * if (l > 0 && l <= 5) * break; * } */ Begin: int score = 0; Wall wall = new Wall(l); Worm worm = new Worm(); Food food = new Food(); worm.Start(); string way = "none"; bool f0 = true; while (f0 == true) { worm = new Worm(); food = new Food(); worm.Start(); f0 = false; for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].Equals(worm.body[0])) { f0 = true; } } } bool s0 = true; bool s1 = true; while (s0 == true || s1 == true) { worm = new Worm(); worm.Start(); s0 = false; s1 = false; for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].Equals(worm.body[0])) { s0 = true; } } if (worm.body[0].Equals(food.location)) { s1 = true; } } while (worm.isAlive) { Console.Clear(); worm.Draw(); food.Draw(); wall.Draw(); Console.WriteLine("\n\nScore: {0}", score); if (score >= 10) { Console.WriteLine("\n\n Next level availible"); } ConsoleKeyInfo pressedKey = Console.ReadKey(); switch (pressedKey.Key) { case ConsoleKey.UpArrow: if (way != "down" || worm.body.Count == 1) { worm.Move(0, -1); way = "up"; } break; case ConsoleKey.DownArrow: if (way != "up" || worm.body.Count == 1) { worm.Move(0, 1); way = "down"; } break; case ConsoleKey.LeftArrow: if (way != "right" || worm.body.Count == 1) { worm.Move(-1, 0); way = "left"; } break; case ConsoleKey.RightArrow: if (way != "left" || worm.body.Count == 1) { worm.Move(1, 0); way = "right"; } break; case ConsoleKey.Escape: worm.isAlive = false; Console.Clear(); goto End; case ConsoleKey.F5: Game game = new Game(food, wall, worm, score, l); Serialize(game); break; case ConsoleKey.F9: Game game2 = Deserialize(); food = game2.food; wall = game2.wall; worm = game2.worm; score = game2.score; l = game2.l; break; case ConsoleKey.OemPlus: if (score >= 10) { l++; goto Begin; } break; case ConsoleKey.OemMinus: if (l > 1) { l--; goto Begin; } break; } for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].Equals(worm.body[0])) { worm.isAlive = false; } } for (int i = 1; i < worm.body.Count; i++) { if (worm.body[0].Equals(worm.body[i])) { worm.isAlive = false; } } if (worm.CanEat(food)) { score++; bool f1 = true; bool f2 = true; while (f1 == true || f2 == true) { f1 = false; f2 = false; food = new Food(); for (int i = 0; i < worm.body.Count; i++) { if (worm.body[i].Equals(food.location)) { f1 = true; } } for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].Equals(food.location)) { f2 = true; } } } } } Console.Clear(); Console.WriteLine("GAME OVER\n\n Again?"); Console.ReadKey(); goto VeryBegin; End: Console.WriteLine("GAME OVER"); }
static void Main(string[] args) { Console.Clear(); Console.SetCursorPosition(35, 7); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Welcome To Snake Game"); Console.SetCursorPosition(20, 8); Console.WriteLine("Reach 110 score to complete level and complete 10 levels to finish game"); Console.SetCursorPosition(25, 9); Console.WriteLine("Please, Enter Your Name"); Console.SetCursorPosition(30, 10); string line = Console.ReadLine();//user's name Console.CursorVisible = false; Console.Clear(); snake.Head(); snake.Draw(); food.Draw(); wall.Draw(); s.Draw(score, level); Thread thread = new Thread(F); thread.Start(); while (true) { Console.ForegroundColor = ConsoleColor.Black; ConsoleKeyInfo key = Console.ReadKey(); if (key.Key == ConsoleKey.DownArrow && last != 2 && last != 1) { snake.Move(0, 1); last = 1; success = 1; } if (key.Key == ConsoleKey.UpArrow && last != 1 && last != 2) { snake.Move(0, -1); last = 2; success = 1; } if (key.Key == ConsoleKey.LeftArrow && last != 4 && last != 3) { snake.Move(-1, 0); last = 3; success = 1; } if (key.Key == ConsoleKey.RightArrow && last != 3 && last != 4) { snake.Move(1, 0); last = 4; success = 1; }//head's move if (key.Key == ConsoleKey.F2) { snake.Ser(); food.Ser(); s.Ser(score); s.Ser1(level); wall.Ser(); } if (key.Key == ConsoleKey.F3) { snake = snake.Des(); wall = wall.Des(); score = int.Parse(s.Des()); level = int.Parse(s.Des1()); food = food.Des(); Console.Clear(); food.Draw(); wall.Draw(); s.Draw(score, level); success = 1; Console.ReadKey(); } if (success == 1) { snake.Draw(); food.Draw(); if (snake.body.Count < 2) { wall.Draw(); } } success = 0; if (snake.CollisionWithFood(food)) { score = score + 10; // every food = 10 point if (score >= 110) { level++; if (level > 10) { Console.SetCursorPosition(30, 5); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Congratulations!!!"); Console.SetCursorPosition(30, 6); Console.WriteLine("You completed My Game"); Console.ReadKey(); return; } speed = 300; score = 0; last = 0; Console.Clear(); wall = new Wall(level); snake = new Snake(); snake.Head(); food = new Food(wall, snake); snake.Draw(); s.Draw(score, level);// if level up } food = new Food(wall, snake); food.Draw(); s.Draw(score, level);// score , level, and food point refresh } if (snake.CollisionWithBody() || snake.CollisionWithWall(wall)) { thread.Abort(); string ss = ""; Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Clear(); Console.SetCursorPosition(30, 7); Console.WriteLine("GAME OVER"); Console.SetCursorPosition(28, 8); Console.WriteLine("Your score is " + ((level - 1) * 110 + score)); Console.SetCursorPosition(27, 9); Console.WriteLine("press R to restrart or Q to quit"); int maxscore = 0; if (!File.Exists(line + ".txt")) { File.WriteAllText(line + ".txt", "0"); } ss = File.ReadAllText(line + ".txt"); Console.SetCursorPosition(28, 10); Console.WriteLine("Your Best score is " + ss); maxscore = int.Parse(ss); if (maxscore < score) { File.WriteAllText(line + ".txt", score.ToString()); }//Highscore score = 0; level = 1;//if Game Over speed = 300; while (true) { Console.ForegroundColor = ConsoleColor.Black; ConsoleKeyInfo kki = Console.ReadKey(); if (kki.Key == ConsoleKey.R) { thread = new Thread(F); thread.Start(); break; } if (kki.Key == ConsoleKey.Q) { return; }//Quit or Restart } wall = new Wall(level); snake = new Snake(); snake.Head(); food = new Food(wall, snake); Console.Clear(); s.Draw(score, level);//start game from the beginning snake.Draw(); wall.Draw(); food.Draw(); } Console.ForegroundColor = ConsoleColor.Black; } }
public void Draw() { worm.Draw(); food.Draw(); wall.Draw(); }
public static int last = 0;//if right , then no left public static void F() { while (true) { if (last == 1) { snake.Move(0, 1); } if (last == 2) { snake.Move(0, -1); } if (last == 3) { snake.Move(-1, 0); } if (last == 4) { snake.Move(1, 0); } snake.Draw(); if (snake.CollisionWithFood(food)) { speed = speed - 20; score = score + 10; // every food = 10 point if (score >= 110) { level++; if (level > 10) { Console.SetCursorPosition(30, 5); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Congratulations!!!"); Console.SetCursorPosition(30, 6); Console.WriteLine("You completed My Game"); Console.ReadKey(); return; } speed = 300; score = 0; last = 0; Console.Clear(); wall = new Wall(level); snake = new Snake(); snake.Head(); food = new Food(wall, snake); snake.Draw(); s.Draw(score, level);// if level up } food = new Food(wall, snake); food.Draw(); s.Draw(score, level);// score , level, and food point refresh } if (snake.CollisionWithBody() || snake.CollisionWithWall(wall)) { string ss = ""; Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Clear(); Console.SetCursorPosition(30, 7); Console.WriteLine("GAME OVER"); Console.SetCursorPosition(28, 8); Console.WriteLine("Your score is " + ((level - 1) * 110 + score)); Console.SetCursorPosition(27, 9); Console.WriteLine("press R to restrart or Q to quit"); Console.SetCursorPosition(28, 10); Console.WriteLine("Your Best score is " + ss); score = 0; level = 1;//if Game Over speed = 300; while (true) { Console.ForegroundColor = ConsoleColor.Black; ConsoleKeyInfo kki = Console.ReadKey(); if (kki.Key == ConsoleKey.R) { break; } if (kki.Key == ConsoleKey.Q) { return; }//Quit or Restart } wall = new Wall(level); snake = new Snake(); snake.Head(); food = new Food(wall, snake); Console.Clear(); s.Draw(score, level);//start game from the beginning snake.Draw(); wall.Draw(); food.Draw(); } Thread.Sleep(speed); } }
public void Process(ConsoleKeyInfo pressedButton) { switch (pressedButton.Key) { case ConsoleKey.UpArrow: worm.Clear(); worm.Move(0, -1); worm.Draw(); break; case ConsoleKey.DownArrow: worm.Clear(); worm.Move(0, 1); worm.Draw(); break; case ConsoleKey.LeftArrow: worm.Clear(); worm.Move(-1, 0); worm.Draw(); break; case ConsoleKey.RightArrow: worm.Clear(); worm.Move(1, 0); worm.Draw(); break; case ConsoleKey.Escape: break; case ConsoleKey.F2: worm.Save(); food.Save(); break; case ConsoleKey.F1: worm = worm.Load() as Worm; food = food.Load() as Food; break; } if (worm.body[0].Equals(food.body[0])) { worm.body.Add(new Point { X = food.body[0].X, Y = food.body[0].Y }); food.Draw(); } else { foreach (Point p in wall.body) { if (p.Equals(worm.body[0])) { Console.Clear(); Console.WriteLine("GAME OVER!!!"); isAlive = false; break; } } } }
static void Main(string[] args) { //Title of Applycation Console.Title = "Snake"; //Scripts Scripts.SizingWindows(80, out ushort dialogs_width, 25, out ushort dialogs_height); Properties.Settings.Default.DialogsWindowsWidth = dialogs_width; Properties.Settings.Default.DialogsWindowsHeight = dialogs_height; do { //MainMenu Dialogs.MainMenu(); // Initialization of Map Map map = new Map(Properties.Settings.Default.MapWidth, Properties.Settings.Default.MapHeight, Properties.Settings.Default.MapBGColor, Properties.Settings.Default.MapFGColor, 1, 0, 1, 0); Scripts.InfoTable(Properties.Settings.Default.InfoScoreCount, Properties.Settings.Default.InfoScorePoint); Console.CursorLeft += 1; map.Sides(); Properties.Settings.Default.MapLeftBound = map.LeftBound; Properties.Settings.Default.MapTopBound = map.TopBound; // Initialization of Bound Bound frame = new Bound(Properties.Settings.Default.MapLeftBound, Properties.Settings.Default.MapTopBound, Properties.Settings.Default.MapWidth, Properties.Settings.Default.MapHeight, Properties.Settings.Default.BoundHSymbol, Properties.Settings.Default.BoundVSymbol, Properties.Settings.Default.BoundBGColor, Properties.Settings.Default.BoundFGColor); frame.Draw(); // Initialization of Snake Point startp = new Point((ushort)(Properties.Settings.Default.MapLeftBound + 1), (ushort)(Properties.Settings.Default.MapWidth - 1), (ushort)(Properties.Settings.Default.MapTopBound + 1), (ushort)(Properties.Settings.Default.MapHeight - 1)); Snake snake = new Snake(Properties.Settings.Default.SnakeSymbol, Properties.Settings.Default.SnakeLength, startp, Properties.Settings.Default.SnakeSpeed, Scripts.RandomDirection(startp), Properties.Settings.Default.SnakeColor); snake.Boost = Properties.Settings.Default.SnakeBoost; snake.Draw(); // Initialization of Food Food food = new Food((ushort)(Properties.Settings.Default.MapLeftBound + 1), (ushort)(Properties.Settings.Default.MapWidth - 1), (ushort)(Properties.Settings.Default.MapTopBound + 1), (ushort)(Properties.Settings.Default.MapHeight - 1), Properties.Settings.Default.FoodSymbol, Properties.Settings.Default.FoodColor); food = new Food(food.Bypass(snake)); food.Draw(); // Initialization of Variables ConsoleKeyInfo keys; bool escape = false; ushort point = 0; // Initialization Process Thread.Sleep(250); while (snake.TouchSelf() == false && snake.TouchBounds(map.LeftBound, map.Width, map.TopBound, map.Height) == false) { if (Console.KeyAvailable) { keys = Console.ReadKey(true); if (Scripts.EscapeGame(keys.Key) == true) { escape = true; break; } snake.Reaction(keys.Key); } if (snake.Eat(food)) { food = new Food(food.Bypass(snake)); food.Draw(); Scripts.InfoTable(Properties.Settings.Default.InfoScoreCount, Properties.Settings.Default.InfoScorePoint, ++point); snake.Booster(5); } snake.Move(); } //Game Over if (escape == false) { Dialogs.GameOverLogo(); } } while (true); }
public void Play(int speed) { Console.BackgroundColor = ConsoleColor.Cyan; Console.ForegroundColor = ConsoleColor.DarkBlue; Console.Clear(); snake.DrawOnLoad(); level.LoadLevel(levelNumber); do { food.Draw(); } while (snake.getHistory().Contains(new Tuple <int, int>(food.x_position, food.y_position)) || level.walls[food.x_position, food.y_position] == 1); food.Show(); do { while (!Console.KeyAvailable && snake.isAlive()) { Console.SetCursorPosition(75, 31); if (score < 10) { Console.Write("0"); } Console.Write(score); if (snake.X_position == food.x_position && snake.Y_position == food.y_position) { snake.Eat(food); score++; if (score == 20) { score = 0; ChangeLevel(); Console.Clear(); level.LoadLevel(levelNumber); snake = new Snake(1, 1); } Console.SetCursorPosition(75, 31); Console.Write(score); do { food.Draw(); } while (level.walls[food.x_position, food.y_position] == 1 || snake.getHistory().Contains(new Tuple <int, int>(food.x_position, food.y_position))); food.Show(); } if (level.walls[snake.X_position, snake.Y_position] == 1) { snake.Kill(); break; } snake.Move(snake.prev); if (snake.prev == Direction.LEFT || snake.prev == Direction.RIGHT) { Thread.Sleep(333 - 27 * speed); } else if (snake.prev == Direction.TOP || snake.prev == Direction.BOTTOM) { Thread.Sleep(500 - 40 * speed); } snake.Draw(); } if (snake.isAlive()) { ConsoleKeyInfo csi = Console.ReadKey(true); switch (csi.Key) { case ConsoleKey.UpArrow: if (snake.prev != Direction.BOTTOM) { snake.prev = Direction.TOP; } break; case ConsoleKey.DownArrow: if (snake.prev != Direction.TOP) { snake.prev = Direction.BOTTOM; } break; case ConsoleKey.LeftArrow: if (snake.prev != Direction.RIGHT) { snake.prev = Direction.LEFT; } break; case ConsoleKey.RightArrow: if (snake.prev != Direction.LEFT) { snake.prev = Direction.RIGHT; } break; case ConsoleKey.Escape: Serialize(); snake.Kill(); break; } } } while (snake.isAlive()); Serialize_Record(); }
public bool Run() { while (GameRunning) { if (GameMode == GameMode.SinglePlayer) { if (Snake.CheckBoardCollision(Board)) { GameRunning = false; this.Winner = "Someone Else"; break; } if (Snake.CheckTailCollision()) { GameRunning = false; this.Winner = "Someone Else"; break; } } //-------------------------------------------- // Check for collision with tail and board //-------------------------------------------- if (GameMode == GameMode.SnakeVsApple) { if (Snake.CheckBoardCollision(Board)) { GameRunning = false; this.Winner = "Apple"; break; } if (Snake.CheckTailCollision()) { GameRunning = false; this.Winner = "Apple"; break; } } else if (GameMode == GameMode.SnakeVsSnake) { //-------------------------------------------- // Check for collision on Snake Player Two //-------------------------------------------- if (SnakePlayerTwo.CheckBoardCollision(Board)) { GameRunning = false; this.Winner = "Player 1"; break; } if (SnakePlayerTwo.CheckTailCollision()) { GameRunning = false; this.Winner = "Player 1"; break; } if (SnakePlayerTwo.CheckSnakeCollision(Snake)) { GameRunning = false; this.Winner = "Player 1"; break; } //-------------------------------------------- // Check for collision with other Snake //-------------------------------------------- //if (Snake.CheckSnakeCollision(SnakePlayerTwo)) //{ // GameRunning = false; // this.Winner = "Player 2"; // break; //} } //-------------------------------------------- // Getting movement direction //-------------------------------------------- if (Console.KeyAvailable) { Direction = Joystick.SetKeyDirection(SnakeDirection); if (Direction == KeyDirection.Up || Direction == KeyDirection.Down || Direction == KeyDirection.Left || Direction == KeyDirection.Right) { SnakeDirection = Direction; } else { AppleDirection = Direction; } } //-------------------------------------------- // Eating of the apple //-------------------------------------------- if (GameMode == GameMode.SnakeVsApple) { SnakeEnergy = Snake.GetEnergy(); IsFoodEaten = Snake.Eat(Apple); } else if (GameMode == GameMode.SinglePlayer) { IsFoodEaten = Snake.Eat(Food); } else if (GameMode == GameMode.SnakeVsSnake) { OnePlayerFoodIsEaten = Snake.Eat(Food); TwoPlayerFoodIsEaten = SnakePlayerTwo.Eat(Food); if (OnePlayerFoodIsEaten) { Score++; IsFoodEaten = true; } if (TwoPlayerFoodIsEaten) { TwoPlayerScore++; IsFoodEaten = true; } } Apple.EraseOldApple(); if (GameMode == GameMode.SnakeVsApple) { Snake.Move(SnakeDirection, IsFoodEaten); AppleDirection = Apple.Move(AppleDirection); } else if (GameMode == GameMode.SnakeVsSnake) { SnakeDirectionPlayerTwo = SnakePlayerTwo.TranslateAppleDirectionToSnake(AppleDirection, SnakeDirectionPlayerTwo); Snake.Move(SnakeDirection, OnePlayerFoodIsEaten); SnakePlayerTwo.Move(SnakeDirectionPlayerTwo, TwoPlayerFoodIsEaten); } else if (GameMode == GameMode.SinglePlayer) { Snake.Move(SnakeDirection, IsFoodEaten); } if (IsFoodEaten) { if (GameMode == GameMode.SnakeVsApple) { Apple.LoseLife(); Apple.MakeFood(BoardWidth, BoardHeight); Energy = Apple.GetEnergy(); Snake.GetEnergyFromApple(Energy); } else if (GameMode == GameMode.SnakeVsSnake) { Food.MakeFood(BoardWidth, BoardHeight); IsFoodEaten = false; } else { Score++; Food.MakeFood(BoardWidth, BoardHeight); } } //-------------------------------------------- // Drawing the graphics in both modes //-------------------------------------------- Snake.Draw("Green"); //-------------------------------------------- // Drawing different for Two vs One Player mode //-------------------------------------------- if (GameMode == GameMode.SnakeVsApple) { Apple.RottTheApple(); Energybar.Draw(BoardHeight, SnakeEnergy); Snake.LoseEnergy(); Apple.Draw(); Apple.DrawLifes(BoardHeight); } else if (GameMode == GameMode.SnakeVsSnake) { SnakePlayerTwo.Draw("Magenta"); Food.Draw(); } else { Food.Draw(); ShowScore(Score, BoardHeight); } if (SnakeEnergy < 1 && GameMode == GameMode.SnakeVsApple) { this.GameRunning = false; this.Winner = "Apple"; } if (Apple.GetLifes() < 1 && GameMode == GameMode.SnakeVsApple) { this.GameRunning = false; this.Winner = "Snake"; } //-------------------------------------------- // Timer for loop //-------------------------------------------- Thread.Sleep(100 - Speed); } Console.Clear(); Console.ForegroundColor = ConsoleColor.White; GameOver(this.Winner); bool playAgain = AskIfPlayAgain(); return(playAgain); }
//static int qm, ql, qcnt, qs, qd, qspeed, qsp; static void Game() { while (!gameover) { if (cnt == 4) { level++; map++; if (map > 4) { map = 1; } cnt = 0; Console.Clear(); wall = new Wall(map); } if (score % 4 == 0 && score != 0) { if (sp == 0) { speed = speed - 10; sp++; } } if (score % 4 == 1) { sp = 0; } if (direction == 1) { snake.Move(0, -1); } if (direction == 2) { snake.Move(0, 1); } if (direction == 3) { snake.Move(1, 0); } if (direction == 4) { snake.Move(-1, 0); } while (food.OnSnake(food.location.x, food.location.y, snake) || food.OnWall(food.location.x, food.location.y, wall)) { food.SetRandomPosition(); } if (snake.Eat(food)) { cnt++; score++; } if (snake.Collision(wall)) { Console.Clear(); Console.SetCursorPosition(30, 10); Console.WriteLine("GAME OVER"); Console.SetCursorPosition(30, 11); Console.WriteLine("press SPACE to start over"); Console.SetCursorPosition(30, 12); Console.WriteLine("press ESC to exit"); Console.SetCursorPosition(30, 13); Console.WriteLine("press them twice, 'cause i have no idea how to fix that"); ConsoleKeyInfo k = Console.ReadKey(); if (k.Key == ConsoleKey.Escape) { gameover = true; break; } if (k.Key == ConsoleKey.Spacebar) { Console.Clear(); snake = new Snake(); direction = 0; cnt = 0; map = 1; level = 1; score = 0; speed = 150; sp = 0; wall = new Wall(map); } } //Console.Clear(); snake.Draw(); food.Draw(); wall.Draw(); Console.SetCursorPosition(10, 25); Console.WriteLine("level " + level); Console.SetCursorPosition(10, 26); Console.WriteLine("score " + score); Console.SetCursorPosition(75, 4); Console.WriteLine("use DIRECTIONAL KEYS to move"); Console.SetCursorPosition(75, 6); Console.WriteLine("press Q to save"); Console.SetCursorPosition(75, 8); Console.WriteLine("press W to load (works wierdly)"); Console.SetCursorPosition(75, 10); Console.WriteLine("press ESC to exit"); Thread.Sleep(speed); } }
public void Draw() { food.Draw(); Console.ForegroundColor = ConsoleColor.Blue; Console.SetCursorPosition(15, 24); Console.WriteLine("SCORE:{0}", score); wall.Draw(); while (true) { worm.Move(); Console.ForegroundColor = ConsoleColor.Blue; Console.SetCursorPosition(15, 24); Console.WriteLine(" "); Console.SetCursorPosition(15, 24); Console.WriteLine("SCORE:{0}", score); if (worm.body[0].Equals(food.body[0])) { worm.body.Add(new Point { X = food.body[0].X, Y = food.body[0].Y }); food.body[0] = food.CreateFood(wall.body, worm.body); score++; } else { foreach (Point p in wall.body) { if (p.Equals(worm.body[0])) { Console.Clear(); Console.SetCursorPosition(10, Game.boardH / 2); Console.WriteLine("GAME OVER!!!"); isAlive = false; Console.ForegroundColor = ConsoleColor.Black; Stop(); } } } if (score == 3) { wall.CleanBody(); interval = 200; gameLevel = GameLevel.Second; wall.LoadLevel(GameLevel.Second); Console.Clear(); wall.Draw(); food.body[0] = food.CreateFood(wall.body, worm.body); worm.body.Clear(); worm.body.Add(new Point { X = 24, Y = 16 }); score++; continue; } worm.Draw(); food.Draw(); Thread.Sleep(Game.interval); } }
static void Main(string[] args) { Console.CursorVisible = false; Console.SetWindowSize(120, 30); int lev = 1; int counter = 0; Snake snake = new Snake(); Food food = new Food(); Wall wall = new Wall(lev); while (true) { Console.SetCursorPosition(0, 0); Console.ForegroundColor = ConsoleColor.Blue; Console.BackgroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; Console.Write(counter + snake.cnt); ConsoleKeyInfo btn = Console.ReadKey(); switch (btn.Key) { case ConsoleKey.UpArrow: snake.Move(0, -1); break; case ConsoleKey.DownArrow: snake.Move(0, 1); break; case ConsoleKey.LeftArrow: snake.Move(-1, 0); break; case ConsoleKey.RightArrow: snake.Move(1, 0); break; } if (snake.body[0].x > 119) { snake.body[0].x = 0; } if (snake.body[0].x < 0) { snake.body[0].x = 119; } if (snake.body[0].y > 29) { snake.body[0].y = 0; } if (snake.body[0].y < 0) { snake.body[0].y = 29; } if (snake.Eat(food)) { food.SetPosition(wall); } if (snake.cnt == lev * 2) { lev++; wall = new Wall(lev); counter += snake.cnt; snake.cnt = 0; Console.SetCursorPosition(20, 20); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("GOOD JOB! " + "PRESS ANY KEY TO PASS TO THE NEXT LEVEL"); Console.ReadKey(); Console.Clear(); } if (snake.Colision() == true || snake.ColisionWithWall(wall) == true) { Console.Clear(); Console.SetCursorPosition(15, 15); Console.WriteLine("GAME OVER!" + " PRESS ANY KEY TPO RESTART"); Console.ReadKey(); Console.Clear(); snake = new Snake(); lev = 1; wall = new Wall(lev); } snake.Draw(); food.Draw(); wall.Draw(); //Console.Clear(); } }
private void FormMain_Paint(object sender, PaintEventArgs e) { graphics = CreateGraphics(); snake.Draw(graphics); food.Draw(graphics); }
static void Main(string[] args) { int l; while (true) { Console.Clear(); Console.WriteLine("Chose level (1-5)"); l = int.Parse(Console.ReadLine()); if (l > 0 && l <= 5) { break; } } Wall wall = new Wall(l); Worm worm = new Worm(); Food food = new Food(); string way = "none"; bool f0 = true; while (f0 == true) { worm = new Worm(); food = new Food(); f0 = false; for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].Equals(worm.body[0])) { f0 = true; } } } bool s0 = true; bool s1 = true; while (s0 == true || s1 == true) { worm = new Worm(); s0 = false; s1 = false; for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].Equals(worm.body[0])) { s0 = true; } } if (worm.body[0].Equals(food.location)) { s1 = true; } } while (worm.isAlive) { Console.Clear(); worm.Draw(); food.Draw(); wall.Draw(); ConsoleKeyInfo pressedKey = Console.ReadKey(); switch (pressedKey.Key) { case ConsoleKey.UpArrow: if (way != "down" || worm.body.Count == 1) { worm.Move(0, -1); way = "up"; } break; case ConsoleKey.DownArrow: if (way != "up" || worm.body.Count == 1) { worm.Move(0, 1); way = "down"; } break; case ConsoleKey.LeftArrow: if (way != "right" || worm.body.Count == 1) { worm.Move(-1, 0); way = "left"; } break; case ConsoleKey.RightArrow: if (way != "left" || worm.body.Count == 1) { worm.Move(1, 0); way = "right"; } break; case ConsoleKey.Escape: worm.isAlive = false; break; } for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].x == worm.body[0].x && wall.bricks[i].y == worm.body[0].y) { worm.isAlive = false; } } for (int i = 1; i < worm.body.Count; i++) { if (worm.body[0].x == worm.body[i].x && worm.body[0].y == worm.body[i].y) { worm.isAlive = false; } } if (worm.CanEat(food)) { bool f1 = true; bool f2 = true; while (f1 == true || f2 == true) { f1 = false; f2 = false; food = new Food(); for (int i = 0; i < worm.body.Count; i++) { if (worm.body[i].Equals(food.location)) { f1 = true; } } for (int i = 0; i < wall.bricks.Count; i++) { if (wall.bricks[i].Equals(food.location)) { f2 = true; } } } } } Console.Clear(); Console.WriteLine("GAME OVER"); }