Esempio n. 1
0
        /**
         * Moves head to the left or right. Turing machines always move one position on the tape at a time.
         */
        private void moveHead(DirectionToMove direction)
        {
            if (direction == DirectionToMove.RIGHT)
            {
                currentHeadPosition++;
            }
            else /* if (direction == DirectionToMove.LEFT) */
            {
                currentHeadPosition--;
            }

            updatePositionMarkers();
        }
 public override void Update(GameTime gameTime, DirectionToMove moveDirection, Point newHeadPosition)
 {
     base.Update(gameTime, moveDirection, newHeadPosition);
     if (game.GetMapManager().getSquare(location.X,location.Y).isBlocking())
     {
         dead = true;
     }
     else if (game.GetMapManager().containsFood(location))
     {
         CowPiece newPiece = new CowPiece(standardTexture, standardTailTexture, game);
         AttachPiece(newPiece);
         game.GetMapManager().removeFood(location);
         game.addToScore();
     }
     game.GetMapManager().getSquare(location.X, location.Y).SetBlocking(true);
 }
Esempio n. 3
0
 public Instruction(char symbolToPrint, DirectionToMove directionToMove, State newState)
 {
     this.symbolToPrint   = symbolToPrint;
     this.directionToMove = directionToMove;
     this.newState        = newState;
 }
Esempio n. 4
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            newState = Keyboard.GetState();
            if (gameMode == GameMode.frontScreen)
            {
                if (newState.IsKeyDown(Keys.Space) && oldState.IsKeyUp(Keys.Space))
                {
                    gameMode = GameMode.mainGame;
                }
            }
            else if (gameMode == GameMode.mainGame)
            {
                timeSinceLastJump += gameTime.ElapsedGameTime.Milliseconds;

                if (newState.IsKeyDown(Keys.Right) && oldState.IsKeyUp(Keys.Right)) { direction = DirectionToMove.right; }
                else if (newState.IsKeyDown(Keys.Left) && oldState.IsKeyUp(Keys.Left)) { direction = DirectionToMove.left; }
                else if (newState.IsKeyDown(Keys.Up) && oldState.IsKeyUp(Keys.Up)) { direction = DirectionToMove.up; }
                else if (newState.IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down)) { direction = DirectionToMove.down; }

                int randomTime = 500 + (int)(Math.Sin(gameTime.TotalGameTime.TotalMilliseconds / 1000.0f) * 12 * score);

                if (timeSinceLastJump > randomTime)
                {
                    head.Update(gameTime, direction, new Point(-1, -1));
                    timeSinceLastJump = 0;
                    mapManager.MoveWalls();
                }

                if (head.isDead())
                {
                    gameMode = GameMode.endScreen;
                }

                oldState = newState;
                base.Update(gameTime);
            }

            else
            {
                if (newState.IsKeyDown(Keys.Space) && oldState.IsKeyUp(Keys.Space))
                {
                    gameMode = GameMode.mainGame;
                    reset();
                    //TODO: Write reset methods
                }
            }
        }
