Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Aiv.Draw;
- namespace Memory
- {
- enum ColorType { White, Blue, Green, Red, Yellow, Purple, Card } //enumerato per i colori
- struct Card //struttura della singola carta
- {
- public int X;
- public int Y;
- public int Width;
- public int Height;
- public Color Color;
- public bool Turned;
- }
- struct Cards //struttura del mazzo di carte con all'interno l'array di carte singole
- {
- public int Num;
- public Card[] cards;
- public int TurnedCards;
- }
- struct Color
- {
- public byte R;
- public byte G;
- public byte B;
- }
- class Program
- {
- static Color Colors(ColorType color) //tira fuori il colore
- {
- Color c;
- c.R = 0;
- c.G = 0;
- c.B = 0;
- switch (color)
- {
- case Memory.ColorType.White:
- c.R = 255;
- c.G = 255;
- c.B = 255;
- break;
- case Memory.ColorType.Blue:
- c.R = 0;
- c.G = 0;
- c.B = 255;
- break;
- case Memory.ColorType.Green:
- c.R = 0;
- c.G = 255;
- c.B = 0;
- break;
- case Memory.ColorType.Red:
- c.R = 255;
- c.G = 0;
- c.B = 0;
- break;
- case Memory.ColorType.Yellow:
- c.R = 0;
- c.G = 255;
- c.B = 255;
- break;
- case Memory.ColorType.Purple:
- c.R = 255;
- c.G = 0;
- c.B = 255;
- break;
- case Memory.ColorType.Card:
- c.R = 100;
- c.G = 140;
- c.B = 255;
- break;
- }
- return c;
- }
- static void Clear(Window window) //metodo per la finestra di "gioco"
- {
- for (int i = 0; i < window.bitmap.Length; i++)
- {
- window.bitmap[i] = 0;
- }
- }
- static void PutPixel(Window window, int x, int y, Color color) //disegna il singolo pixel colorato
- {
- if(x > 0 && y > 0 && x < window.width && y < window.height)
- {
- int index = (y * window.width + x) * 3;
- window.bitmap[index] = color.R;
- window.bitmap[index + 1] = color.G;
- window.bitmap[index + 2] = color.B;
- }
- }
- static void DrawHorizontalLine(Window window, int x, int y, int width, Color color) //disegna una linea orizzontale
- {
- for (int i = x; i < width; i++)
- {
- PutPixel(window, i, y, color);
- }
- }
- static void InitCards(out Cards cards, int num, int turnedCards) //inizializza il mazzo di carte
- {
- cards.Num = num;
- cards.cards = new Card[num];
- cards.TurnedCards = turnedCards;
- }
- static void InitCarta(out Card card, int x, int y, int width, int height, Color color, bool turned) //inizializza la singola carta
- {
- card.X = x;
- card.Y = y;
- card.Width = width;
- card.Height = height;
- card.Turned = turned;
- card.Color = color;
- }
- static void ChooseColor(int a, out ColorType color) //tira fuori il colore da assegnare alle carte, devo renderlo random dopo
- {
- color = ColorType.Card;
- if (a == 3 || a == 8) color = ColorType.Blue;
- if (a == 5 || a == 10) color = ColorType.White;
- if (a == 0 || a == 7) color = ColorType.Green;
- if (a == 9 || a == 1) color = ColorType.Purple;
- if (a == 6 || a == 11) color = ColorType.Yellow;
- if (a == 4 || a == 2) color = ColorType.Red;
- }
- 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
- {
- if (numCards == 8)
- {
- c = 4;
- r = 2;
- }
- if (numCards == 12)
- {
- c = 3;
- r = 4;
- }
- if (numCards == 16)
- {
- c = 4;
- r = 4;
- }
- }
- static void DrawCards(Window window, ref Cards mazzo, Card carta) //disegna il mazzo di carte
- {
- int r=1;
- int c=1;
- int num1 = 1;
- int num2 = 1;
- int z = carta.X;
- int i = 0;
- CheckNumCards(mazzo.Num, ref c, ref r);
- ColorType cType;
- 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
- {
- while (num1 <= c)
- {
- ChooseColor(i, out cType);
- Color color = Colors(cType);
- InitCarta(out mazzo.cards[i], carta.X, carta.Y, carta.Width, carta.Height, color, carta.Turned);
- Input(window, ref mazzo); //metodo per il click del mouse, se clicko la carta si gira
- Game(ref mazzo); //se la carta è girata cambia colore
- DrawSingleCard(window, mazzo.cards[i]);
- carta.X += carta.Width + 20;
- num1++;
- i++;
- }
- num1 = 1;
- carta.X = z;
- carta.Y += carta.Height + 20;
- num2++;
- }
- }
- static int AskForCards() //chiedo il numero di carte
- {
- Console.WriteLine("Con quante carte vuoi giocare?\n- 8\n- 12\n- 16");
- int numCards = int.Parse(Console.ReadLine());
- if(numCards != 8 && numCards != 12 && numCards != 16)
- {
- Console.WriteLine("Inserire un numero di carte tra quelli proposti");
- AskForCards();
- }
- return numCards;
- }
- static void DrawSingleCard(Window window, Card carta) //disegna la singola carta
- {
- for (int i = carta.Y; i < carta.Y + carta.Height; i++)
- {
- for (int j = carta.X; j < carta.X + carta.Width; j++)
- {
- PutPixel(window, j, i, carta.Color);
- }
- }
- }
- static void Input(Window window, ref Cards mazzo) //a seconda se clicko o meno sulla carta richiama il metodo che la gira
- {
- bool leftClicked = false;
- for (int i = 0; i < mazzo.cards.Length; i++)
- {
- if (window.mouseX >= mazzo.cards[i].X && window.mouseY >= mazzo.cards[i].Y) //controllo se il cursore è fuori dal rettangolo
- {
- for (int y = window.mouseY; y < mazzo.cards[i].Y + mazzo.cards[i].Height; y++)
- {
- 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
- {
- if (window.mouseLeft)
- {
- if (!leftClicked)
- {
- mazzo.cards[i].Turned = true;
- leftClicked = true;
- }
- }
- //else if (leftClicked)
- //{
- // leftClicked = false;
- //}
- }
- }
- }
- }
- }
- static void Game(ref Cards mazzo) //serve a cambiare il colore a seconda se è girata o meno la carta
- {
- for (int i = 0; i < mazzo.cards.Length; i++)
- {
- if (mazzo.cards[i].Turned == true)
- {
- mazzo.cards[i].Color = mazzo.cards[i].Color;
- }
- else
- {
- mazzo.cards[i].Color = Colors(ColorType.Card);
- }
- }
- }
- static void Main(string[] args)
- {
- Window window = new Window(800, 600, "Memory", PixelFormat.RGB);
- Random random = new Random();
- Color green;
- green.R = 0;
- green.G = 255;
- green.B = 0;
- Card carta;
- carta.Width = 60;
- carta.Height = 80;
- carta.Turned = false;
- carta.X = 20;
- carta.Y = 20;
- carta.Color = Colors(ColorType.Blue);
- Cards mazzo;
- InitCards(out mazzo, AskForCards(), 0);
- bool leftClicked = false;
- while (window.opened)
- {
- Clear(window);
- DrawCards(window, ref mazzo, carta);
- window.Blit();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement