@Konstantin_Mads_Y wrote:
Hello from Russia, friends!
A can't understand how i can realized
algorithm to find neighboring pieces and replacing them for another piecesthis example very simillar
But i dont understand anything
Here my codeusing System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;namespace match3_game_test1
{ {GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;Vector2 Centerposition = Vector2.Zero; Texture2D gridTexture; Texture2D boxTexture0; Texture2D boxTexture1; Texture2D boxTexture2; Texture2D boxTexture3; Texture2D boxTexture4; Texture2D boxTexture5; List<Box> piecesList; List<Box> backgroundbox; int tlsizeX = 50; int tlsizeY = 50; int[,] map = new int[,]{ {0,0,1,1,1,1,0,0},{0,1,1,1,1,1,1,0},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{0,1,1,1,1,1,1,0},{0,0,1,1,1,1,0,0}}; #region //WINDOW RESOLUTION int screenWidth = 612; int screenHeight = 612; #endregion #region Private variables private MouseState _previousMouse, _currentMouse; private SpriteBatch _spriteBatch; private List<Tile> _tiles = new List<Tile>(); private Random _rnd = new Random(); private Box _currentlyDraggedTile, _tileUnderMouse; private Vector2 _startingPositionForCurrentlyDraggedTile, _currentMousePosition; private SpriteFont _bigFont; #endregion public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here graphics.PreferredBackBufferWidth = screenWidth; graphics.PreferredBackBufferHeight = screenHeight; graphics.ApplyChanges(); IsMouseVisible = true; base.Initialize(); } /// <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); gridTexture = Content.Load<Texture2D>("maps"); boxTexture0 = Content.Load<Texture2D>("cude"); boxTexture1 = Content.Load<Texture2D>("round"); boxTexture2 = Content.Load<Texture2D>("triangle"); boxTexture3 = Content.Load<Texture2D>("cude_purple"); boxTexture4 = Content.Load<Texture2D>("round_purple"); boxTexture5 = Content.Load<Texture2D>("triangle_purple"); _spriteBatch = new SpriteBatch(GraphicsDevice); _bigFont = Content.Load<SpriteFont>("BigFont"); Backgroundtiles(); CreateTileswithTextures(); } public void CreateTileswithTextures() { Random rnd = new Random(); string[] texturelist; texturelist = new string[] { "triangle_purple", "round_purple", "triangle", "cude_purple", "round", "cude"}; piecesList = new List<Box>(); int x = 0; int y = 0; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { Vector2 topRightCorner = new Vector2(103,103); Texture2D texture = Content.Load<Texture2D>(texturelist[rnd.Next(texturelist.GetLength(0))]); Vector2 pos = new Vector2(x , y) + topRightCorner ; int a = map[i, j]; if (a == 1) { Box piece = new Box { Texture = texture, Position = pos }; piecesList.Add(piece); } x += tlsizeX; } x = 0; y += tlsizeY; } } void Backgroundtiles() { backgroundbox = new List<Box>(); int x = 0; int y = 0; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { Vector2 topRightCorner = new Vector2(103, 103); Vector2 pos = new Vector2(x, y) + topRightCorner; int a = map[i, j]; if (a == 1) { Box bpiece = new Box { Texture = gridTexture, Position = pos }; backgroundbox.Add(bpiece); } x += tlsizeX; } x = 0; y += tlsizeY; } } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <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) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); // TODO: Add your update logic here UpdateMouseStates(); CheckForTileRelease(); CheckForNewDrag(); UpdateDraggedTilePosition(); } private void CheckForTileRelease() { if (_currentMouse.LeftButton == ButtonState.Released) { if (_currentlyDraggedTile != null) { if (_tileUnderMouse != null) { _currentlyDraggedTile.Position = _tileUnderMouse.Position; _tileUnderMouse.Position = _startingPositionForCurrentlyDraggedTile; } else { _currentlyDraggedTile.Position = _startingPositionForCurrentlyDraggedTile; } } _currentlyDraggedTile = null; } } private void CheckForNewDrag() { if (_previousMouse.LeftButton == ButtonState.Released && _currentMouse.LeftButton == ButtonState.Pressed) { _currentlyDraggedTile = GetTileUnderMouse(); if (_currentlyDraggedTile != null) { _startingPositionForCurrentlyDraggedTile = _currentlyDraggedTile.Position; } } } private Box GetTileUnderMouse() { return piecesList.FirstOrDefault(piece => piece.GetBounds().Contains((int)_currentMousePosition.X, (int)_currentMousePosition.Y) && piece != _currentlyDraggedTile); } private void UpdateDraggedTilePosition() { if (_currentlyDraggedTile != null) { _currentlyDraggedTile.Position = _currentMousePosition - Vector2.One * _currentlyDraggedTile.Texture.Width / 2; } } private void UpdateMouseStates() { _previousMouse = _currentMouse; _currentMouse = Mouse.GetState(); _currentMousePosition = new Vector2(_currentMouse.X, _currentMouse.Y); _tileUnderMouse = GetTileUnderMouse(); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.DarkViolet); // TODO: Add your drawing code here spriteBatch.Begin(); DrawText(); DrawbackgroundTiles(); DrawPiecesAndSelection(); spriteBatch.End(); base.Draw(gameTime); } private void DrawbackgroundTiles() { foreach (Box bpiece in backgroundbox) { MouseState mouseState = Mouse.GetState(); if (bpiece.Bounds.Contains(mouseState.Position)) { // TODO: Code to handle node being clicked... Texture2D select = Content.Load<Texture2D>("select"); Rectangle rect = bpiece.Bounds; spriteBatch.Draw(select, rect, Color.WhiteSmoke); } bpiece.Draw(spriteBatch); } } private void DrawPiecesAndSelection() { foreach (Box piece in piecesList) { piece.Draw(spriteBatch); } } private void DrawText() { spriteBatch.DrawString(_bigFont, "Developer MaDcat413", new Vector2(50, 10), Color.White); } } }
Posts: 2
Participants: 2