Esempio n. 5
0
        public WallHead(int x, int y, Texture2D texture, Color colour, MapManager manager, int numberOfVertices)
            : base(x,y,texture,colour,manager)
        {
            headPoint = new Point(x, y);
            Point lastPoint = new Point(x, y);
            WallPiece previous = this;
            Nullable<DirectionToMove> lastDirectionMoved = null;
            for (int verticesCreated = 0; verticesCreated <= numberOfVertices; ++verticesCreated)
            {
                //lastMovedDirection - this is the variable that tells the next piece what direction to go when moving
                //lastDirectionMoved - this is used in creation of the original wall only DON'T GET CONFUSED!!!
                DirectionToMove newDirectionToMove;
                do
                {
                    newDirectionToMove = (DirectionToMove)random.Next(4);
                } while (newDirectionToMove == lastDirectionMoved);

                //Reverse direction since if we create the wall upwards then the snake is facing downwards but only for first point
                if (lastDirectionMoved == null)
                {
                    switch (newDirectionToMove)
                    {
                        case DirectionToMove.down:
                            lastMovedDirection = DirectionToMove.up;
                            break;
                        case DirectionToMove.up:
                            lastMovedDirection = DirectionToMove.down;
                            break;
                        case DirectionToMove.left:
                            lastMovedDirection = DirectionToMove.right;
                            break;
                        case DirectionToMove.right:
                            lastMovedDirection = DirectionToMove.left;
                            break;
                    }
                }

                Console.WriteLine("Starting direction: " + lastMovedDirection.ToString());

                MapManager.HedgeType newWallHedgeType;
                if (newDirectionToMove == DirectionToMove.left || newDirectionToMove == DirectionToMove.right)
                {
                    newWallHedgeType = MapManager.HedgeType.horizontal;
                }
                else
                {
                    newWallHedgeType = MapManager.HedgeType.vertical;
                }

                if (lastDirectionMoved != null && lastPoint.X < manager.size && lastPoint.X > 0 && lastPoint.Y < manager.size && lastPoint.Y > 0)
                {
                    //Must create a corner piece
                    MapManager.HedgeType cornerType = newWallHedgeType;
                    switch (lastDirectionMoved)
                    {
                        case DirectionToMove.down:
                            switch (newDirectionToMove)
                            {
                                case DirectionToMove.left:
                                    cornerType = MapManager.HedgeType.corner_topleft;
                                    break;

                                case DirectionToMove.right:
                                    cornerType = MapManager.HedgeType.corner_topright;
                                    break;
                            }
                            break;
                        case DirectionToMove.up:
                            switch (newDirectionToMove)
                            {
                                case DirectionToMove.left:
                                    cornerType = MapManager.HedgeType.corner_bottomleft;
                                    break;

                                case DirectionToMove.right:
                                    cornerType = MapManager.HedgeType.corner_bottomright; //bottomright
                                    break;
                            }
                            break;

                        case DirectionToMove.left:
                            switch (newDirectionToMove)
                            {
                                case DirectionToMove.up:
                                    cornerType = MapManager.HedgeType.corner_topright;
                                    break;

                                case DirectionToMove.down:
                                    cornerType = MapManager.HedgeType.corner_bottomright;
                                    break;
                            }
                            break;

                        case DirectionToMove.right:
                            switch (newDirectionToMove)
                            {
                                case DirectionToMove.up:
                                    cornerType = MapManager.HedgeType.corner_topleft;
                                    break;

                                case DirectionToMove.down:
                                    cornerType = MapManager.HedgeType.corner_bottomleft;
                                    break;
                            }
                            break;
                    }

                    //Now make the relevant corner
                    manager.map[lastPoint.X, lastPoint.Y] = manager.createHedge(lastPoint.X, lastPoint.Y, cornerType);
                    previous.Attach((WallPiece)manager.map[lastPoint.X, lastPoint.Y]);
                    previous = (WallPiece)manager.map[lastPoint.X, lastPoint.Y];
                }

                Point direction = Point.Zero;
                switch (newDirectionToMove)
                {
                    case DirectionToMove.left:
                        direction = new Point(-1, 0);
                        break;

                    case DirectionToMove.right:
                        direction = new Point(1, 0);
                        break;

                    case DirectionToMove.up:
                        direction = new Point(0, -1);
                        break;

                    case DirectionToMove.down:
                        direction = new Point(0, 1);
                        break;
                }
                int length = random.Next(4, 6);
                for (int i = 1; i < length; ++i)
                {
                    Point pointToMakeWall = new Point(lastPoint.X + direction.X * i, lastPoint.Y + direction.Y * i);
                    if (isValid(pointToMakeWall.X,pointToMakeWall.Y) && pointToMakeWall != CowPiece.startPoint)
                    {
                        manager.map[pointToMakeWall.X, pointToMakeWall.Y] = manager.createHedge(pointToMakeWall.X, pointToMakeWall.Y, newWallHedgeType);
                        previous.Attach((WallPiece)manager.map[pointToMakeWall.X, pointToMakeWall.Y]);
                        previous = (WallPiece)manager.map[pointToMakeWall.X, pointToMakeWall.Y];
                    }
                }

                lastPoint = new Point(lastPoint.X + direction.X * length, lastPoint.Y + direction.Y * length);
                lastDirectionMoved = newDirectionToMove;
            }
        }
