Recent

Author Topic: Rock,Paper,Scissors  (Read 10645 times)

s158

  • New Member
  • *
  • Posts: 10
Rock,Paper,Scissors
« on: May 03, 2018, 04:39:03 pm »
Hey guys, a bit new to Lazarus here and am trying to make a Rock,Paper,Scissors program. Can someone help me out?
I am in Lazarus.
I'm trying to make a program that'll load an image of rock,paper,scissors  randomly on both boxes above(see the attachment) and at the same time will display who won, lost or draw on the boxes below when the button is pressed. So yep it's like a comp vs comp game. Any help is appreciated!

Uhh,sorry for my bad english/ grammar. Its not my native language.
Also, sorry if this is not the right section to post this stuff.

Handoko

  • Hero Member
  • *****
  • Posts: 5377
  • My goal: build my own game engine using Lazarus
Re: Rock,Paper,Scissors
« Reply #1 on: May 03, 2018, 05:39:20 pm »
Hello s158,
Welcome to the forum.

You first need the pictures. Have you drawn them? Or maybe you can get some from OpenClipArt:
https://openclipart.org/search/?query=hand+scissor

It is easier to use bmp or png or jpg images than the others. So if the pictures you got aren't in these formats, you should convert them.

Do you understand how to use Random in Pascal? If not, you may need to read this:
https://www.freepascal.org/docs-html/rtl/system/random.html

You may need to use a TImageList to store the images, read more here:
http://wiki.freepascal.org/TImageList

The tutorial in the TImageList link above is a bit too technical. What you need for showing the image is TImageList.Draw or TImageList.GetBitmap (depends on how you use it). And for loading images from file, you only need to double click the TImageList that you've already put it on the form. But remember, you need to set the size (height and width) before loading any images.

You also need a TTimer. Set the Interval you want and then write your code inside OnTimer event.
http://lazarus-ccr.sourceforge.net/docs/lcl/extctrls/ttimer.html

Have fun.

s158

  • New Member
  • *
  • Posts: 10
Re: Rock,Paper,Scissors
« Reply #2 on: May 03, 2018, 05:57:39 pm »
Thx, please can you explain me what should i give to timer?

balazsszekely

  • Guest
Re: Rock,Paper,Scissors
« Reply #3 on: May 03, 2018, 06:05:33 pm »
@s158
First of all you should really read what @Handoko posted,  basically it covers everything you need.
Secondly if you are a beginner it can be overwhelming, so I decided to help you a little bit, but I'm not gonna do all the work for you:
Code: Pascal  [Select][+][-]
  1. function RandomRPS: Integer;
  2. begin
  3.   Result := (Random(1000) + 1) mod 3;
  4. end;
  5.  
  6. function WhoWins(const R1, R2: Integer): String;
  7. begin
  8.   case R2 - R1 of
  9.         0: Result := 'Draw.';
  10.     -2, 1: Result := 'First wins.';
  11.     -1, 2: Result := 'Second wins.';
  12.   end;
  13. end;
  14.  
  15. procedure Play;
  16. const
  17.   RPS:  array[0..2] of String = ('Rock', 'Scissor', 'Paper');
  18. var
  19.   R1, R2: Integer;
  20. begin
  21.   R1 := RandomRPS;
  22.   R2 := RandomRPS;
  23.   ShowMessage('1: ' + RPS[R1] + sLineBreak +
  24.               '2: ' + RPS[R2] + sLineBreak +
  25.               WhoWins(R1, R2);
  26. end;
  27.  
  28. procedure TForm1.Button1Click(Sender: TObject);
  29. begin
  30.   Play;
  31. end;
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35.   Randomize;
  36. end;

To do: Load the images, add a timer if you wish to repeat the process multiple times, etc...

PS: I did not test the code myself. It may contain bugs.
« Last Edit: May 03, 2018, 06:08:21 pm by GetMem »

s158

  • New Member
  • *
  • Posts: 10
Re: Rock,Paper,Scissors
« Reply #4 on: May 03, 2018, 06:41:05 pm »
I am trying to understanding it, but i am not, please can you give me yet some more advice? Thx

balazsszekely

  • Guest
Re: Rock,Paper,Scissors
« Reply #5 on: May 03, 2018, 06:46:35 pm »
Quote
I am trying to understanding it, but i am not, please can you give me yet some more advice?
What advice? Basically the problem is solved in 80%. I'm not gonna do the rest. It looks like you don't want to learn, you're here for a quick fix.

Handoko

  • Hero Member
  • *****
  • Posts: 5377
  • My goal: build my own game engine using Lazarus
Re: Rock,Paper,Scissors
« Reply #6 on: May 03, 2018, 06:47:05 pm »
@s158

Wait, I'm preparing ...

s158

  • New Member
  • *
  • Posts: 10
Re: Rock,Paper,Scissors
« Reply #7 on: May 03, 2018, 07:00:47 pm »
Thx Handoko.  :)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Rock,Paper,Scissors
« Reply #8 on: May 03, 2018, 07:02:15 pm »
Here's one way to go about the task. No timer. And you'll want better images.

