public void makemove(int addx, int addy, int szx, int szy, food Food) { pair t = new pair((body[0].x + addx + szx) % szx, (body[0].y + addy + szy) % szy); if (badmove(t)) { lose = true; return; } bool bigger = false; int pos = -1; for (int i = 0; i < Food.body.Count; ++i) if (Food.body[i].x == t.x && Food.body[i].y == t.y) { bigger = true; pos = i; } pair v = new pair(); v = body[body.Count - 1]; for (int i = body.Count - 1; i > 0; --i) body[i] = body[i - 1]; if (bigger) { body.Add(v); Food.body.Remove(Food.body[pos]); pair w = new pair((getrand() % szx + szx) % szx, (getrand() % szy + szy) % szy); Food.add(w.x, w.y); } body[0] = t; }
static bool playlevel(int level) { Console.SetWindowSize(szx, szy); Console.Clear(); Console.WriteLine("Welcome to level {0}", level); Console.ReadKey(); Console.SetCursorPosition(1, 1); snake dragon = new snake(1, 1); Wall = new wall(); Food = new food(); for(int i = 0; i < level+1; ++i) { pair t = new pair((getrand() % szx + szx) % szx, (getrand() % szy + szy) % szy); Food.add(t.x, t.y); } for (int i = 0; i < 4 * level; ++i) { pair t = new pair((getrand() % szx + szx) % szx, (getrand() % szy + szy) % szy); while(t.x == 1 && t.y == 1) t = new pair((getrand() % szx + szx) % szx, (getrand() % szy + szy) % szy); Wall.add(t.x, t.y); } Draw risovanie = new Draw(dragon, Wall, Food); while (!risovanie.dragon.lose) { ConsoleKeyInfo button = Console.ReadKey(); if(button.Key == ConsoleKey.UpArrow) { dragon.makemove(0, -1, szx, szy, Food); } else if(button.Key == ConsoleKey.DownArrow) { dragon.makemove(0, +1, szx, szy, Food); } else if (button.Key == ConsoleKey.RightArrow) { dragon.makemove(1, 0, szx, szy, Food); } else if (button.Key == ConsoleKey.LeftArrow) { dragon.makemove(-1, 0, szx, szy, Food); } if (dragon.body.Count % (level + 5) == 0) return true; risovanie.Print(); } if(dragon.lose) { Console.Clear(); Console.WriteLine("You Lost"); Console.ReadKey(); return false; } return true; }
public bool badmove(pair w) { for(int i = 0; i < Program.Wall.body.Count; ++i) { if (Program.Wall.body[i].x == w.x && Program.Wall.body[i].y == w.y) return true; } for(int i = 0; i < body.Count; ++i) { if (body[i].x == w.x && body[i].y == w.y) return true; } return false; }
public void add(int x, int y) { pair t = new pair(x, y); body.Add(t); }