public void MovingPos(Tile toMove, Tile movingTo, Board board) { if (toMove.X < movingTo.X) { X += (int)toMove.moveSpeed; } else if (toMove.X > movingTo.X) { X -= (int)toMove.moveSpeed; } else if (toMove.Y < movingTo.Y) { Y += (int)toMove.moveSpeed; } else if (toMove.Y > movingTo.Y) { Y -= (int)toMove.moveSpeed; } if (Math.Abs(toMove.X - movingTo.X) <= 5 && Math.Abs(toMove.Y - movingTo.Y) <= 5) { X = movingTo.X; Y = movingTo.Y; board.TileMoving = false; } board.ToMove.X = X; board.ToMove.Y = Y; }
//Click event for starting the game public void Click(GameLoop game, Tile[] array, Board board) { //Instatiates class and randomises the tile placement Shuffle shuffle = new Shuffle(); shuffle.ShuffleArray(array); //Creates variables int rowCounter = 0; int colCounter = 0; Tile index; //Loops through array of tiles for (int i = 0; i < array.Length; i++) { //Checks if the current tile is empty and it isn't placed in the bottom right if (array[i] == null && i != array.Length - 1) { //Stores tile currently in bottom right index = array[array.Length - 1]; //Swaps it with empty tile array[array.Length - 1] = null; array[i] = index; } } //Loops through array of tiles for (int i = 0; i < array.Length; i++) { //Checks counter hasn't reached the empty tile if (i < array.Length - 1) { //Assigns tile's column and row array[i].Col = colCounter; array[i].Row = rowCounter; //Increment column counter colCounter++; //If the end of the row has been reached, reset column to zero and move to next row if (colCounter >= 4) { colCounter = 0; rowCounter++; } //Sets current tile's x and y position based on row and column array[i].SetPos(board); } } //Tracks that the game is now in progress game.GameStarted = true; }
//Sets the button's X and Y positions to align to a part of the board public void SetPos(Board board, string alignment) { switch (alignment) { case "left": X = board.X; break; case "right": X = board.X + board.Width - W; break; case "middle": X = board.X + board.Width / 2 - W / 2; break; } Y = board.Y + board.Height; }
//Checks for input every tick public void Update(GameLoop game, Board board) { //Gets the current state of the mouse MouseState newMState = Mouse.GetState(); KeyboardState newKState = Keyboard.GetState(); kArray = oldKState.GetPressedKeys(); Keys pressedKey = Keys.A; if (kArray.Length > 0) { pressedKey = kArray[kArray.Length - 1]; } //Checks if the mouse has just been clicked by comparing the current state with the previous state if (newMState.LeftButton == ButtonState.Pressed && oldMState.LeftButton == ButtonState.Released) { //Logs click coordinates int x = newMState.X; int y = newMState.Y; //Checks if a button has been pressed before checking if a tile was clicked if (board.ButtonClick(x, y, game) == false) { board.TileClick(x, y); board.CheckWin(); } } //Resets old mouse state oldMState = newMState; //Checks if keys have been pressed to end the game if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { game.Exit(); } if ((newKState.IsKeyUp(Keys.Left) && oldKState.IsKeyDown(Keys.Left)) || (newKState.IsKeyUp(Keys.Right) && oldKState.IsKeyDown(Keys.Right)) || (newKState.IsKeyUp(Keys.Down) && oldKState.IsKeyDown(Keys.Down)) || (newKState.IsKeyUp(Keys.Up) && oldKState.IsKeyDown(Keys.Up))) { board.TileKeyboard(pressedKey); } oldKState = newKState; }
//private CheckButton check; //Constructor taking in current screen size public Board(ContentManager Content, SpriteBatch sprBatch, int screenWidth, int screenHeight) { //Get's GameLoop's ContentManager content = Content; spriteBatch = sprBatch; //Creates a variable to pass into parameters self = this; //Creates buttons start = new ingame_StartButton("gameStart", content); //check = new CheckButton("check", content); //Sets background image and centers the board based on the screen background = new Sprite("graphics/background", content); int bgx = screenWidth / 2 - background.W / 2; int bgY = screenHeight / 2 - background.H / 2 - start.H / 2; background.SetPos(bgx, bgY); //Locally stores properties of the background image width = background.W; height = background.H; x = background.X; y = background.Y; //Creates an empty array for the tiles array = new Tile[numTiles]; //Creates another empty array to compare against winPattern = new Tile[numTiles]; toMove = new Tile("1", content); movingTo = new Tile("1", content); //Creates the board CreateBoard(); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); mainMenu = new MainMenu(Content, screenWidth, screenHeight); // Instantiates class using window size board = new Board(Content, spriteBatch, screenWidth, screenHeight); }
//Set's the tile's position based on it's row and column relative to the board public void SetPos(Board board) { X = board.X + W * col; Y = board.Y + H * row; }
//Click event checks if the game has been won public void Click(Board board) { board.CheckWin(); }