s158

  • New Member
  • *
  • Posts: 10
Re: Rock,Paper,Scissors
« Reply #9 on: May 03, 2018, 07:21:19 pm »
Thx to all, please can you help me yet with one more, its also app rock,paper,scissors.
Using images, in component image will be specific picture of rock,paper or scissors.
One image will be for random choose by pc, second image will be choice of player.
There will be 3 next small components Image, where will be also on1.rock,2.paper and 3.scissors
After clicking on one of small images, will be process of select by pc and comparing results.
Winner will add point, in case of draw no points are added.
Score will be always shown in component Label.
The game ends, if player gets 5 points.
Add too button for run again program and end application.
Thx you very much for your help.

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Rock,Paper,Scissors
« Reply #10 on: May 03, 2018, 07:37:43 pm »
Hi, this one is my try:


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6.  
  7. interface
  8.  
  9.  
  10. uses
  11.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  12.   StdCtrls;
  13.  
  14.  
  15. type
  16.   TGesture = (rock, scissor, paper);
  17.  
  18.  
  19.   { TForm1 }
  20.  
  21.  
  22.   TForm1 = class(TForm)
  23.     btnRun: TButton;
  24.     Image1: TImage;
  25.     Image2: TImage;
  26.     Label1: TLabel;
  27.     Label2: TLabel;
  28.     Label3: TLabel;
  29.     Label4: TLabel;
  30.     procedure btnRunClick(Sender: TObject);
  31.     procedure FormCreate(Sender: TObject);
  32.   private
  33.     { private declarations }
  34.     FBitmaps : array[TGesture] of TBitmap;
  35.   public
  36.     { public declarations }
  37.   end;
  38.  
  39.  
  40. var
  41.   Form1: TForm1;
  42.   Gesture: array[TGesture] of string = ('Rock', 'Scissor', 'Paper');
  43.   Rule: array[TGesture,TGesture] of string = (
  44.     ('Rocks.', //0=0',
  45.      'Rock breaks Scissor.', // 0~1', //
  46.      'Rock wrapped by Paper.' // 0~2'
  47.      ),
  48.    ('Scissor broken by Rock.', // 1~0',
  49.     'Scissors.', // 1=1',
  50.     'Scissor cuts Paper.' // 1~2'
  51.     ),
  52.     ('Paper wraps Rock.', // 2~0',
  53.      'Paper cutten by Scissor.', // 2~1',
  54.      'Papers.' // 2=2'
  55.      )
  56.     );
  57.  
  58.  
  59.  
  60.  
  61. implementation
  62. uses math;
  63. {$R *.lfm}
  64.  
  65.  
  66. { TForm1 }
  67.  
  68.  
  69. procedure TForm1.FormCreate(Sender: TObject);
  70. var i : integer;
  71. begin
  72.   FBitmaps[rock] := TBitmap.Create();
  73.   FBitmaps[rock].LoadFromFile('images\Shape\rock.bmp');
  74.   FBitmaps[scissor] := TBitmap.Create();
  75.   FBitmaps[scissor].LoadFromFile('images\Shape\scissor.bmp');
  76.   FBitmaps[paper] := TBitmap.Create();
  77.   FBitmaps[paper].LoadFromFile('images\Shape\paper.bmp');
  78.   //Image1.Picture.LoadFromFile('images\Shape\paper.bmp');
  79.   //Image2.Picture.LoadFromFile('images\Shape\scissor.bmp');
  80. end;
  81.  
  82.  
  83. function Fight(a,b:TGesture):TGesture;
  84. begin
  85.   if ( (a=rock) and (b=paper) ) or ( (b=rock) and (a=paper) ) then
  86.      result := paper
  87.   else
  88.     result := TGesture( min(ord(a),ord(b)));
  89. end;
  90.  
  91.  
  92. procedure TForm1.btnRunClick(Sender: TObject);
  93. var player1,player2, winner : TGesture;
  94. begin
  95.   randomize;
  96.   player1 := TGesture( random(3));
  97.   player2 := TGesture(random(3));
  98.  
  99.  
  100.   Image1.Picture.Assign(FBitmaps[player1]);
  101.   Image2.Picture.Assign(FBitmaps[player2]);
  102.  
  103.  
  104.   label1.Caption:= Gesture[player1];
  105.   label2.Caption:= Gesture[player2];
  106.  
  107.  
  108.   if player1 = player2 then
  109.   begin
  110.      label3.Caption:='Equal. Try again';
  111.      label4.Caption:='';
  112.   end
  113.   else
  114.     begin
  115.       winner := Fight(player1,player2);
  116.       label3.Caption:= 'The Winner is: ' +Gesture[winner];
  117.     end;
  118.  
  119.  
  120.   label4.Caption:=Rule[player1,player2];
  121. end;
  122.  
  123.  
  124. end.      
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Rock,Paper,Scissors
« Reply #11 on: May 03, 2018, 07:38:53 pm »
Thx to all, please can you help me yet with one more...

