Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aiv.Draw;
  7.  
  8. namespace Memory
  9. {
  10.     enum ColorType { White, Blue, Green, Red, Yellow, Purple, Card } //enumerato per i colori
  11.     struct Card //struttura della singola carta
  12.     {
  13.         public int X;
  14.         public int Y;
  15.         public int Width;
  16.         public int Height;
  17.         public Color Color;
  18.         public bool Turned;
  19.     }
  20.     struct Cards //struttura del mazzo di carte con all'interno l'array di carte singole
  21.     {
  22.         public int Num;
  23.         public Card[] cards;
  24.         public int TurnedCards;
  25.     }
  26.     struct Color
  27.     {
  28.         public byte R;
  29.         public byte G;
  30.         public byte B;
  31.     }
  32.     class Program
  33.     {
  34.         static Color Colors(ColorType color) //tira fuori il colore
  35.         {
  36.             Color c;
  37.             c.R = 0;
  38.             c.G = 0;
  39.             c.B = 0;
  40.             switch (color)
  41.             {
  42.                 case Memory.ColorType.White:
  43.                     c.R = 255;
  44.                     c.G = 255;
  45.                     c.B = 255;
  46.                     break;
  47.                 case Memory.ColorType.Blue:
  48.                     c.R = 0;
  49.                     c.G = 0;
  50.                     c.B = 255;
  51.                     break;
  52.                 case Memory.ColorType.Green:
  53.                     c.R = 0;
  54.                     c.G = 255;
  55.                     c.B = 0;
  56.                     break;
  57.                 case Memory.ColorType.Red:
  58.                     c.R = 255;
  59.                     c.G = 0;
  60.                     c.B = 0;
  61.                     break;
  62.                 case Memory.ColorType.Yellow:
  63.                     c.R = 0;
  64.                     c.G = 255;
  65.                     c.B = 255;
  66.                     break;
  67.                 case Memory.ColorType.Purple:
  68.                     c.R = 255;
  69.                     c.G = 0;
  70.                     c.B = 255;
  71.                     break;
  72.                 case Memory.ColorType.Card:
  73.                     c.R = 100;
  74.                     c.G = 140;
  75.                     c.B = 255;
  76.                     break;
  77.             }
  78.             return c;
  79.         }
  80.         static void Clear(Window window)   //metodo per la finestra di "gioco"
  81.         {
  82.             for (int i = 0; i < window.bitmap.Length; i++)
  83.             {
  84.                 window.bitmap[i] = 0;
  85.             }
  86.         }
  87.         static void PutPixel(Window window, int x, int y, Color color) //disegna il singolo pixel colorato
  88.         {
  89.             if(x > 0 && y > 0 && x < window.width && y < window.height)
  90.             {
  91.                 int index = (y * window.width + x) * 3;
  92.                 window.bitmap[index] = color.R;
  93.                 window.bitmap[index + 1] = color.G;
  94.                 window.bitmap[index + 2] = color.B;
  95.             }
  96.         }
  97.         static void DrawHorizontalLine(Window window, int x, int y, int width, Color color) //disegna una linea orizzontale
  98.         {
  99.             for (int i = x; i < width; i++)
  100.             {
  101.                 PutPixel(window, i, y, color);
  102.             }
  103.         }      
  104.         static void InitCards(out Cards cards, int num, int turnedCards) //inizializza il mazzo di carte
  105.         {
  106.             cards.Num = num;
  107.             cards.cards = new Card[num];
  108.             cards.TurnedCards = turnedCards;
  109.         }
  110.         static void InitCarta(out Card card, int x, int y, int width, int height, Color color, bool turned) //inizializza la singola carta
  111.         {
  112.             card.X = x;
  113.             card.Y = y;
  114.             card.Width = width;
  115.             card.Height = height;
  116.             card.Turned = turned;
  117.             card.Color = color;
  118.         }
  119.         static void ChooseColor(int a, out ColorType color) //tira fuori il colore da assegnare alle carte, devo renderlo random dopo
  120.         {
  121.             color = ColorType.Card;
  122.             if (a == 3 || a == 8) color = ColorType.Blue;
  123.             if (a == 5 || a == 10) color = ColorType.White;
  124.             if (a == 0 || a == 7) color = ColorType.Green;
  125.             if (a == 9 || a == 1) color = ColorType.Purple;
  126.             if (a == 6 || a == 11) color = ColorType.Yellow;
  127.             if (a == 4 || a == 2) color = ColorType.Red;                      
  128.         }
  129.         static void CheckNumCards(int numCards, ref int c, ref int r) //crea le colonne e righe a seconda del numero di carte che viene dato
  130.         {
  131.             if (numCards == 8)
  132.             {
  133.                 c = 4;
  134.                 r = 2;
  135.             }
  136.             if (numCards == 12)
  137.             {
  138.                 c = 3;
  139.                 r = 4;
  140.             }
  141.             if (numCards == 16)
  142.             {
  143.                 c = 4;
  144.                 r = 4;
  145.             }
  146.            
  147.         }
  148.         static void DrawCards(Window window, ref Cards mazzo, Card carta) //disegna il mazzo di carte
  149.         {
  150.             int r=1;
  151.             int c=1;
  152.             int num1 = 1;
  153.             int num2 = 1;
  154.             int z = carta.X;
  155.             int i = 0;
  156.             CheckNumCards(mazzo.Num, ref c, ref r);
  157.             ColorType cType;
  158.            
  159.             while (num2 <= r) //ho creato questi cicli per far si che disegnasse riga per riga le carte, funziona, anche se non so quanto sia giusto
  160.             {
  161.                  while (num1 <= c)
  162.                  {
  163.                     ChooseColor(i, out cType);
  164.                     Color color = Colors(cType);
  165.                     InitCarta(out mazzo.cards[i], carta.X, carta.Y, carta.Width, carta.Height, color, carta.Turned);
  166.                     Input(window, ref mazzo); //metodo per il click del mouse, se clicko la carta si gira
  167.                     Game(ref mazzo); //se la carta è girata cambia colore
  168.                     DrawSingleCard(window, mazzo.cards[i]);
  169.                     carta.X += carta.Width + 20;
  170.                     num1++;
  171.                     i++;
  172.                  }
  173.                  num1 = 1;
  174.                  carta.X = z;
  175.                  carta.Y += carta.Height + 20;
  176.                  num2++;
  177.              }
  178.            
  179.                                        
  180.         }
  181.         static int AskForCards() //chiedo il numero di carte
  182.         {
  183.             Console.WriteLine("Con quante carte vuoi giocare?\n- 8\n- 12\n- 16");
  184.             int numCards = int.Parse(Console.ReadLine());
  185.             if(numCards != 8 && numCards != 12 && numCards != 16)
  186.             {
  187.                 Console.WriteLine("Inserire un numero di carte tra quelli proposti");
  188.                 AskForCards();
  189.             }
  190.             return numCards;
  191.         }
  192.         static void DrawSingleCard(Window window, Card carta) //disegna la singola carta
  193.         {
  194.             for (int i = carta.Y; i < carta.Y + carta.Height; i++)
  195.             {
  196.                 for (int j = carta.X; j < carta.X + carta.Width; j++)
  197.                 {
  198.                     PutPixel(window, j, i, carta.Color);
  199.                 }
  200.             }
  201.         }
  202.         static void Input(Window window, ref Cards mazzo) //a seconda se clicko o meno sulla carta richiama il metodo che la gira
  203.         {
  204.             bool leftClicked = false;
  205.             for (int i = 0; i < mazzo.cards.Length; i++)
  206.             {
  207.                 if (window.mouseX >= mazzo.cards[i].X && window.mouseY >= mazzo.cards[i].Y) //controllo se il cursore è fuori dal rettangolo
  208.                 {
  209.                     for (int y = window.mouseY; y < mazzo.cards[i].Y + mazzo.cards[i].Height; y++)
  210.                     {
  211.                         for (int x = window.mouseX; x < mazzo.cards[i].X + mazzo.cards[i].Width; x++) //matrice che fa si che il click sia effettivo solo all'interno del triangolo
  212.                         {
  213.                             if (window.mouseLeft)
  214.                             {
  215.                                 if (!leftClicked)
  216.                                 {
  217.                                     mazzo.cards[i].Turned = true;
  218.                                     leftClicked = true;
  219.                                 }
  220.                             }
  221.                             //else if (leftClicked)
  222.                             //{
  223.                             //    leftClicked = false;
  224.                             //}
  225.                         }
  226.                     }
  227.                 }
  228.             }
  229.         }
  230.         static void Game(ref Cards mazzo) //serve a cambiare il colore a seconda se è girata o meno la carta
  231.         {
  232.  
  233.             for (int i = 0; i < mazzo.cards.Length; i++)
  234.             {
  235.                 if (mazzo.cards[i].Turned == true)
  236.                 {
  237.                     mazzo.cards[i].Color = mazzo.cards[i].Color;
  238.                 }
  239.                 else
  240.                 {
  241.                     mazzo.cards[i].Color = Colors(ColorType.Card);
  242.                 }
  243.             }
  244.         }
  245.         static void Main(string[] args)
  246.         {
  247.             Window window = new Window(800, 600, "Memory", PixelFormat.RGB);
  248.             Random random = new Random();
  249.  
  250.             Color green;
  251.             green.R = 0;
  252.             green.G = 255;
  253.             green.B = 0;
  254.  
  255.             Card carta;
  256.             carta.Width = 60;
  257.             carta.Height = 80;
  258.             carta.Turned = false;
  259.             carta.X = 20;
  260.             carta.Y = 20;
  261.             carta.Color = Colors(ColorType.Blue);
  262.  
  263.            
  264.             Cards mazzo;
  265.             InitCards(out mazzo, AskForCards(), 0);
  266.          
  267.  
  268.             bool leftClicked = false;
  269.  
  270.             while (window.opened)
  271.             {                                
  272.                 Clear(window);
  273.  
  274.                 DrawCards(window, ref mazzo, carta);
  275.                
  276.                 window.Blit();
  277.             }          
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement