Author Topic: Here a simple engine for game 2D with horizontal and vertical scrolling  (Read 4195 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi guys
Some times ago following this thread I arrived at this position https://www.qb64.org/forum/index.php?topic=1077.msg102890#msg102890
Just some days after it I found a bug in images manager if user presses continuosly the key without releasing it (the character stucks into a image). Here attached there is the fixing of that problem.
It is a demo of game engine with tecnique of scrolling horizontal for movement to right or to left of character, and scrolling vertical if character jumps.  In the 7zip file attached there are images for graphic demo and the .BAS

Thanks to take a look.
* rpg.7z (Filesize: 1.63 MB, Downloads: 293)
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Looks nice image and action. When there is a jump while moving in a direction, it would look better if the character continued moving in direction after the jump and during the jump should still be moving in the direction going.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Nice! Graphics are pleasing to eyes. :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hy guys
 thanks to try

about Graphic
-->we give to Caesar what is of Caesar... I use the images of thread linked.

-->About movement it's mine developed following the above linked thread... in fact the title of this thread is a simple engine for .....

@bplus
about jumping....
if jumping must be directional I'll implement 3 cases jumping going to left, jumping going to right and jumping only high
but to get a good action I'll need to remove INKEY$ and use _KEYHIT and _KEYDOWN to execute the three different
commands  W+A,  W+D or only W. (so for  now there is only W command in execution)
And if you find more suitable to continue the action in execution before to jump... it is possible with no efforts, but this feature is implemented in some games while no in others, moreover I have seen game in which the character starts with an action and in air he can change this (for example it starts moving to left and while jumping it can stop in air or turn to right and vice versa until character is jumping)

Thanks for feedbacks
I'll try to go on quickly with your requests
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Et voilĂ !
1. for now character can be moved by AWSD and cursors,
2. jumping moving in the original direction
3. it continues to move in original direction after jump

Code: QB64: [Select]
  1. '2020 03 18  posting this demo at  forum QB64
  2. ' I have had some suggestions among these
  3. ' 1---jumping directional with the starting movement
  4. ' 2---continue the action of starting movement made before to jump
  5.  
  6. '--------------
  7. ' 2019 03 02
  8. ' TempodiBasic modification to demonstrate
  9. ' an one loop game engine for
  10. ' action in a scrolling world
  11. ' scrolling to left  and to right for walking
  12. ' scrolling up to jump
  13.  
  14. 'original idea from:
  15. 'Prithak Adhikari
  16. 'Saugat Adhikari
  17. '---------------------------
  18. 'A Project From
  19. '2/16/2019
  20. 'To
  21. '-
  22. '---------------------------
  23. 'This project was created for
  24. 'us to collaborate with a game
  25. 'that we dreamt to made for
  26. 'some time!
  27.  
  28. ' variable and constants declarations
  29. CONST HalfWImage = 50, HImage = 100, MaxJump = 100, Cstep = 5
  30. DIM idle&(15) 'array for idle images
  31. DIM walk&(15) ' array for walk image
  32. DIM jump&(15) 'array for jump image
  33. DIM bg AS LONG ' background handle
  34. DIM minJump AS INTEGER, i AS INTEGER
  35. DIM a AS INTEGER, k AS INTEGER, OldType$
  36.  
  37. SCREEN _NEWIMAGE(800, 600, 32) ' screen initialization
  38. _TITLE "Horizontal and Vertical scrolling"
  39. ' variables initialization
  40. minJump = INT(MaxJump / 15) 'vertical step unit = MaxJump DIV numframeSequence
  41. x = 0 'starter X of character
  42. y = 500 'starter Y of character
  43. ic = 1 'starter direction
  44. cx = 0 ' starter X correction to avoid flip effect changing direction of movement
  45. i = 0 'index for arrays
  46. Type$ = "idle" ' default command of character
  47. a = 0 ' index of image to display
  48. bgx = 0 'left background
  49. bgy = 0 'up background
  50. bgx2 = 800 'right backgorund
  51. bgy2 = 600 ' bottom background
  52.  
  53. ' images initialization
  54. bg = _LOADIMAGE("rpg/tileset/bg/bg.png") ' background file
  55. IF bg > 0 THEN PRINT "Error loading bg.pgn" 'it test if file is loaded
  56. FOR i = 1 TO 15 ' this FOR loads images of idle of character
  57.     idle&(i) = _LOADIMAGE("rpg/char/idle (" + LTRIM$(STR$(i)) + ").png")
  58.     IF idle&(i) > 0 THEN PRINT "Error loading  idle(" + LTRIM$(STR$(i)) + ").png"
  59. FOR i = 1 TO 15 'this FOR loads images of walk of character
  60.     walk&(i) = _LOADIMAGE("rpg/char/walk (" + LTRIM$(STR$(i)) + ").png")
  61.     IF walk&(i) > 0 THEN PRINT "Error loading  walk(" + LTRIM$(STR$(i)) + ").png"
  62. FOR i = 1 TO 15 ' this FOR loads images of jump of character
  63.     jump&(i) = _LOADIMAGE("rpg/char/jump (" + LTRIM$(STR$(i)) + ").png")
  64.     IF jump&(i) > 0 THEN PRINT "Error loading  jump(" + LTRIM$(STR$(i)) + ").png"
  65.  
  66. ' main loop
  67.  
  68.     k = _KEYHIT 'this is input manager
  69.  
  70.     ' this is command manager
  71.     IF NOT k = 0 THEN
  72.  
  73.         IF (k = 65 OR k = 97 OR k = 19200) AND Type$ <> "lwalk" THEN ' if user press A or a and char is not leftwalk
  74.             Type$ = "lwalk" ' command for  engine executor
  75.             IF ic = 1 THEN cx = -HalfWImage
  76.             ic = -1
  77.             a = 0
  78.         END IF
  79.         IF (k = 68 OR k = 100 OR k = 19712) AND Type$ <> "rwalk" THEN ' if user press D or d and char is not rightwalk
  80.             Type$ = "rwalk"
  81.             IF ic = -1 THEN cx = 0
  82.             ic = 1
  83.             a = 0
  84.         END IF
  85.         IF (k = 87 OR k = 119 OR k = 18432) AND Type$ <> "jump" THEN ' if user press W or w and char is not jumping
  86.             OldType$ = Type$
  87.             Type$ = "jump": a = 0
  88.         END IF
  89.         IF (k = 83 OR k = 115 OR k = 20480) AND Type$ <> " idle" THEN ' if user press S or s and char is not idle
  90.             Type$ = "idle": a = 0
  91.         END IF
  92.         IF k = 27 THEN END ' hotkey to quit
  93.     END IF
  94.  
  95.     ' this is command executor
  96.     IF a < 15 THEN a = a + 1 ELSE a = 1 'this calculates frame to use
  97.     IF Type$ = "jump" THEN
  98.         IF a < 7 THEN ' this calculates movement towards sky
  99.             dy = dy + minJump
  100.             IF dy > MaxJump THEN dy = MaxJump
  101.         ELSE ' this calculates movement towards hearth
  102.             dy = dy - minJump
  103.             IF dy < 0 THEN dy = 0
  104.             IF a = 15 THEN Type$ = OldType$
  105.         END IF
  106.         x = x + Cstep * -ic ' this lets movement of character
  107.     END IF
  108.     '  -ic because background goes in direction opposite to walking
  109.     IF Type$ = "lwalk" OR Type$ = "rwalk" THEN x = x + Cstep * -ic
  110.     IF x > 795 THEN x = 5 ' x cycle in the range of visible 5-795
  111.     IF x < 5 THEN x = 795
  112.  
  113.     ' output screen manager
  114.  
  115.     ' this create level background
  116.     ' this creates a background with 3 panel
  117.     '    |left|central|right|
  118.     'in this manner you can cover space left to left or right from scrolling central panel
  119.     ' at time 0 where x = 0 bgx =0 bgx2 = 800
  120.     _PUTIMAGE (bgx + x, bgy + dy)-(bgx2 + x, bgy2 + dy), bg ' central panel
  121.     _PUTIMAGE (-bgx2 + x, bgy + dy)-(bgx + x, bgy2 + dy), bg ' left panel
  122.     _PUTIMAGE (bgx2 + x, bgy + dy)-((2 * bgx2) + x, bgy2 + dy), bg ' right panel
  123.  
  124.  
  125.     ' manager of image set by command
  126.     IF Type$ = "idle" THEN
  127.         ' HalfWImage let us to use x position as center of the image
  128.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), idle&(a)
  129.     ELSEIF Type$ = "lwalk" OR Type$ = "rwalk" THEN
  130.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), walk&(a)
  131.     ELSEIF Type$ = "jump" THEN
  132.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), jump&(a)
  133.     END IF
  134.  
  135.     ' frame rate
  136.     _LIMIT 30
  137.     _DISPLAY
  138.  