Esempio n. 6
0
        public void MakeMove(DirectionToMove direction)
        {
            Console.WriteLine("Trying to move: " + direction.ToString());
            int newx = xlocation;
            int newy = ylocation;
            Texture2D textureToPassOn = null;
            switch (direction)
            {
                case DirectionToMove.down:
                {
                    if (isValid(xlocation, ylocation + 1))
                    {
                        if (!mapManager.map[xlocation, ylocation + 1].isBlocking())
                        {
                            newx = xlocation; newy = ylocation + 1;
                            switch (lastMovedDirection)
                            {
                                case DirectionToMove.left:
                                    {
                                        //Top left
                                        textureToPassOn = mapManager.hedgeCornerTexturebr;
                                        break;
                                    }

                                case DirectionToMove.right:
                                    {
                                        Console.WriteLine("Using tr texture");
                                        textureToPassOn = mapManager.hedgeCornerTexturebl;
                                        break;
                                    }
                            }
                        }
                    }
                    break;
                }
                case DirectionToMove.up:
                {
                    if (isValid(xlocation, ylocation - 1))
                    {
                        if (!mapManager.map[xlocation, ylocation - 1].isBlocking())
                        {
                            newx = xlocation; newy = ylocation - 1;
                            switch (lastMovedDirection)
                            {
                                case DirectionToMove.left:
                                    {
                                        //Top left
                                        Console.WriteLine("Using bl texture");
                                        textureToPassOn = mapManager.hedgeCornerTexturetr;
                                        break;
                                    }

                                case DirectionToMove.right:
                                    {
                                        Console.WriteLine("Using br texture");
                                        textureToPassOn = mapManager.hedgeCornerTexturetl;
                                        break;
                                    }
                            }
                        }
                    }
                    break;
                }
                case DirectionToMove.left :
                {
                    if (isValid(xlocation - 1, ylocation))
                    {
                        if (!mapManager.map[xlocation -1 , ylocation].isBlocking())
                        {
                            newx = xlocation - 1; newy = ylocation;
                            switch (lastMovedDirection)
                            {
                                case DirectionToMove.up:
                                    {
                                        Console.WriteLine("Using tr texture");
                                        textureToPassOn = mapManager.hedgeCornerTexturebl;
                                        break;
                                    }

                                case DirectionToMove.down:
                                    {
                                        Console.WriteLine("Using br texture");
                                        textureToPassOn = mapManager.hedgeCornerTexturetl; //should be br?
                                        break;
                                    }
                            }
                        }
                    }
                    break;
                }
                case DirectionToMove.right :
                {
                    if (isValid(xlocation + 1, ylocation))
                    {
                        if (!mapManager.map[xlocation + 1, ylocation].isBlocking())
                        {
                            newx = xlocation + 1; newy = ylocation;
                            switch (lastMovedDirection)
                            {
                                case DirectionToMove.up:
                                    {
                                        textureToPassOn = mapManager.hedgeCornerTexturebr;
                                        Console.WriteLine("Using tl texture");
                                        break;
                                    }

                                case DirectionToMove.down:
                                    {
                                        textureToPassOn = mapManager.hedgeCornerTexturetr;
                                        Console.WriteLine("Using bl texture");
                                        break;
                                    }
                            }
                        }
                    }
                    break;
                }
            }

            if (newx != xlocation || newy != ylocation)
            {
                if (textureToPassOn == null)
                {
                    Console.WriteLine("Going straight");
                    switch (direction)
                    {
                        case DirectionToMove.left:
                        case DirectionToMove.right:
                            textureToPassOn = mapManager.hedgeTexture;
                            Console.WriteLine("Using horizontal");
                            break;

                        case DirectionToMove.up:
                        case DirectionToMove.down:
                            textureToPassOn = mapManager.hedgeVertTexture;
                            Console.WriteLine("Using vertical");
                            break;
                    }
                }

                switch (direction)
                {
                    case DirectionToMove.left:
                    case DirectionToMove.right:
                        texture = mapManager.hedgeTexture;
                        Console.WriteLine("Using horizontal");
                        break;

                    case DirectionToMove.up:
                    case DirectionToMove.down:
                        texture = mapManager.hedgeVertTexture;
                        Console.WriteLine("Using vertical");
                        break;
                }

                if (next != null) { next.Move(xlocation, ylocation, textureToPassOn); }
                else { mapManager.map[xlocation, ylocation] = mapManager.createGrass(xlocation, ylocation, false); }
                xlocation = newx;
                ylocation = newy;
                if (mapManager.map[newx, newy].containsFood()) { mapManager.GenerateFood(); }
                mapManager.map[newx, newy] = this;
                lastMovedDirection = direction;
            }
        }
Esempio n. 7
0
        protected bool isDirectionOpposite(DirectionToMove a, DirectionToMove b)
        {
            switch (a)
            {
                case DirectionToMove.right:
                    return b == DirectionToMove.left;

                case DirectionToMove.left:
                    return b == DirectionToMove.right;

                case DirectionToMove.up:
                    return b == DirectionToMove.down;

                case DirectionToMove.down:
                    return b == DirectionToMove.up;
            }

            return false;
        }
Esempio n. 8
0
        public virtual void Update(GameTime gameTime, DirectionToMove moveDirection, Point newHeadPosition)
        {
            if (isDirectionOpposite(moveDirection, lastMoveDirection))
            {
                moveDirection = lastMoveDirection;
            }
            if (isTail) { game.GetMapManager().getSquare(location.X, location.Y).SetBlocking(false); }
            switch (moveDirection)
            {
                case DirectionToMove.down:
                    {
                        location.Y += 1;
                        break;
                    }

                case DirectionToMove.up:
                    {
                        location.Y -= 1;
                        break;
                    }

                case DirectionToMove.left:
                    {
                        location.X -= 1;
                        break;
                    }

                case DirectionToMove.right:
                    {
                        location.X += 1;
                        break;
                    }
            }

            if (newHeadPosition.X == -1)
            {
                headPosition = location;
            }
            else
            {
                headPosition = newHeadPosition;
            }

            if (nextPiece != null)
            {
                nextPiece.Update(gameTime, lastMoveDirection, headPosition);
            }

            lastMoveDirection = moveDirection;
        }