Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- 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.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace GrafikaRuchoma_01b
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Tile Background, Empty;
- List<Tile> map;
- int moveCount = 0, selection = 0;
- bool mouseLock = false;
- Random random;
- Tile[] selected;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = 700;
- graphics.PreferredBackBufferHeight = 500;
- IsMouseVisible = true;
- }
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- selected = new Tile[2];
- random = new Random();
- base.Initialize();
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- var puste = Content.Load<Texture2D>("puste");
- Empty = new Tile(puste, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 0);
- Background = new Tile(Content.Load<Texture2D>("background"), new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 0);
- var kafelki = Content.Load<Texture2D>("kafelki");
- var dev = 6;
- int count = 0;
- List<List<bool>> PosListX = new List<List<bool>>();
- for (int x = 0; x < 6; x++)
- {
- PosListX.Add(new List<bool>());
- for (int y = 0; y < 4; y++)
- {
- PosListX[x].Add(false);
- }
- }
- map = new List<Tile>();
- for (int i = 0; i < 2; i++)
- {
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 4; x++)
- {
- var cut = new Rectangle(x * 100, y * 100, 100, 100);
- int posX, posY;
- do{
- posX = random.Next(0,6);
- posY = random.Next(0, 4);
- } while (PosListX[posX][posY]);
- PosListX[posX][posY] = true;
- map.Add(new Tile(kafelki, new Rectangle(posX * 100 + 100, posY * 100 + 100, 100, 100), 0, puste, cut));
- map[count].ShowCard = true;
- count++;
- }
- }
- }
- // TODO: use this.Content to load your game content here
- }
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- protected override void Update(GameTime gameTime)
- {
- var mouseState = Mouse.GetState();
- var keyboardState = Keyboard.GetState();
- var mousePos = new Point(mouseState.X, mouseState.Y);
- // Allows the game to exit
- if (keyboardState.IsKeyDown(Keys.Escape))
- this.Exit();
- var remaning = map.Count(a => !a.Discavered);
- if (remaning == 0)
- {
- Window.Title = "Wygrana w " + moveCount;
- }
- else
- {
- for (int y = 0; y < map.Count; y++)
- {
- if (map[y].IsOn(mousePos) && mouseState.LeftButton == ButtonState.Pressed && !mouseLock)
- {
- moveCount++;
- Window.Title = "Ruch " + moveCount;
- if (selection == 0)
- {
- selected[0] = null;
- selected[1] = null;
- }
- mouseLock = true;
- selected[selection] = map[y];
- if (selected[0] != null && selected[1] != null && selected[0].Source == selected[1].Source)
- {
- selected[0].Discavered = true;
- selected[1].Discavered = true;
- }
- selection = selection == 0 ? 1 : 0;
- }
- }
- }
- if (mouseState.LeftButton == ButtonState.Released)
- {
- mouseLock = false;
- }
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- {
- Background.Draw(spriteBatch, false);
- for (int y = 0; y < map.Count; y++)
- {
- if (selected.Contains(map[y]))
- {
- map[y].ShowCard = false;
- }
- else
- {
- map[y].ShowCard = true;
- }
- map[y].Draw(spriteBatch);
- }
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- public class Tile
- {
- public Texture2D SpriteAlt { get; set; }
- public bool ShowCard { get; set; }
- public Texture2D Sprite { get; set; }
- public Rectangle Position { get; set; }
- public double Rotation { get; set; }
- public Color TintColor { get; set; }
- public Rectangle? Source { get; set; }
- public float Scale { get; set; }
- public int Width { get { return Position.Width; } }
- public int Height { get { return Position.Height; } }
- public bool Discavered { get; set; }
- public bool IsVisible { get; set; }
- public Tile(Texture2D sprite, Rectangle position, int rotation, Texture2D spriteAlt = null, Rectangle? source = null)
- {
- SpriteAlt = spriteAlt;
- ShowCard = false;
- Sprite = sprite;
- Position = position;
- Rotation = rotation;
- Source = source;
- TintColor = Color.White;
- IsVisible = true;
- Discavered = false;
- Scale = 1;
- }
- public void Rotate(int degrees)
- {
- Rotation += (Math.PI / 180) * degrees;
- if (Rotation >= 360)
- Rotation -= 360;
- else if (Rotation < 0)
- Rotation += 360;
- }
- public bool IsOn(Point mousePosition)
- {
- var position = Position;
- position.X -= Position.Width / 2;
- position.Y -= Position.Height / 2;
- return position.Contains(mousePosition);
- }
- public void Draw(SpriteBatch spriteBatch, bool center = true)
- {
- if (IsVisible)
- {
- var source = Source.HasValue ? Source : new Rectangle(0, 0, Sprite.Width, Sprite.Height);
- if (ShowCard && !Discavered)
- {
- source = new Rectangle(0, 0, SpriteAlt.Width, SpriteAlt.Height);
- }
- Vector2 origin = new Vector2(0, 0);
- if (center)
- origin = Source.HasValue ? new Vector2(Source.Value.Width / 2, Source.Value.Height / 2) : new Vector2(Sprite.Width / 2, Sprite.Height / 2);
- if (ShowCard && !Discavered)
- {
- origin = new Vector2(SpriteAlt.Width / 2, SpriteAlt.Height / 2);
- }
- var position = new Rectangle(Position.X, Position.Y, (int)(Position.Width * Scale), (int)(Position.Height * Scale));
- var sprite = !ShowCard || Discavered || SpriteAlt == null ? Sprite : SpriteAlt;
- spriteBatch.Draw(sprite, position, source, TintColor, (float)Rotation, origin, SpriteEffects.None, 0);
- }
- }
- public void Resize(int width, int height)
- {
- Position = new Rectangle(Position.X, Position.Y, width, height);
- }
- public void SetPosition(int x, int y)
- {
- Position = new Rectangle(x, y, Position.Width, Position.Height);
- }
- public void SetScale(float scale)
- {
- Scale = scale;
- }
- public void SetColor(Color color)
- {
- TintColor = color;
- }
- public void SetVisibility(bool visible)
- {
- IsVisible = visible;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement