* * *

Author Topic: Moving shape with keys  (Read 3122 times)

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Moving shape with keys
« Reply #15 on: April 29, 2018, 04:48:43 pm »
Is it possible somehow to get instant and continuous reaction without the little pause?
That happens because you use OnKeyDown event, don't use that.

The code below should works as what you want now:

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, LCLType, LclIntf, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Shape1: TShape;
  16.     Timer1: TTimer;
  17.     procedure Timer1Timer(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. type
  25.   TDirection = (dirLeft, dirRight, dirNone);
  26.  
  27. var
  28.   Form1: TForm1;
  29.   Direction: TDirection = dirNone;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.Timer1Timer(Sender: TObject);
  38. begin
  39.   if GetKeyState(VK_LSHIFT) < 0 then Direction := dirLeft;
  40.   if GetKeyState(VK_RSHIFT) < 0 then Direction := dirRight;
  41.   case Direction of
  42.     dirLeft:  Shape1.Left := Shape1.Left - 3;
  43.     dirRight: Shape1.Left := Shape1.Left + 3;
  44.   end;
  45.   if (Shape1.Left < -70)   then Shape1.Left := Width;
  46.   if (Shape1.Left > Width) then Shape1.Left := -70;
  47.   Direction := dirNone;
  48. end;
  49.  
  50. end.

Edit:
Sorry I didn't read your post carefully. I now saw you use Left/Right keys, you should change the keys in my example to VK_RIGHT and VK_LEFT.

@Handoko: this is fantastic, I implemented it and works like the dream!
Thank you!

Handoko

  • Hero Member
  • *****
  • Posts: 2598
  • My goal: build my own game engine using Lazarus
Re: Moving shape with keys
« Reply #16 on: April 29, 2018, 04:52:23 pm »
I saw you're interested in game programming. You should join the Game Contest 2018:
http://forum.lazarus.freepascal.org/index.php/topic,39495.0.html

Handoko

  • Hero Member
  • *****
  • Posts: 2598
  • My goal: build my own game engine using Lazarus
Re: Moving shape with keys
« Reply #17 on: April 29, 2018, 05:13:01 pm »
For newbies, this thread in the link below is valuable if you want to learn game programming:
http://forum.lazarus.freepascal.org/index.php/topic,38136.msg258290.html#msg258290

It shows you how to build a snake game, things can be learn from there:
- Using array to represent the game world
- Game loop
- Collision detection
- Dynamic array
- TList
- Random
- Using data buffer to store keyboard input
- How to reduce flickering

The thread is very long, but it's worth especially for beginners.

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Moving shape with keys
« Reply #18 on: April 29, 2018, 05:16:29 pm »
For newbies, this thread in the link below is valuable if you want to learn game programming:
http://forum.lazarus.freepascal.org/index.php/topic,38136.msg258290.html#msg258290

It shows you how to build a snake game, things can be learn from there:
- Using array to represent the game world
- Game loop
- Collision detection
- Dynamic array
- TList
- Random
- Using data buffer to store keyboard input
- How to reduce flickering

The thread is very long, but it's worth especially for beginners.

Great, thank you, I will study it!
Anyway, here is "my" tennis game (mostly aquired from a video):
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls, uplaysound, LclType, LclIntf;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     lblRestart: TLabel;
  17.     field: TImage;
  18.     lblGameOver: TLabel;
  19.     lblScore: TLabel;
  20.     playsound1: Tplaysound;
  21.     Racket: TShape;
  22.     Ball: TShape;
  23.     tmrGame: TTimer;
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure lblRestartClick(Sender: TObject);
  26.     procedure lblRestartMouseEnter(Sender: TObject);
  27.     procedure lblRestartMouseLeave(Sender: TObject);
  28.     procedure tmrGameTimer(Sender: TObject);
  29.   private
  30.     procedure initGame;
  31.     procedure updateScore;
  32.     procedure gameOver;
  33.     procedure increaseSpeed;
  34.   public
  35.  
  36.   end;
  37.  
  38. type
  39.   TDirection = (dirLeft, dirRight, dirNone);
  40.  
  41. var
  42.   Form1: TForm1;
  43.   score: integer;
  44.   speedX, speedY: integer;
  45.   step: byte = 8;
  46.   Direction: TDirection = dirNone;
  47.  
  48. implementation
  49.  
  50. {$R *.lfm}
  51.  
  52. { TForm1 }
  53.  
  54. procedure TForm1.FormCreate(Sender: TObject);
  55. begin
  56.   DoubleBuffered := True;
  57.   Randomize;
  58.   initGame;
  59. end;
  60.  
  61. procedure TForm1.lblRestartClick(Sender: TObject);
  62. begin
  63.   initGame;
  64. end;
  65.  
  66. procedure TForm1.lblRestartMouseEnter(Sender: TObject);
  67. begin
  68.   lblRestart.Font.Style := [fsBold, fsUnderline];
  69. end;
  70.  
  71. procedure TForm1.lblRestartMouseLeave(Sender: TObject);
  72. begin
  73.   lblRestart.Font.Style := [];
  74. end;
  75.  
  76. procedure TForm1.initGame;
  77. var
  78.   startPos: integer;
  79. begin
  80.   score := 0;
  81.   speedX := -4;
  82.   speedY := -4;
  83.  
  84.   startPos := ClientWidth div 2 + Random(2 * 200) - 200;
  85.  
  86.   Racket.Left := startPos - Racket.Width div 2;
  87.   Racket.Top := ClientHeight - Racket.Height - 2;
  88.  
  89.   Ball.Left := startPos - Ball.Width div 2;
  90.   Ball.Top := Racket.Top - Ball.Height - 2;
  91.  
  92.   lblGameOver.Visible := False;
  93.   lblRestart.Visible := False;
  94.   lblRestart.Font.Style := [];
  95.  
  96.   updateScore;
  97.   Cursor := crNone;
  98.   field.Cursor := crNone;
  99.   tmrGame.Enabled := True;
  100. end;
  101.  
  102. procedure TForm1.updateScore;
  103. begin
  104.   lblScore.Caption := IntToStr(score);
  105. end;
  106.  
  107. procedure TForm1.increaseSpeed;
  108. begin
  109.   if speedX > 0 then
  110.     Inc(speedX)
  111.   else
  112.     Dec(speedX);
  113.   if speedY > 0 then
  114.     Inc(speedY)
  115.   else
  116.     Dec(speedY);
  117. end;
  118.  
  119. procedure TForm1.gameOver;
  120. begin
  121.   tmrGame.Enabled := False;
  122.   lblGameOver.Visible := True;
  123.   lblRestart.Visible := True;
  124.   Cursor := crDefault;
  125.   field.Cursor := crDefault;
  126. end;
  127.  
  128. procedure TForm1.tmrGameTimer(Sender: TObject);
  129. begin
  130.   if GetKeyState(VK_LSHIFT) < 0 then
  131.     Direction := dirLeft;
  132.   if GetKeyState(VK_RSHIFT) < 0 then
  133.     Direction := dirRight;
  134.   case Direction of
  135.     dirLeft: if Racket.Left >= step then
  136.         Racket.Left := Racket.Left - step;
  137.     dirRight: if Racket.Left + Racket.Width <= ClientWidth - step then
  138.         Racket.Left := Racket.Left + step;
  139.   end;
  140.   Direction := dirNone;
  141.  
  142.   Ball.Left := Ball.Left + speedX;
  143.   Ball.Top := Ball.Top + speedY;
  144.  
  145.   //Walls
  146.   if Ball.Top <= 0 then
  147.   begin
  148.     playsound1.SoundFile := 'wall1.wav';
  149.     playsound1.Execute;
  150.     speedY := -speedY;
  151.   end;
  152.  
  153.   if (Ball.Left <= 0) or (Ball.Left + Ball.Width >= ClientWidth) then
  154.   begin
  155.     playsound1.SoundFile := 'wall1.wav';
  156.     playsound1.Execute;
  157.     speedX := -speedX;
  158.   end;
  159.  
  160.  
  161.   //Game over
  162.   if Ball.Top + Ball.Height > Racket.Top + Racket.Height div 2 then
  163.   begin
  164.     playsound1.SoundFile := 'gameover2.wav';
  165.     playsound1.Execute;
  166.     gameOver;
  167.   end;
  168.  
  169.  
  170.   //Good hit
  171.   if (Ball.Left + Ball.Width > Racket.Left) and (Ball.Left < Racket.Left + Racket.Width) and (Ball.Top + Ball.Height >= Racket.Top) then
  172.   begin
  173.     playsound1.SoundFile := 'hit.wav';
  174.     playsound1.Execute;
  175.     speedY := -speedY;
  176.     increaseSpeed;
  177.     Inc(score);
  178.     updateScore;
  179.   end;
  180. end;
  181.  
  182.  
  183. end.
« Last Edit: April 29, 2018, 05:21:11 pm by justnewbie »

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Moving shape with keys
« Reply #19 on: April 29, 2018, 05:43:19 pm »

Handoko

  • Hero Member
  • *****
  • Posts: 2598
  • My goal: build my own game engine using Lazarus
Re: Moving shape with keys
« Reply #20 on: April 29, 2018, 05:49:40 pm »
@justnewbie

Your tennis game looks much better than my first game arkanoid-clone. You should enter the game contest!

Because you didn't provide the resource files (audio, image and .lfm), it can't run as what you designed. See the image below.

Here is how you should do if you want to publish your source code here:
Copy all the files (including images and audios) in to a new folder, except: *.bak, the binary and the lib folder. Compress the folder and attach the zip file to the forum.

Alternatifly, you can use Publish Project feature in the Lazarus IDE (which is a bit technical if you never use it):
Lazarus main menu > Project > Publish

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Moving shape with keys
« Reply #21 on: April 29, 2018, 06:22:35 pm »
LOL, I and the Game Contest ... funny! I'm a newbie, so it would be a bit bigger challenge than optimal. Maybe later!  :)
Because of the file size limit here, I uploaded THE GAME here: https://ufile.io/n4rxa
Guaranteed enjoy for years! Attention: heavily addictive GAME!  :D
« Last Edit: April 29, 2018, 06:28:06 pm by justnewbie »

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Moving shape with keys
« Reply #22 on: April 29, 2018, 08:49:58 pm »
There are newbies in the game contest, just check the entries.

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Moving shape with keys
« Reply #23 on: April 29, 2018, 11:27:27 pm »
Really thank you for the kind invitations, and honestly, I'd like to participate in it. But, my knowledge is very humble and my free time is not so much currently.  :)

Handoko

  • Hero Member
  • *****
  • Posts: 2598
  • My goal: build my own game engine using Lazarus
Re: Moving shape with keys
« Reply #24 on: April 30, 2018, 06:31:48 am »
By participating the contest, you will learn lots of new tricks in programming. Your program will be tested by users who use different hardware and OSes. You will learn about compatibility issue, deployment, user experience and many more.

Last year, I submitted 2 programs for the contest. I thought my programs were working good but no, I was wrong. I used TImage's built-in image loader for my glSlideshow, but now I know it has some issues when opening certain tiff files. That bug forced me to learn FPimage unit, which has better support for loading tiff files. Also an experienced OpenGL programmer suggested me to use modern OpenGL, he even rewrote my program showing how to program using it. I still have problem understanding modern OpenGL concept, but I bookmarked his posts for later studying.

The second program was a simple game created using Allegro library. It worked on my computer but not on others. Because I had not properly did the deployment. Also, an user told me that the display of my game was too small when running on his large screen monitor. That made me adding double size display feature.

See, even I have been learning graphics and game programming for years, I still learned many new things participating the contest.

It's not about to be the winner. It's about learning new things that you won't get from reading.

Your Tennis game has potential, it already has background texture and audio. If I'm allowed, I will suggest adding some random elements. For example in my arkanoid-clone, there were some objects that will cause random direction and speed if being hit, some others are for surprise, for example double even triple the balls that appear on the screen. And the bounce angle should not be constant, if it hit near the edge of the racket, it should has sharp angle. Also if you implement game state, it can have pause, demo and etc.

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Moving shape with keys
« Reply #25 on: April 30, 2018, 09:25:03 am »
By participating the contest, you will learn lots of new tricks in programming. Your program will be tested by users who use different hardware and OSes. You will learn about compatibility issue, deployment, user experience and many more.

Last year, I submitted 2 programs for the contest. I thought my programs were working good but no, I was wrong. I used TImage's built-in image loader for my glSlideshow, but now I know it has some issues when opening certain tiff files. That bug forced me to learn FPimage unit, which has better support for loading tiff files. Also an experienced OpenGL programmer suggested me to use modern OpenGL, he even rewrote my program showing how to program using it. I still have problem understanding modern OpenGL concept, but I bookmarked his posts for later studying.

The second program was a simple game created using Allegro library. It worked on my computer but not on others. Because I had not properly did the deployment. Also, an user told me that the display of my game was too small when running on his large screen monitor. That made me adding double size display feature.

See, even I have been learning graphics and game programming for years, I still learned many new things participating the contest.

It's not about to be the winner. It's about learning new things that you won't get from reading.

Your Tennis game has potential, it already has background texture and audio. If I'm allowed, I will suggest adding some random elements. For example in my arkanoid-clone, there were some objects that will cause random direction and speed if being hit, some others are for surprise, for example double even triple the balls that appear on the screen. And the bounce angle should not be constant, if it hit near the edge of the racket, it should has sharp angle. Also if you implement game state, it can have pause, demo and etc.
Very true viewpoints, you are absolutely right in all aspects. But, the time is limited that I can take for it. Maybe later!  :-X
« Last Edit: April 30, 2018, 09:47:48 am by justnewbie »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus