Advertisement
Alan468

GR_01_Karty

Oct 16th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace GrafikaRuchoma_01b
  13. {
  14.     public class Game1 : Microsoft.Xna.Framework.Game
  15.     {
  16.         GraphicsDeviceManager graphics;
  17.         SpriteBatch spriteBatch;
  18.  
  19.         Tile Background, Empty;
  20.         List<Tile> map;
  21.         int moveCount = 0, selection = 0;
  22.         bool mouseLock = false;
  23.         Random random;
  24.  
  25.         Tile[] selected;
  26.  
  27.         public Game1()
  28.         {
  29.             graphics = new GraphicsDeviceManager(this);
  30.             Content.RootDirectory = "Content";
  31.  
  32.             graphics.PreferredBackBufferWidth = 700;
  33.             graphics.PreferredBackBufferHeight = 500;
  34.             IsMouseVisible = true;
  35.         }
  36.  
  37.         protected override void Initialize()
  38.         {
  39.             // TODO: Add your initialization logic here
  40.             selected = new Tile[2];
  41.             random = new Random();
  42.  
  43.             base.Initialize();
  44.         }
  45.  
  46.         protected override void LoadContent()
  47.         {
  48.             // Create a new SpriteBatch, which can be used to draw textures.
  49.             spriteBatch = new SpriteBatch(GraphicsDevice);
  50.  
  51.             var puste = Content.Load<Texture2D>("puste");
  52.             Empty = new Tile(puste, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 0);
  53.             Background = new Tile(Content.Load<Texture2D>("background"), new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 0);
  54.  
  55.             var kafelki = Content.Load<Texture2D>("kafelki");
  56.  
  57.             var dev = 6;
  58.             int count = 0;
  59.  
  60.             List<List<bool>> PosListX = new List<List<bool>>();
  61.             for (int x = 0; x < 6; x++)
  62.             {
  63.                 PosListX.Add(new List<bool>());
  64.                 for (int y = 0; y < 4; y++)
  65.                 {
  66.                     PosListX[x].Add(false);
  67.                 }
  68.             }
  69.  
  70.             map = new List<Tile>();
  71.             for (int i = 0; i < 2; i++)
  72.             {
  73.                 for (int y = 0; y < 3; y++)
  74.                 {
  75.                     for (int x = 0; x < 4; x++)
  76.                     {
  77.                         var cut = new Rectangle(x * 100, y * 100, 100, 100);
  78.  
  79.                         int posX, posY;
  80.                         do{
  81.                             posX = random.Next(0,6);
  82.                             posY = random.Next(0, 4);
  83.  
  84.                         } while (PosListX[posX][posY]);
  85.  
  86.                         PosListX[posX][posY] = true;
  87.  
  88.                         map.Add(new Tile(kafelki, new Rectangle(posX * 100 + 100, posY * 100 + 100, 100, 100), 0, puste, cut));
  89.                         map[count].ShowCard = true;
  90.  
  91.                         count++;
  92.                     }
  93.                 }
  94.             }
  95.  
  96.  
  97.  
  98.             // TODO: use this.Content to load your game content here
  99.         }
  100.  
  101.         protected override void UnloadContent()
  102.         {
  103.             // TODO: Unload any non ContentManager content here
  104.         }
  105.  
  106.         protected override void Update(GameTime gameTime)
  107.         {
  108.             var mouseState = Mouse.GetState();
  109.             var keyboardState = Keyboard.GetState();
  110.             var mousePos = new Point(mouseState.X, mouseState.Y);
  111.  
  112.             // Allows the game to exit
  113.             if (keyboardState.IsKeyDown(Keys.Escape))
  114.                 this.Exit();
  115.  
  116.             var remaning = map.Count(a => !a.Discavered);
  117.             if (remaning == 0)
  118.             {
  119.                 Window.Title = "Wygrana w " + moveCount;
  120.             }
  121.             else
  122.             {
  123.                 for (int y = 0; y < map.Count; y++)
  124.                 {
  125.                     if (map[y].IsOn(mousePos) && mouseState.LeftButton == ButtonState.Pressed && !mouseLock)
  126.                     {
  127.                         moveCount++;
  128.                         Window.Title = "Ruch " + moveCount;
  129.  
  130.  
  131.                         if (selection == 0)
  132.                         {
  133.                             selected[0] = null;
  134.                             selected[1] = null;
  135.                         }
  136.                         mouseLock = true;
  137.                         selected[selection] = map[y];
  138.  
  139.  
  140.                         if (selected[0] != null && selected[1] != null && selected[0].Source == selected[1].Source)
  141.                         {
  142.                             selected[0].Discavered = true;
  143.                             selected[1].Discavered = true;
  144.                         }
  145.  
  146.                         selection = selection == 0 ? 1 : 0;
  147.                     }
  148.                 }
  149.             }
  150.  
  151.             if (mouseState.LeftButton == ButtonState.Released)
  152.             {
  153.                 mouseLock = false;
  154.             }
  155.  
  156.             base.Update(gameTime);
  157.         }
  158.  
  159.         protected override void Draw(GameTime gameTime)
  160.         {
  161.             GraphicsDevice.Clear(Color.CornflowerBlue);
  162.  
  163.             spriteBatch.Begin();
  164.             {
  165.                 Background.Draw(spriteBatch, false);
  166.  
  167.                 for (int y = 0; y < map.Count; y++)
  168.                 {
  169.                     if (selected.Contains(map[y]))
  170.                     {
  171.                         map[y].ShowCard = false;
  172.                     }
  173.                     else
  174.                     {
  175.                         map[y].ShowCard = true;
  176.                     }
  177.  
  178.                     map[y].Draw(spriteBatch);
  179.                 }
  180.             }
  181.             spriteBatch.End();
  182.  
  183.             base.Draw(gameTime);
  184.         }
  185.     }
  186.  
  187.     public class Tile
  188.     {
  189.         public Texture2D SpriteAlt { get; set; }
  190.         public bool ShowCard { get; set; }
  191.  
  192.         public Texture2D Sprite { get; set; }
  193.         public Rectangle Position { get; set; }
  194.         public double Rotation { get; set; }
  195.         public Color TintColor { get; set; }
  196.         public Rectangle? Source { get; set; }
  197.         public float Scale { get; set; }
  198.  
  199.         public int Width { get { return Position.Width; } }
  200.         public int Height { get { return Position.Height; } }
  201.  
  202.         public bool Discavered { get; set; }
  203.  
  204.         public bool IsVisible { get; set; }
  205.  
  206.         public Tile(Texture2D sprite, Rectangle position, int rotation, Texture2D spriteAlt = null, Rectangle? source = null)
  207.         {
  208.             SpriteAlt = spriteAlt;
  209.             ShowCard = false;
  210.  
  211.             Sprite = sprite;
  212.             Position = position;
  213.             Rotation = rotation;
  214.             Source = source;
  215.             TintColor = Color.White;
  216.             IsVisible = true;
  217.             Discavered = false;
  218.             Scale = 1;
  219.         }
  220.  
  221.         public void Rotate(int degrees)
  222.         {
  223.             Rotation += (Math.PI / 180) * degrees;
  224.  
  225.             if (Rotation >= 360)
  226.                 Rotation -= 360;
  227.             else if (Rotation < 0)
  228.                 Rotation += 360;
  229.         }
  230.  
  231.         public bool IsOn(Point mousePosition)
  232.         {
  233.             var position = Position;
  234.             position.X -= Position.Width / 2;
  235.             position.Y -= Position.Height / 2;
  236.  
  237.             return position.Contains(mousePosition);
  238.         }
  239.  
  240.         public void Draw(SpriteBatch spriteBatch, bool center = true)
  241.         {
  242.             if (IsVisible)
  243.             {
  244.                 var source = Source.HasValue ? Source : new Rectangle(0, 0, Sprite.Width, Sprite.Height);
  245.                 if (ShowCard && !Discavered)
  246.                 {
  247.                     source = new Rectangle(0, 0, SpriteAlt.Width, SpriteAlt.Height);
  248.                 }
  249.  
  250.                 Vector2 origin = new Vector2(0, 0);
  251.                 if (center)
  252.                     origin = Source.HasValue ? new Vector2(Source.Value.Width / 2, Source.Value.Height / 2) : new Vector2(Sprite.Width / 2, Sprite.Height / 2);
  253.  
  254.                 if (ShowCard && !Discavered)
  255.                 {
  256.                     origin = new Vector2(SpriteAlt.Width / 2, SpriteAlt.Height / 2);
  257.                 }
  258.  
  259.                 var position = new Rectangle(Position.X, Position.Y, (int)(Position.Width * Scale), (int)(Position.Height * Scale));
  260.  
  261.                 var sprite = !ShowCard || Discavered || SpriteAlt == null ? Sprite : SpriteAlt;
  262.  
  263.                 spriteBatch.Draw(sprite, position, source, TintColor, (float)Rotation, origin, SpriteEffects.None, 0);
  264.             }
  265.         }
  266.  
  267.         public void Resize(int width, int height)
  268.         {
  269.             Position = new Rectangle(Position.X, Position.Y, width, height);
  270.         }
  271.  
  272.         public void SetPosition(int x, int y)
  273.         {
  274.             Position = new Rectangle(x, y, Position.Width, Position.Height);
  275.         }
  276.  
  277.         public void SetScale(float scale)
  278.         {
  279.             Scale = scale;
  280.         }
  281.  
  282.         public void SetColor(Color color)
  283.         {
  284.             TintColor = color;
  285.         }
  286.  
  287.         public void SetVisibility(bool visible)
  288.         {
  289.             IsVisible = visible;
  290.         }
  291.  
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement