Author Topic: How to make a player jump in ONE LOOP!  (Read 4887 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #15 on: February 19, 2019, 04:21:02 am »
@Prithak


Quote
  it just goes downwards and not upwards. 
I am hungry so also in this time I have eaten something...
this in the section jump after DeltaY calculation
Code: QB64: [Select]
  1.  IF DeltaY < 0 THEN DeltaY = 0 '<-- newline
I must be sure that it is positive or max 0
:-)
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #16 on: February 19, 2019, 04:25:54 am »
@ _vince

Hey but you have got a bigger boy ! :-)
Cool
and the horizontal scrolling by _PUTIMAGE is fine!

Thanks
I would have time to develop also vertical scrolling, but I need some free time  so we see after some days.
Thanks again to capture my imagination!
Programming isn't difficult, only it's  consuming time and coffee

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: How to make a player jump in ONE LOOP!
« Reply #17 on: February 20, 2019, 09:09:26 pm »
This is a true sidescroller variant where the character remains stationary while the background moves. You can add obstacles and items based on value of variable x to create your map.

You are welcome to use the code as you wish

Edit: Hope I didn't just do your homework, careful submitting any code you did not write - review the policy on that
Code: QB64: [Select]
  1. 'Prithak Adhikari
  2. 'Saugat Adhikari
  3. '---------------------------
  4. 'A Project From
  5. '2/16/2019
  6. 'To
  7. '-
  8. '---------------------------
  9. 'This project was created for
  10. 'us to collaborate with a game
  11. 'that we dreamt to made for
  12. 'some time!
  13. SCREEN _NEWIMAGE(800, 600, 32)
  14. DIM idle&(15)
  15. DIM walk&(15)
  16. DIM jump&(15)
  17.  
  18. bg = _LOADIMAGE("rpg/tileset/BG/BG.png")
  19. y = 500
  20. ic = 1
  21. FOR i = 1 TO 15
  22.     idle&(i) = _LOADIMAGE("rpg/char/Idle (" + LTRIM$(STR$(i)) + ").png")
  23. FOR i = 1 TO 15
  24.     walk&(i) = _LOADIMAGE("rpg/char/Walk (" + LTRIM$(STR$(i)) + ").png")
  25. FOR i = 1 TO 15
  26.     jump&(i) = _LOADIMAGE("rpg/char/Jump (" + LTRIM$(STR$(i)) + ").png")
  27. i = 0
  28. type$ = "Idle"
  29. bgx = 0
  30. bgy = 0
  31. bgx2 = 800
  32. bgy2 = 600
  33.  
  34. t = 0
  35. direction = 1
  36. base_y = y-100
  37. y = base_y
  38. jump = 0
  39. jump_i = 0
  40.     CLS
  41.     k$ = UCASE$(INKEY$)
  42.         if x>=0 then
  43.                 _PUTIMAGE (bgx-(x) mod 800, 0)-step(800,600), bg
  44.                 _PUTIMAGE (bgx-(x) mod 800+800, 0)-step(800,600), bg
  45.         else
  46.                 _PUTIMAGE (bgx+abs(x) mod 800, 0)-step(800,600), bg
  47.                 _PUTIMAGE (bgx+abs(x) mod 800-800, 0)-step(800,600), bg
  48.         end if
  49.  
  50.         t = t + 1
  51.         if _keydown(19200) then
  52.                 x = x - 10
  53.                 if direction = 1 then direction = direction * -1
  54.         end if
  55.         if _keydown(19712) then
  56.                 x = x + 10
  57.                 if direction = -1 then direction = direction * -1
  58.         end if
  59.         if _keydown(18432) and jump = 0 then
  60.                 'start jump sequence
  61.                 jump_i = 5
  62.                 jump = 1
  63.         end if
  64.  
  65.  
  66.         if jump then
  67.                 if jump_i > 0 then
  68.                         jump_i = jump_i - 1
  69.                         y = y - 20
  70.                 else
  71.                         y = y + vy
  72.                         vy = vy + 0.8
  73.                 end if
  74.  
  75.                 if y >= base_y then
  76.                         y = base_y
  77.                         vy = 0
  78.                         jump = 0
  79.                 end if
  80.  
  81.                 if direction = 1 then
  82.                         _PUTIMAGE (350, y-30)-(350 + direction*200, y + 200), jump&(7)
  83.                 elseif direction = -1 then
  84.                         _PUTIMAGE (350 + 100, y-15)-(350 + 100 + direction*200, y + 200), jump&(7)
  85.                 end if
  86.         else
  87.                 if _keydown(19200) or _keydown(19712) then
  88.                         if direction = 1 then
  89.                                 _PUTIMAGE (350, y)-(350 + direction*200, y + 200), walk&(t mod 15 + 1)
  90.                         elseif direction = -1 then
  91.                                 _PUTIMAGE (350 + 100, y)-(350 + 100 + direction*200, y + 200), walk&(t mod 15 + 1)
  92.                         end if
  93.                 else
  94.                         if direction = 1 then
  95.                                 _PUTIMAGE (350, y)-(350 + direction*200, y + 200), idle&(t mod 15 + 1)
  96.                         elseif direction = -1 then
  97.                                 _PUTIMAGE (350 + 100, y)-(350 + 100 + direction*200, y + 200), idle&(t mod 15 + 1)
  98.                         end if
  99.                 end if
  100.         end if
  101.  
  102.     _LIMIT 30
  103.     _DISPLAY
  104.  
  105.  
Thanks for making this _Vince xD. No, I am not like flat on CHR$(3) + CHR$(22) it, but referencing it and making something of my own out of it :D. (i hope you see what I did there xD).
- Prithak
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #18 on: February 24, 2019, 12:29:24 pm »
Hi guys
Hi Prithak

Thanks to Vince's demo that rose up my imagination after a battle with _PUTIMAGE that is very powerful but you must enter in the idea of the function here my version of walking suggested by Vince and horizontal scrolling and horizontal plus vertical scrolling

Code: QB64: [Select]
  1. 'Prithak Adhikari
  2. 'Saugat Adhikari
  3. '---------------------------
  4. 'A Project From
  5. '2/16/2019
  6. 'To
  7. '-
  8. '---------------------------
  9. 'This project was created for
  10. 'us to collaborate with a game
  11. 'that we dreamt to made for
  12. 'some time!
  13.  
  14. ' variable and constants declarations
  15. CONST HalfWImage = 50, HImage = 100, MaxJump = 50, Cstep = 5
  16. DIM idle&(15)
  17. DIM walk&(15)
  18. DIM jump&(15)
  19. DIM minJump AS INTEGER, i AS INTEGER
  20.  
  21. SCREEN _NEWIMAGE(800, 600, 32) ' screen initialization
  22. _TITLE "Continuosly walking"
  23. ' variables initialization
  24. minJump = INT(MaxJump / 7) 'vertical step unit = numframeSequence DIV 2
  25. x = 90 'starter X of character
  26. y = 500 'starter Y of character
  27. ic = 1 'starter direction
  28. i = 0
  29. type$ = "idle"
  30. bgx = 0
  31. bgy = 0
  32. bgx2 = 800
  33. bgy2 = 600
  34.  
  35. ' images initialization
  36. bg = _LOADIMAGE("rpg/tileset/bg/bg.png")
  37. IF bg > 0 THEN PRINT "Error loading bg.pgn"
  38. FOR i = 1 TO 15
  39.     idle&(i) = _LOADIMAGE("rpg/char/idle (" + LTRIM$(STR$(i)) + ").png")
  40.     IF idle&(i) > 0 THEN PRINT "Error loading  idle(" + LTRIM$(STR$(i)) + ").png"
  41. FOR i = 1 TO 15
  42.     walk&(i) = _LOADIMAGE("rpg/char/walk (" + LTRIM$(STR$(i)) + ").png")
  43.     IF walk&(i) > 0 THEN PRINT "Error loading  walk(" + LTRIM$(STR$(i)) + ").png"
  44. FOR i = 1 TO 15
  45.     jump&(i) = _LOADIMAGE("rpg/char/jump (" + LTRIM$(STR$(i)) + ").png")
  46.     IF jump&(i) > 0 THEN PRINT "Error loading  jump(" + LTRIM$(STR$(i)) + ").png"
  47.  
  48. ' main loop
  49.  
  50.     k$ = UCASE$(INKEY$) 'this is input manager
  51.  
  52.     ' this is command manager
  53.     IF NOT k$ = "" THEN
  54.         IF k$ = "A" THEN
  55.             type$ = "lwalk"
  56.             IF ic = 1 THEN x = x + HalfWImage * -ic
  57.             ic = -1
  58.         END IF
  59.         IF k$ = "D" THEN
  60.             type$ = "rwalk"
  61.             IF ic = -1 THEN x = x + HalfWImage * -ic
  62.             ic = 1
  63.         END IF
  64.         IF k$ = "W" THEN type$ = "jump"
  65.         IF k$ = "S" THEN type$ = "idle"
  66.         IF k$ = CHR$(27) THEN END ' hotkey to quit
  67.     END IF
  68.  
  69.     ' this is command executor
  70.     IF a < 15 THEN a = a + 1 ELSE a = 1 'this calculates frame to use
  71.     IF type$ = "jump" THEN
  72.         IF a < 7 THEN ' this calculates movement toward sky
  73.             dy = dy + minJump
  74.             IF dy > MaxJump THEN dy = MaxJump
  75.         ELSE
  76.             dy = dy - minJump
  77.             IF dy < 0 THEN dy = 0
  78.             IF a = 15 THEN type$ = "idle"
  79.         END IF
  80.     END IF
  81.     IF type$ = "lwalk" OR type$ = "rwalk" THEN x = x + Cstep * ic
  82.  
  83.  
  84.     _PUTIMAGE (bgx, bgy)-(bgx2, bgy2), bg ' this create level background
  85.  
  86.     ' manager of image set by command
  87.     IF type$ = "idle" THEN
  88.         ' HalfWImage let us to use x position as center of the image
  89.         _PUTIMAGE (x - HalfWImage * ic, y)-(x + HalfWImage * ic, y + HImage), idle&(a)
  90.     ELSEIF type$ = "lwalk" OR type$ = "rwalk" THEN
  91.         _PUTIMAGE (x - HalfWImage * ic, y)-(x + HalfWImage * ic, y + HImage), walk&(a)
  92.     ELSEIF type$ = "jump" THEN
  93.         _PUTIMAGE (x - HalfWImage * ic, y - dy)-(x + HalfWImage * ic, y + HImage - dy), jump&(a)
  94.     END IF
  95.  
  96.     ' debug manager
  97.     LOCATE 1, 1: PRINT x; y; type$
  98.  
  99.     ' output screen manager
  100.     _LIMIT 30
  101.     _DISPLAY
  102.  