What prevents you writing this yourself?

s158

  • New Member
  • *
  • Posts: 10
Re: Rock,Paper,Scissors
« Reply #12 on: May 03, 2018, 07:41:10 pm »
I dont understand it all the time, i am trying to learn it but I cant and i promised my friend, that today i will send it to him.
Please, I will be very grateful if you will help me. Thx

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Rock,Paper,Scissors
« Reply #13 on: May 03, 2018, 07:51:00 pm »
Here's one way to go about the task. No timer. And you'll want better images.
memory leak, while form closed.
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Handoko

  • Hero Member
  • *****
  • Posts: 5377
  • My goal: build my own game engine using Lazarus
Re: Rock,Paper,Scissors
« Reply #14 on: May 03, 2018, 08:18:59 pm »
@s158

Here is the explanation about using TTimer.

Beginners usually like to use a TTimer as the main loop in game programming. What is main loop? I guess you're not interested with the theory, so lets go directly to the coding part.

I won't use your rock-paper-scissor game as the example. Instead, I use a simple animation. Below is the code of a simple animation using for-do loop:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Shape1: TShape;
  17.     procedure Button1Click(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. { TForm1 }
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30. var
  31.   i: Integer;
  32. begin
  33.   Shape1.Left := 8;
  34.   for i := 0 to 50 do begin
  35.     Sleep(50); Application.ProcessMessages;
  36.     Shape1.Left := Shape1.Left + 5;
  37.   end;
  38. end;
  39.  
  40. end.
You can download animation-for-do-loop.zip for testing, which I provided at the attachment.

Can you understand the code? The line #35: Sleep(50); Application.ProcessMessages; is used to slowdown the process. The Line #34 is the condition to stop the looping, which is when i = 50.

We can use a for-do looping, while-do, repeat-until, even goto as the main loop. Nothing wrong with them. But using a TTimer has its own advantages, but you're a beginner I won't go to the detail.

 

TinyPortal © 2005-2018