private bool MoveAndEat() { SnakePoint p = snake.GetSnakeNextPoint(); if (food != null && p.X == food.X && p.Y == food.Y) { snake.AddSnakeBody(p.X, p.Y); food = null; return(true); } else { return(snake.MoveSnake()); } }
private void TimerEvent(object source, System.Timers.ElapsedEventArgs e) { if (!MoveAndEat()) { Lost(); } else { RefreshSnake(); } iCount++; if (iCount == 5) { food = SetFood(); } if (food == null && iCount > 5) { food = SetFood(); if (iCount > 5 && snake.SnakeLength > 5 && snake.SnakeLength % 5 == 0) { speed++; t.Interval = 3000 / (speed * 2); lbSpeed.Text = speed.ToString(); } } }
private bool MoveAndEat() { SnakePoint p = snake.GetSnakeNextPoint(); if (food != null && p.X == food.X && p.Y == food.Y) { snake.AddSnakeBody(p.X, p.Y); food = null; return true; } else { return snake.MoveSnake(); } }
private void InitForm() { BgArray = new BaseBlock[iWindowSize, iWindowSize]; snake = new SnakeClass(6); snake.MaxPos = iWindowSize; List<Control> c = new List<Control>(); for (int i = 0; i < iWindowSize; i++) { for (int j = 0; j < iWindowSize; j++) { BaseBlock b = new BaseBlock(); b.Top = iSize * i + 25; b.Left = iSize * j; b.X = j; b.Y = i; b.Visible = true; BgArray[j, i] = b; c.Add(b); } } this.Controls.AddRange(c.ToArray()); RefreshSnake(); this.Width = 10 + iSize * iWindowSize; this.Height = 45 + iSize * iWindowSize + iSize; t = new System.Timers.Timer(3000 / speed); t.AutoReset = true; t.Elapsed += TimerEvent; t.SynchronizingObject = this; t.Start(); lbName.Visible = true; lbSpeed.Visible = true; lbSpeed.Text = speed.ToString(); this.Refresh(); }