コード例 #1
0
 public SnakeGame()
 {
     grid  = new SnakeGrid();
     snake = new Snake(1, 3, 1);
     snack = new SnakeSnack(3, 4);
     snack.reposition(grid);
 }
コード例 #2
0
        public void Render(SnakeGrid grid)
        {
            ChromaColor[] colors = { ChromaColor.White };
            int           i      = 0;

            foreach (var pos in tail)
            {
                ChromaColor color = colors[i++ % colors.Length];
                if (i == tail.Count)
                {
                    color = ChromaColor.Yellow;
                }
                if (IsDead)
                {
                    var coordHead = tail.Last();
                    grid.SetDataPixel(coordHead.x, coordHead.y, GridDataType.SNAKE, ChromaColor.Red);

                    color = ChromaColor.Black;
                    if (i >= deadAnimationIdx)
                    {
                        deadAnimationIdx++;
                        grid.SetDataPixel(pos.x, pos.y, GridDataType.SNAKE, color);
                        return;
                    }
                }
                grid.SetDataPixel(pos.x, pos.y, GridDataType.SNAKE, color);
            }
            if (IsDead)
            {
                IsDeadRendered = true;
            }
        }
コード例 #3
0
 public void reposition(SnakeGrid grid)
 {
     position = grid.FindRandomEmptyCoord();
     update(grid);
 }
コード例 #4
0
 public void update(SnakeGrid grid)
 {
     ChromaColor[] colors = { ChromaColor.Red, ChromaColor.Green, ChromaColor.Blue };
     color = colors[color_i++ % colors.Length];
     grid.SetDataPixel(position.x, position.y, GridDataType.FOOD, color);
 }
コード例 #5
0
        public void update(SnakeGrid grid, ref bool foodEaten)
        {
            if (IsDead)
            {
                return;
            }
            if (proposedDirection != SnakeDirection.NONE)
            {
                direction         = proposedDirection;
                proposedDirection = SnakeDirection.NONE;
            }
            switch (direction)
            {
            case SnakeDirection.UP:
                positon.y--;
                if (positon.y < 1)
                {
                    positon.y = SnakeGrid.Height - 2;
                }
                break;

            case SnakeDirection.LEFT:
                positon.x--;
                if (positon.x < 1)
                {
                    positon.x = SnakeGrid.Width - 2;
                }
                break;

            case SnakeDirection.RIGHT:
                positon.x++;
                if (positon.x >= SnakeGrid.Width - 1)
                {
                    positon.x = 1;
                }
                break;

            case SnakeDirection.DOWN:
                positon.y++;
                if (positon.y >= SnakeGrid.Height - 1)
                {
                    positon.y = 1;
                }
                break;
            }
            if (grid.GetGridDataAt(positon.x, positon.y) == GridDataType.FOOD)
            {
                ++length;
                foodEaten = true;
            }

            tail.Enqueue(positon.Clone());
            while (tail.Count > length)
            {
                Coord removedTail = tail.Dequeue();
                grid.SetDataPixel(removedTail.x, removedTail.y, GridDataType.EMPTY);
            }

            if (grid.GetGridDataAt(positon.x, positon.y) == GridDataType.SNAKE)
            {
                IsDead = true;
            }
        }