public void createApple() { Random rnd = new Random(); int xRan = rnd.Next(3, (this.Width - 90) / 30); int yRan = rnd.Next(3, (this.Height - 90) / 30); Apple snakeFood = new Apple(xRan * 30, yRan * 30); Button appleBtn = new Button(); appleBtn.Left = snakeFood.X; appleBtn.Top = snakeFood.Y; appleBtn.Width = 30; appleBtn.Height = 30; appleBtn.FlatStyle = FlatStyle.Flat; appleBtn.BackColor = Color.Red; Controls.Add(appleBtn); }
/// <summary> /// Default constructor. /// </summary> public GameViewModel() { // Initialize player and apple Snake = new Snake(); Apple = new Apple(); // Initialize collection SnakeBodyParts = new AsyncObservableCollection <SnakeSquare>(); // Create new timer Timer = new Timer(100); // Invoke Move() after interval time Timer.Elapsed += new ElapsedEventHandler(CheckMove); Timer.Enabled = true; // Create command KeyPressedCommand = new RelayParameterCommand((parameter) => KeyPressed(parameter)); }
private void SnakeMovement(Coordinates newCoordinates) { Coordinates newSnakeHeadPosition = newCoordinates; Coordinates snakeElementToRemove = Snake.Body[0]; Snake.Body.Remove(snakeElementToRemove); Snake.Body.Add(newSnakeHeadPosition); PrintSnake(); Console.SetCursorPosition(snakeElementToRemove.X, snakeElementToRemove.Y); Console.Write(" "); if (currentApple.X == Snake.Body[Snake.Body.Count - 1].X && currentApple.Y == Snake.Body[Snake.Body.Count - 1].Y) { Snake.Body.Add(newCoordinates); currentApple = GenerateApple(); } }
static void GameLogic(int dir) { if (gameOver) { return; } ChangeSnakeDir(dir); if (snake.dir == 0) { return; } int x = snake.bodies.First.Value.x; int y = snake.bodies.First.Value.y; switch (snake.dir) { case 1: y -= 1; break; case 2: y += 1; break; case 3: x -= 1; break; case 4: x += 1; break; default: break; } if (x < 0 || x >= W || y < 0 || y >= H) { gameOver = true; return; } // 链表移动 snake.bodies.RemoveLast(); snake.bodies.AddFirst(new Pos(x, y)); if (snake.bodies.First.Value.Equals(apple.pos)) { // apple = new Apple(); //apple.pos = new Pos(random.Next(0, W), random.Next(0, H)); int n = W * H - snake.bodies.Count; int rand = random.Next(0, n); bool[,] temp = new bool[H, W]; LinkedListNode <Pos> p = snake.bodies.First; while (p != null) { temp[p.Value.y, p.Value.x] = true; p = p.Next; } for (int i = 0; i < H; i++) { bool flag = false; for (int j = 0; j < W; j++) { if (!temp[i, j]) { if (rand == 0) { apple.pos = new Pos(j, i); flag = true; break; } rand -= 1; } } if (flag) { break; } } // 蛇变长 var tail = snake.bodies.Last.Value; snake.bodies.AddLast(tail); } Debug.WriteLine("snake:{0} {1}", x, y); }
private void eat(Apple food) { food.getEaten(); grow(); applesEaten++; }
public void Render() { /*---------------add Apple to 'myCanvas.Children' if needed--------------*/ if (apple == null) { apple = new Apple(rnd.Next(0, 26), rnd.Next(0, 26)); } if (mycanvas.Children.Contains(apple.Shape)) { mycanvas.Children.Remove(apple.Shape); } apple.Setfoodposition(); mycanvas.Children.Add(apple.Shape); /*---------------set Position of snakeElements --------------*/ xtmp = snakebody[0].X; ytmp = snakebody[0].Y; switch (snakebody[0].Direction) { case 0: { break; } default: { for (int i = snakebody.Count - 1; i > 0; i--) { snakebody[i].Direction = snakebody[i - 1].Direction; if (xtmp == snakebody[i].X && ytmp == snakebody[i].Y) { snakebody[i].X = snakebody[i - 1].X + ((snakebody[i - 1].Direction == left) ? snakebody[i - 1].Rect.Width : ((snakebody[i - 1].Direction == right) ? -snakebody[i - 1].Rect.Width : 0)); snakebody[i].Y = snakebody[i - 1].Y + ((snakebody[i - 1].Direction == up) ? snakebody[i - 1].Rect.Height : ((snakebody[i - 1].Direction == down) ? -snakebody[i - 1].Rect.Height : 0)); } } break; } } /*---------------remove snakeElements from and add them again to 'myCanvas.Children'--------------*/ if (mycanvas.Children.Count != 0) { foreach (SnakeElem snkEl in snakebody) { if (mycanvas.Children.Contains(snkEl.Rect)) { mycanvas.Children.Remove(snkEl.Rect); } } foreach (SnakeElem snk in snakebody) { foreach (SnakeElem s in snakebody) { Canvas.SetLeft(s.Rect, s.X); Canvas.SetTop(s.Rect, s.Y); } if (!(mycanvas.Children.Contains(snk.Rect))) { mycanvas.Children.Add(snk.Rect); } } } }
public static void Display(this Apple apple) { Console.SetCursorPosition(apple.X, apple.Y); Console.Write("@"); }
static void Play(int sizeX, int sizeY, int speed, bool loop) { int score = 0; //Initialize playing area InitUi(sizeX, sizeY); //Create snake from snake.cs and set it's bounds so it dies when is leaves the play area var snake = new Snake(sizeX, sizeY) { Loop = loop }; //Create a new thread to take in and pass controls to the snake, start it. var controlThread = new Thread(() => Controls(snake, Key.Up, Key.Down, Key.Left, Key.Right)); controlThread.SetApartmentState(ApartmentState.STA); controlThread.Start(); //Make new apples until Apple is not inside of snake var apple = new Apple(sizeX, sizeY); while (snake.IsTouching(apple.X, apple.Y)) { apple = new Apple(sizeX, sizeY); } DateTime sinceLastTick = DateTime.Now; Thread.Sleep(500); int ticks = 0; while (!snake.Dead) { if (DateTime.Now - sinceLastTick > TimeSpan.FromMilliseconds(1000 / speed)) { sinceLastTick = DateTime.Now; snake.Tick(); } //If snake isn't touching apple, skip back to start of while loop if (!snake.IsTouching(apple.X, apple.Y)) { continue; } //If it is touching apple snake.Length++; //Add to the score based on a basic algorithm taking into account size of play area and speed score += (int)Math.Ceiling((speed ^ 2 * 1000) / Math.Sqrt(sizeX * sizeY / 2)); //Update the score's display value ShowScore(sizeY, score); while (snake.IsTouching(apple.X, apple.Y)) { //Write snake body to Apples previous location Console.SetCursorPosition(apple.X, apple.Y); Console.Write("O"); //Spawn new apple apple = new Apple(sizeX, sizeY); } ticks += 1; } //Stop controls thread. controlThread.Abort(); Console.CursorTop = Console.WindowHeight / 2; ConsoleWrite.WriteCentered("Game Over"); PostStats(new [] { loop ? 1 : 0, score, snake.Length - 4, ticks }); }
public GameEngine() { this.Snake = new Snake(); this.currentApple = GenerateApple(); }