Recent

Author Topic: Simple Memory Color Game  (Read 975 times)

Gigatron

  • Sr. Member
  • ****
  • Posts: 336
  • Amiga Rulez !!
Simple Memory Color Game
« on: May 02, 2025, 10:34:17 pm »
Hi,

Here's a variation of the simple color-based memory game; Useful for human brain health.
Used 16 cells or 8 pair of definable colors 8*2 =16;

Flush routine is based on javascript , you have now the start base of a memory games and add
pictures + sounds, so improve it.

If you look the score i'am not really clever :)

Have fun.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  9.   BGRAVirtualScreen, BGRABitmap,BGRABitmapTypes,mmsystem;
  10.  
  11. const
  12.   GridCols = 4;   // 4*4 = 16 cartes
  13.   GridRows = 4;   //
  14.   CardWidth = 64;
  15.   CardHeight = 64;
  16.   CardSpacing = 10;
  17.   CardColors: array[0..7] of TBGRAPixel = (   // packed record of tbgrapixel ;
  18.     (blue: 0; green: 0; red: 255; alpha: 255),    // Rouge
  19.     (blue: 0; green: 255; red: 0; alpha: 255),    // Vert
  20.     (blue: 255; green: 0; red: 0; alpha: 255),    // Bleu
  21.     (blue: 0; green: 255; red: 255; alpha: 255),  // Jaune
  22.     (blue: 255; green: 0; red: 255; alpha: 255),  // Magenta
  23.     (blue: 255; green: 255; red: 0; alpha: 255),  // Cyan
  24.     (blue: 128; green: 0; red: 128; alpha: 255),  // Violet
  25.     (blue: 0; green: 165; red: 255; alpha: 255)   // Orange
  26.   );
  27.   soundsflag =  snd_Async;  // one shot !!
  28.  
  29. type
  30.   TCard = record
  31.     ImageIndex: Integer;
  32.     IsFlipped: Boolean;
  33.     IsMatched: Boolean;
  34.     Rect: TRect;
  35.   end;
  36.  
  37. type
  38.  
  39.   { TForm1 }
  40.  
  41.   TForm1 = class(TForm)
  42.     BGRAVirtualScreen1: TBGRAVirtualScreen;
  43.     Memo1: TMemo;
  44.     Timer1: TTimer;
  45.     procedure BGRAVirtualScreen1MouseDown(Sender: TObject;
  46.       Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  47.     procedure BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  48.     procedure FormCreate(Sender: TObject);
  49.     procedure Timer1Timer(Sender: TObject);
  50.     procedure Reset;
  51.   private
  52.  
  53.   public
  54.  
  55.   end;
  56.  
  57. var
  58.   Form1: TForm1;
  59.  
  60.   Cards: array[0..GridCols-1, 0..GridRows-1] of TCard;
  61.   CardImages: array of TBGRABitmap;
  62.   BackImage: TBGRABitmap;
  63.   FirstCardX, FirstCardY: Integer;
  64.   SecondCardX, SecondCardY: Integer;
  65.   CardsFlipped: Integer = 0;
  66.   Score : Integer = 0;
  67.   Coup : integer = 0;
  68.   Reste : Integer = 0;
  69.   Partie : Integer = 0;
  70.  
  71. implementation
  72.  
  73. {$R *.lfm}
  74.  
  75. { TForm1 }
  76.  
  77. procedure TForm1.FormCreate(Sender: TObject);
  78. begin
  79.     Reset; // ResetAll & start game
  80.     BGRAVirtualScreen1.OnMouseDown := @BGRAVirtualScreen1MouseDown;
  81. end;
  82.  
  83. procedure TForm1.Timer1Timer(Sender: TObject);
  84. begin
  85.   Timer1.Enabled := False;
  86.  
  87.   // associe les couleurs 2 par deux et teste le resultat
  88.   if (FirstCardX >= 0) and (FirstCardY >= 0) and (SecondCardX >= 0) and (SecondCardY >= 0) then
  89.   begin
  90.     if Cards[FirstCardX, FirstCardY].ImageIndex = Cards[SecondCardX, SecondCardY].ImageIndex then
  91.     begin
  92.       Cards[FirstCardX, FirstCardY].IsMatched := True;
  93.       Cards[SecondCardX, SecondCardY].IsMatched := True;
  94.       playsound('snds/hit.wav',0, soundsflag);// play sound if Matched !!
  95.       Inc(Score);  // score !
  96.       Dec(Reste);
  97.  
  98.     end
  99.     else
  100.     begin
  101.       playsound('snds/lost.wav',0, soundsflag);// play sound if Lost !!
  102.       Cards[FirstCardX, FirstCardY].IsFlipped := False;
  103.       Cards[SecondCardX, SecondCardY].IsFlipped := False;
  104.     end;
  105.   end;
  106.  
  107.   Inc(coup); // coup !
  108.  
  109.   if Reste =0 then
  110.   begin
  111.     inc(partie);
  112.     Memo1.Lines.Add('Round : '+IntToStr(Partie)+ '  Move : ' + IntToStr(Coup));
  113.     Sleep(2000); // wait 2 sec to restart and reset all variables !!
  114.     reset;
  115.   end;
  116.  
  117.   // Reset pour un nouveau tour ;
  118.   FirstCardX := -1;
  119.   FirstCardY := -1;
  120.   SecondCardX := -1;
  121.   SecondCardY := -1;
  122.   CardsFlipped := 0;
  123.   BGRAVirtualScreen1.DiscardBitmap;
  124. end;
  125.  
  126. // affichage permanent
  127. procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  128. var
  129.   i, j: Integer;
  130.   card: TCard;
  131. begin
  132.   //Bitmap.Fill(BGRAWhite);
  133.  
  134.   for i := 0 to GridCols-1 do
  135.     for j := 0 to GridRows-1 do
  136.     begin
  137.       card := Cards[i,j];
  138.       if card.IsFlipped or card.IsMatched then
  139.         Bitmap.PutImage(card.Rect.Left, card.Rect.Top, CardImages[card.ImageIndex], dmSet)
  140.       else
  141.         Bitmap.PutImage(card.Rect.Left, card.Rect.Top, BackImage, dmSet);
  142.     end;
  143.  
  144.    // le score  inutile !
  145.   Bitmap.FontHeight := 20;
  146.   Bitmap.FontStyle := [];
  147.   Bitmap.TextOut(0, GridRows*(CardHeight+CardSpacing) + 10, 'Score : ' + IntToStr(Score), BGRA(0,0,0));
  148.  
  149.   //  les coups
  150.   Bitmap.FontHeight := 20;
  151.   Bitmap.FontStyle := [];
  152.   Bitmap.TextOut(100, GridRows*(CardHeight+CardSpacing) + 10, 'Draw : ' + IntToStr(Coup), BGRA(0,0,0));
  153.  
  154.   //  le restant des paires
  155.   Bitmap.FontHeight := 20;
  156.   Bitmap.FontStyle := [];
  157.   Bitmap.TextOut(200, GridRows*(CardHeight+CardSpacing) + 10, 'Remain : ' + IntToStr(Reste), BGRA(0,0,0));
  158. end;
  159.  
  160. procedure TForm1.BGRAVirtualScreen1MouseDown(Sender: TObject; Button: TMouseButton;
  161.   Shift: TShiftState; X, Y: Integer);
  162. var
  163.   i, j: Integer;
  164. begin
  165.   if CardsFlipped = 2 then Exit;
  166.  
  167.   for i := 0 to GridCols-1 do
  168.     for j := 0 to GridRows-1 do
  169.     begin
  170.       if (X >= Cards[i,j].Rect.Left) and (X < Cards[i,j].Rect.Right) and
  171.    (Y >= Cards[i,j].Rect.Top) and (Y < Cards[i,j].Rect.Bottom) and
  172.    (not Cards[i,j].IsFlipped) and (not Cards[i,j].IsMatched) then
  173.       begin
  174.         Cards[i,j].IsFlipped := True;
  175.  
  176.         if CardsFlipped = 0 then
  177.         begin
  178.           FirstCardX := i;
  179.           FirstCardY := j;
  180.         end
  181.         else if CardsFlipped = 1 then
  182.         begin
  183.           SecondCardX := i;
  184.           SecondCardY := j;
  185.           Timer1.Enabled := True;  // une fois pour test resultat si ok or Not !
  186.         end;
  187.  
  188.         Inc(CardsFlipped);
  189.         BGRAVirtualScreen1.DiscardBitmap;
  190.         Exit;
  191.       end;
  192.     end;
  193. end;
  194. // start game and Reset All
  195. procedure TForm1.Reset;
  196. var
  197.   i, j, k: Integer;
  198.   indexList: array of Integer;
  199. begin
  200.   SetLength(CardImages, 8); // 8 paires pour une grille 4x4 , 16/2=8
  201.  
  202.   // Couleurs à la place des cartes !
  203.   for i := 0 to 7 do
  204.   begin
  205.     CardImages[i] := TBGRABitmap.Create(CardWidth, CardHeight, CardColors[i]);
  206.   end;
  207.  
  208.   // back color of all cards
  209.   BackImage := TBGRABitmap.Create(CardWidth, CardHeight, BGRA(155, 155, 100));
  210.  
  211.   // JS to pascal code
  212.   SetLength(indexList, 16);
  213.   for i := 0 to 15 do
  214.     indexList[i] := i div 2;
  215.   for i := 0 to 15 do
  216.   begin
  217.     j := Random(16);
  218.     k := indexList[i];
  219.     indexList[i] := indexList[j];
  220.     indexList[j] := k;
  221.   end;
  222.  
  223.   // fill cells 4*4
  224.   k := 0;
  225.   for i := 0 to GridCols-1 do
  226.     for j := 0 to GridRows-1 do
  227.     begin
  228.       Cards[i,j].ImageIndex := indexList[k];
  229.       Cards[i,j].IsFlipped := False;
  230.       Cards[i,j].IsMatched := False;
  231.       Cards[i,j].Rect := Rect(i*(CardWidth+CardSpacing), j*(CardHeight+CardSpacing),
  232.                               i*(CardWidth+CardSpacing)+CardWidth,
  233.                               j*(CardHeight+CardSpacing)+CardHeight);
  234.       Inc(k);
  235.     end;
  236.   // reset vars and start game !
  237.   playsound('snds/restart.wav',0, soundsflag); // play start sound
  238.   FirstCardX := -1;
  239.   FirstCardY := -1;
  240.   SecondCardX := -1;
  241.   SecondCardY := -1;
  242.   Reste := 8 ;
  243.   Coup  := 0;
  244.   Score := 0;
  245.  
  246. end;
  247.  
  248.  
  249. end.
  250.  
Trip to Europe...  finished in 40 days !

Guva

  • Full Member
  • ***
  • Posts: 201
  • 🌈 ZX-Spectrum !!!
Re: Simple Memory Color Game
« Reply #1 on: May 04, 2025, 08:26:28 am »
@Gigatron, good job. I needed something to use a couple shaders on.  Rewrote your code a bit :)

https://guvacode.itch.io/candy-memory-game

Gigatron

  • Sr. Member
  • ****
  • Posts: 336
  • Amiga Rulez !!
Re: Simple Memory Color Game
« Reply #2 on: May 04, 2025, 12:16:03 pm »
@Gigatron, good job. I needed something to use a couple shaders on.  Rewrote your code a bit :)

https://guvacode.itch.io/candy-memory-game

@Guva nice job ! The game and shaders are very cool  ; In addition you are a good
programmer.
« Last Edit: May 04, 2025, 01:01:03 pm by Gigatron »
Trip to Europe...  finished in 40 days !

 

TinyPortal © 2005-2018