the next step will be to jump changing direction while is on air  OR perform 3 kind of jumps using 3 different combokeys.
Let me know what do you think is better or do you prefer.
Thanks to watch.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
That was quick! I imagine if you are turning when you start jump then you can continue turning in air but can't start turning in mid air unless you want really cool game effect! ;)

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi guys
here an update following the suggestion of Bplus
now character can jump and turn in air to left or to right and after landing he continues to walk in the last direction choosen

Code: QB64: [Select]
  1. ' 2020 03 19
  2. '  Update now character can turn himself in air and when he lands he walks in the last direction choosen
  3. ' to be more visible this last feature I have doubled the space that he jumps adjusting the variables and adding a flag
  4. ' and more control on setting variables
  5.  
  6. ' 2020 03 19
  7. ' a little bug, you you turn character while he is jumping the bottom Y
  8. ' of the character increases letting walk in air him :-)
  9.  
  10.  
  11. '2020 03 18  posting this demo at  forum QB64
  12. ' I have had some suggestions among these
  13. ' 1---jumping directional with the starting movement
  14. ' 2---continue the action of starting movement made before to jump
  15.  
  16. '--------------
  17. ' 2019 03 02
  18. ' TempodiBasic modification to demonstrate
  19. ' an one loop game engine for
  20. ' action in a scrolling world
  21. ' scrolling to left  and to right for walking
  22. ' scrolling up to jump
  23.  
  24. 'original idea from:
  25. 'Prithak Adhikari
  26. 'Saugat Adhikari
  27. '---------------------------
  28. 'A Project From
  29. '2/16/2019
  30. 'To
  31. '-
  32. '---------------------------
  33. 'This project was created for
  34. 'us to collaborate with a game
  35. 'that we dreamt to made for
  36. 'some time!
  37.  
  38. ' variable and constants declarations
  39. CONST HalfWImage = 50, HImage = 100, MaxJump = 200, Cstep = 5
  40. DIM idle&(15) 'array for idle images
  41. DIM walk&(15) ' array for walk image
  42. DIM jump&(15) 'array for jump image
  43. DIM bg AS LONG ' background handle
  44. DIM minJump AS INTEGER, i AS INTEGER
  45. DIM a AS SINGLE, k AS INTEGER, OldType$, jumping AS INTEGER
  46.  
  47. SCREEN _NEWIMAGE(800, 600, 32) ' screen initialization
  48. _TITLE "Horizontal and Vertical scrolling"
  49. ' variables initialization
  50. minJump = INT(MaxJump / 15) 'vertical step unit = MaxJump DIV numframeSequence
  51. x = 0 'starter X of character
  52. y = 500 'starter Y of character
  53. ic = 1 'starter direction
  54. cx = 0 ' starter X correction to avoid flip effect changing direction of movement
  55. i = 0 'index for arrays
  56. TYPE$ = "idle" ' default command of character
  57. a = 0.5 ' index of image to display
  58. jumping = 0 ' no jump at start
  59. bgx = 0 'left background
  60. bgy = 0 'up background
  61. bgx2 = 800 'right backgorund
  62. bgy2 = 600 ' bottom background
  63.  
  64. ' images initialization
  65. bg = _LOADIMAGE("rpg/tileset/bg/bg.png") ' background file
  66. IF bg > 0 THEN PRINT "Error loading bg.pgn" 'it test if file is loaded
  67. FOR i = 1 TO 15 ' this FOR loads images of idle of character
  68.     idle&(i) = _LOADIMAGE("rpg/char/idle (" + LTRIM$(STR$(i)) + ").png")
  69.     IF idle&(i) > 0 THEN PRINT "Error loading  idle(" + LTRIM$(STR$(i)) + ").png"
  70. FOR i = 1 TO 15 'this FOR loads images of walk of character
  71.     walk&(i) = _LOADIMAGE("rpg/char/walk (" + LTRIM$(STR$(i)) + ").png")
  72.     IF walk&(i) > 0 THEN PRINT "Error loading  walk(" + LTRIM$(STR$(i)) + ").png"
  73. FOR i = 1 TO 15 ' this FOR loads images of jump of character
  74.     jump&(i) = _LOADIMAGE("rpg/char/jump (" + LTRIM$(STR$(i)) + ").png")
  75.     IF jump&(i) > 0 THEN PRINT "Error loading  jump(" + LTRIM$(STR$(i)) + ").png"
  76.  
  77. ' main loop
  78.  
  79.     k = _KEYHIT 'this is input manager
  80.  
  81.     ' this is command manager
  82.     IF NOT k = 0 THEN
  83.  
  84.         IF (k = 65 OR k = 97 OR k = 19200) AND TYPE$ <> "lwalk" THEN ' if user press A or a and char is not leftwalk
  85.             TYPE$ = "lwalk" ' command for  engine executor
  86.             IF ic = 1 THEN cx = -HalfWImage
  87.             ic = -1
  88.             IF NOT jumping THEN a = 0.5
  89.         END IF
  90.         IF (k = 68 OR k = 100 OR k = 19712) AND TYPE$ <> "rwalk" THEN ' if user press D or d and char is not rightwalk
  91.             TYPE$ = "rwalk"
  92.             IF ic = -1 THEN cx = 0
  93.             ic = 1
  94.             IF NOT jumping THEN a = 0.5
  95.         END IF
  96.         IF (k = 87 OR k = 119 OR k = 18432) AND TYPE$ <> "jump" THEN ' if user press W or w and char is not jumping
  97.             OldType$ = TYPE$
  98.             TYPE$ = "jump": a = 0.5
  99.         END IF
  100.         IF (k = 83 OR k = 115 OR k = 20480) AND TYPE$ <> " idle" THEN ' if user press S or s and char is not idle
  101.             TYPE$ = "idle": a = 0.5
  102.         END IF
  103.         IF k = 27 THEN END ' hotkey to quit
  104.     END IF
  105.  
  106.     ' this is command executor
  107.     IF a < 15 THEN a = a + .5 ELSE a = .5 'this calculates frame to use
  108.     IF TYPE$ = "jump" AND jumping = 0 THEN jumping = -1
  109.     IF jumping THEN
  110.         IF a < 7 THEN ' this calculates movement towards sky
  111.             dy = dy + minJump
  112.             IF dy > MaxJump THEN dy = MaxJump
  113.         ELSE ' this calculates movement towards hearth
  114.             dy = dy - minJump
  115.             IF dy < 0 THEN dy = 0: jumping = 0
  116.             IF a = 15 THEN TYPE$ = OldType$: jumping = 0
  117.         END IF
  118.         x = x + Cstep * -ic ' this lets movement of character
  119.     END IF
  120.     '  -ic because background goes in direction opposite to walking
  121.     IF TYPE$ = "lwalk" OR TYPE$ = "rwalk" THEN x = x + Cstep * -ic
  122.     IF jumping = -1 THEN TYPE$ = "jump"
  123.     IF x > 795 THEN x = 5 ' x cycle in the range of visible 5-795
  124.     IF x < 5 THEN x = 795
  125.  
  126.     ' output screen manager
  127.  
  128.     ' this create level background
  129.     ' this creates a background with 3 panel
  130.     '    |left|central|right|
  131.     'in this manner you can cover space left to left or right from scrolling central panel
  132.     ' at time 0 where x = 0 bgx =0 bgx2 = 800
  133.     _PUTIMAGE (bgx + x, bgy + dy)-(bgx2 + x, bgy2 + dy), bg ' central panel
  134.     _PUTIMAGE (-bgx2 + x, bgy + dy)-(bgx + x, bgy2 + dy), bg ' left panel
  135.     _PUTIMAGE (bgx2 + x, bgy + dy)-((2 * bgx2) + x, bgy2 + dy), bg ' right panel
  136.  
  137.  
  138.     ' manager of image set by command
  139.  
  140.     b = _CEIL(a)
  141.     IF TYPE$ = "idle" THEN
  142.         ' HalfWImage let us to use x position as center of the image
  143.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), idle&(b)
  144.     ELSEIF TYPE$ = "lwalk" OR TYPE$ = "rwalk" THEN
  145.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), walk&(b)
  146.     ELSEIF TYPE$ = "jump" THEN
  147.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), jump&(b)
  148.     END IF
  149.  
  150.  
  151.     ' frame rate
  152.     _LIMIT 30
  153.     _DISPLAY
  154.  
Thanks to try

PS about graphic of the game I have found the origin... here https://www.gameart2d.com/freebies.html# you'll find character set and tileset.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Hi TempodiBasic, I think you nailed it!