running it you can  appreciate the difference of input method that is just a bit more delayed  than _KEYDOWN  :-)

Here Horizontal scrolling idea
Code: QB64: [Select]
  1. 'Prithak Adhikari
  2. 'Saugat Adhikari
  3. '---------------------------
  4. 'A Project From
  5. '2/16/2019
  6. 'To
  7. '-
  8. '---------------------------
  9. 'This project was created for
  10. 'us to collaborate with a game
  11. 'that we dreamt to made for
  12. 'some time!
  13.  
  14. ' variable and constants declarations
  15. CONST HalfWImage = 50, HImage = 100, MaxJump = 100, Cstep = 5
  16. DIM idle&(15)
  17. DIM walk&(15)
  18. DIM jump&(15)
  19. DIM minJump AS INTEGER, i AS INTEGER
  20.  
  21. SCREEN _NEWIMAGE(800, 600, 32) ' screen initialization
  22.  
  23. ' variables initialization
  24. minJump = INT(MaxJump / 15) 'vertical step unit = MaxJump DIV numframeSequence
  25. x = 0 'starter X of character
  26. y = 500 'starter Y of character
  27. ic = 1 'starter direction
  28. cx = 0 ' starter X correction to avoid flip effect changing direction of movement
  29. i = 0
  30. type$ = "idle"
  31. bgx = 0
  32. bgy = 0
  33. bgx2 = 800
  34. bgy2 = 600
  35.  
  36. ' images initialization
  37. bg = _LOADIMAGE("rpg/tileset/bg/bg.png")
  38. IF bg > 0 THEN PRINT "Error loading bg.pgn"
  39. FOR i = 1 TO 15
  40.     idle&(i) = _LOADIMAGE("rpg/char/idle (" + LTRIM$(STR$(i)) + ").png")
  41.     IF idle&(i) > 0 THEN PRINT "Error loading  idle(" + LTRIM$(STR$(i)) + ").png"
  42. FOR i = 1 TO 15
  43.     walk&(i) = _LOADIMAGE("rpg/char/walk (" + LTRIM$(STR$(i)) + ").png")
  44.     IF walk&(i) > 0 THEN PRINT "Error loading  walk(" + LTRIM$(STR$(i)) + ").png"
  45. FOR i = 1 TO 15
  46.     jump&(i) = _LOADIMAGE("rpg/char/jump (" + LTRIM$(STR$(i)) + ").png")
  47.     IF jump&(i) > 0 THEN PRINT "Error loading  jump(" + LTRIM$(STR$(i)) + ").png"
  48.  
  49. ' main loop
  50.  
  51.     k$ = UCASE$(INKEY$) 'this is input manager
  52.  
  53.     ' this is command manager
  54.     IF NOT k$ = "" THEN
  55.         a = 0
  56.         IF k$ = "A" THEN
  57.             type$ = "lwalk"
  58.             IF ic = 1 THEN cx = -HalfWImage
  59.             ic = -1
  60.         END IF
  61.         IF k$ = "D" THEN
  62.             type$ = "rwalk"
  63.             IF ic = -1 THEN cx = 0
  64.             ic = 1
  65.         END IF
  66.         IF k$ = "W" THEN type$ = "jump"
  67.         IF k$ = "S" THEN type$ = "idle"
  68.         IF k$ = CHR$(27) THEN END ' hotkey to quit
  69.     END IF
  70.  
  71.     ' this is command executor
  72.     IF a < 15 THEN a = a + 1 ELSE a = 1 'this calculates frame to use
  73.     IF type$ = "jump" THEN
  74.         IF a < 7 THEN ' this calculates movement toward sky
  75.             dy = dy + minJump
  76.             IF dy > MaxJump THEN dy = MaxJump
  77.         ELSE
  78.             dy = dy - minJump
  79.             IF dy < 0 THEN dy = 0
  80.             IF a = 15 THEN type$ = "idle"
  81.         END IF
  82.     END IF
  83.     '         -ic because background goes in direction opposite to walking
  84.     IF type$ = "lwalk" OR type$ = "rwalk" THEN x = x + Cstep * -ic
  85.     IF x > 795 THEN x = 5
  86.     IF x < 5 THEN x = 795
  87.  
  88.     ' this create level background
  89.     ' this creates a background with 3 panel
  90.     '  |left|central|right|
  91.     'in this manner you can cover space left to left or right from scrolling central panel
  92.     ' at time 0 where x = 0 bgx =0 bgx2 = 800
  93.     _PUTIMAGE (bgx + x, bgy)-(bgx2 + x, bgy2), bg ' central panel
  94.     _PUTIMAGE (-bgx2 + x, bgy)-(bgx + x, bgy2), bg ' left panel
  95.     _PUTIMAGE (bgx2 + x, bgy)-((2 * bgx2) + x, bgy2), bg ' right panel
  96.  
  97.  
  98.     ' manager of image set by command
  99.     IF type$ = "idle" THEN
  100.         ' HalfWImage let us to use x position as center of the image
  101.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), idle&(a)
  102.     ELSEIF type$ = "lwalk" OR type$ = "rwalk" THEN
  103.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), walk&(a)
  104.     ELSEIF type$ = "jump" THEN
  105.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y - dy)-((350 + cx) + HalfWImage * ic, y + HImage - dy), jump&(a)
  106.     END IF
  107.  
  108.     ' output screen manager
  109.     _LIMIT 30
  110.     _DISPLAY
  111.  

and here Horizontal and Vertical scrolling together
Code: QB64: [Select]
  1. 'Prithak Adhikari
  2. 'Saugat Adhikari
  3. '---------------------------
  4. 'A Project From
  5. '2/16/2019
  6. 'To
  7. '-
  8. '---------------------------
  9. 'This project was created for
  10. 'us to collaborate with a game
  11. 'that we dreamt to made for
  12. 'some time!
  13.  
  14. ' variable and constants declarations
  15. CONST HalfWImage = 50, HImage = 100, MaxJump = 100, Cstep = 5
  16. DIM idle&(15)
  17. DIM walk&(15)
  18. DIM jump&(15)
  19. DIM minJump AS INTEGER, i AS INTEGER
  20.  
  21. SCREEN _NEWIMAGE(800, 600, 32) ' screen initialization
  22. _TITLE "Vertical scrolling"
  23. ' variables initialization
  24. minJump = INT(MaxJump / 15) 'vertical step unit = MaxJump DIV numframeSequence
  25. x = 0 'starter X of character
  26. y = 500 'starter Y of character
  27. ic = 1 'starter direction
  28. cx = 0 ' starter X correction to avoid flip effect changing direction of movement
  29. i = 0
  30. type$ = "idle"
  31. bgx = 0
  32. bgy = 0
  33. bgx2 = 800
  34. bgy2 = 600
  35.  
  36. ' images initialization
  37. bg = _LOADIMAGE("rpg/tileset/bg/bg.png")
  38. IF bg > 0 THEN PRINT "Error loading bg.pgn"
  39. FOR i = 1 TO 15
  40.     idle&(i) = _LOADIMAGE("rpg/char/idle (" + LTRIM$(STR$(i)) + ").png")
  41.     IF idle&(i) > 0 THEN PRINT "Error loading  idle(" + LTRIM$(STR$(i)) + ").png"
  42. FOR i = 1 TO 15
  43.     walk&(i) = _LOADIMAGE("rpg/char/walk (" + LTRIM$(STR$(i)) + ").png")
  44.     IF walk&(i) > 0 THEN PRINT "Error loading  walk(" + LTRIM$(STR$(i)) + ").png"
  45. FOR i = 1 TO 15
  46.     jump&(i) = _LOADIMAGE("rpg/char/jump (" + LTRIM$(STR$(i)) + ").png")
  47.     IF jump&(i) > 0 THEN PRINT "Error loading  jump(" + LTRIM$(STR$(i)) + ").png"
  48.  
  49. ' main loop
  50.  
  51.     k$ = UCASE$(INKEY$) 'this is input manager
  52.  
  53.     ' this is command manager
  54.     IF NOT k$ = "" THEN
  55.         a = 0
  56.         IF k$ = "A" THEN
  57.             type$ = "lwalk"
  58.             IF ic = 1 THEN cx = -HalfWImage
  59.             ic = -1
  60.         END IF
  61.         IF k$ = "D" THEN
  62.             type$ = "rwalk"
  63.             IF ic = -1 THEN cx = 0
  64.             ic = 1
  65.         END IF
  66.         IF k$ = "W" THEN type$ = "jump"
  67.         IF k$ = "S" THEN type$ = "idle"
  68.         IF k$ = CHR$(27) THEN END ' hotkey to quit
  69.     END IF
  70.  
  71.     ' this is command executor
  72.     IF a < 15 THEN a = a + 1 ELSE a = 1 'this calculates frame to use
  73.     IF type$ = "jump" THEN
  74.         IF a < 7 THEN ' this calculates movement toward sky
  75.             dy = dy + minJump
  76.             IF dy > MaxJump THEN dy = MaxJump
  77.         ELSE
  78.             dy = dy - minJump
  79.             IF dy < 0 THEN dy = 0
  80.             IF a = 15 THEN type$ = "idle"
  81.         END IF
  82.     END IF
  83.     '         -ic because background goes in direction opposite to walking
  84.     IF type$ = "lwalk" OR type$ = "rwalk" THEN x = x + Cstep * -ic
  85.     IF x > 795 THEN x = 5
  86.     IF x < 5 THEN x = 795
  87.  
  88.     ' this create level background
  89.     ' this creates a background with 3 panel
  90.     '  |left|central|right|
  91.     'in this manner you can cover space left to left or right from scrolling central panel
  92.     ' at time 0 where x = 0 bgx =0 bgx2 = 800
  93.     _PUTIMAGE (bgx + x, bgy + dy)-(bgx2 + x, bgy2 + dy), bg ' central panel
  94.     _PUTIMAGE (-bgx2 + x, bgy + dy)-(bgx + x, bgy2 + dy), bg ' left panel
  95.     _PUTIMAGE (bgx2 + x, bgy + dy)-((2 * bgx2) + x, bgy2 + dy), bg ' right panel
  96.  
  97.  
  98.     ' manager of image set by command
  99.     IF type$ = "idle" THEN
  100.         ' HalfWImage let us to use x position as center of the image
  101.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), idle&(a)
  102.     ELSEIF type$ = "lwalk" OR type$ = "rwalk" THEN
  103.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), walk&(a)
  104.     ELSEIF type$ = "jump" THEN
  105.         _PUTIMAGE ((350 + cx) - HalfWImage * ic, y)-((350 + cx) + HalfWImage * ic, y + HImage), jump&(a)
  106.     END IF
  107.  
  108.     ' output screen manager
  109.     _LIMIT 30
  110.     _DISPLAY
  111.  

the 3 file.BAS are attached for those don't like to copy and paste into QB64IDE.

Thanks to read
Programming isn't difficult, only it's  consuming time and coffee