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

0 Members and 1 Guest are viewing this topic.

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
How to make a player jump in ONE LOOP!
« on: February 16, 2019, 08:41:15 pm »
Here's the code I built up for a project that my teacher gave me and my friend (He is not in this forum yet, cuz he doesn't have internet connection right now, but he will be soon!) to make a game! And I need to optimize it in order for it to run on his Core 2 Duo Computer (Like 100% same as mine lol). So, I needed to do this in ONE LOOP! So, here's my code for moving the character(Make sure to download the zip file!)
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.     CLS
  34.     k$ = UCASE$(INKEY$)
  35.  
  36.     _PUTIMAGE (bgx, bgy)-(bgx2, bgy2), bg
  37.  
  38.     IF k$ = "A" THEN type$ = "lwalk"
  39.     IF k$ = "D" THEN type$ = "rwalk"
  40.     IF k$ = "W" THEN type$ = "jump"
  41.     IF k$ <> "W" THEN
  42.         IF LEN(k$) = 0 THEN type$ = "idle"
  43.     END IF
  44.     IF type$ = "idle" THEN
  45.         a = a + 1
  46.         IF a > 15 THEN a = 1
  47.         IF ic = 1 THEN
  48.             _PUTIMAGE (x, y)-(x + 100, y + 100), idle&(a)
  49.         ELSEIF ic = 2 THEN
  50.             _PUTIMAGE (x + 100, y)-(x, y + 100), idle&(a)
  51.         END IF
  52.     ELSEIF type$ = "lwalk" THEN
  53.         ic = 2
  54.         b = b + 1
  55.         IF b > 14 THEN b = 1: type$ = "idle"
  56.         _PUTIMAGE (x + 100, y)-(x, y + 100), walk&(b)
  57.         x = x - 5
  58.     ELSEIF type$ = "rwalk" THEN
  59.         ic = 1
  60.         c = c + 1
  61.         IF c > 14 THEN c = 1: type$ = "idle"
  62.         _PUTIMAGE (x, y)-(x + 100, y + 100), walk&(c)
  63.         x = x + 5
  64.     END IF
  65.     LOCATE 1, 1: PRINT x; y; type$
  66.     _LIMIT 30
  67.     _DISPLAY
  68.  

IF the player presses "W", it jumps!
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #1 on: February 16, 2019, 10:53:30 pm »
Hi Prithak,

When I press w, it just blinks (a and d work).

Do you have a question? If not, I have one: How can you show a jump in only one loop? Wouldn't the character just end up down in new spot?  Opps, that's 2 questions ;)
« Last Edit: February 16, 2019, 10:56:54 pm by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #2 on: February 17, 2019, 03:33:35 am »
Hi Prithak,

Nice graphics. Is that your own job? About the jump. The maximum jump height is frame 7, as I found out. So, for the animation to be smooth, you have to take the current time when you insert the first slide, that is, if the jump takes one second, you have to insert the first 7 frames within half a second. You need to watch this, for example, using TIMER or ON TIMER. Just with inserting the next image you will always find out how much the current timer is, or set a new one. This way, you will not affect the main program loop. At the same time, when inserting pictures 1 through 7, you reduce the size of the Y axis in order for the figure to go up, and after half of the jump it increases again in the same step to the original value. If the height of the jump is 50 pixels, then is one shift in the Y axis for jump 50/7. For better effect, you can add or remove a value in the X axis, depending on whether the move key is pressed, but unfortunately, if you want to combine pressed keys, forget to the INKEY$ and use _KEYHIT.
« Last Edit: February 17, 2019, 03:35:28 am by Petr »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #3 on: February 17, 2019, 01:40:38 pm »
Hi Prithak
an expert of game engine seems to be UnseenMachine that together to other clever coders seem to be absent in these times on this forum
sob so I gain by this sentence the angryness of all the  clever coders that now are active on this forum... :-(
sorry I don't want to hit all you clever coders

Well, coming back to your question...
IMHO
a jump sequence is a mini film like the walking, but in your way to create this sequence you don't need a continuosly pressing the key W to jump while you want a continuosly pressing key for walking.... it is ok because
1. it is your game
2. it is one of the many ways to make a walking

so you have already the response....
I have coded it like my mod of your code just if you cannot figure how to do it... but it is the same manner that you have used for walking... as Petr has suggested you have to split the space of jumping in half of sequence imagines (I see they are 15 so we can use 7).... and mantain the command "jump" in your brain of engine type$ until the jump sequence is finished or the user press another key...

2 feedback:
1. passing from left idle to righ idle and vice-versa  you have coded an error because the character shifts to left or to right of 5 units...
2. you have loaded only rightward sequence of images for jump, so if your character look leftward then it jumps to righward
fine but wrong!

I hope this is useful for you

PS: just a curiosity
what do this?
Quote
    IF k$ <> "W" THEN
        IF LEN(k$) = 0 THEN type$ = "idle"
    END IF

More suggestions

you can use one counter variable instead of different variables if the number of images of sequence is the same for walk to left, walk to right, jump, die, fly, fire etc etc etc

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 #4 on: February 18, 2019, 07:30:17 am »
Hi Prithak,

Nice graphics. Is that your own job? About the jump. The maximum jump height is frame 7, as I found out. So, for the animation to be smooth, you have to take the current time when you insert the first slide, that is, if the jump takes one second, you have to insert the first 7 frames within half a second. You need to watch this, for example, using TIMER or ON TIMER. Just with inserting the next image you will always find out how much the current timer is, or set a new one. This way, you will not affect the main program loop. At the same time, when inserting pictures 1 through 7, you reduce the size of the Y axis in order for the figure to go up, and after half of the jump it increases again in the same step to the original value. If the height of the jump is 50 pixels, then is one shift in the Y axis for jump 50/7. For better effect, you can add or remove a value in the X axis, depending on whether the move key is pressed, but unfortunately, if you want to combine pressed keys, forget to the INKEY$ and use _KEYHIT.
Thanks, the background isn't my work but the character is. BTW, I am a part time animator! Thanks for the help but I already seem to have figured it out, but it still has some bugs that I know how to fix :D
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #5 on: February 18, 2019, 10:32:20 am »
OK I play with TempodiBasic's mod (nice start!)

Now when you press D the character walks right forever and if W is pressed it will jump.
Now to stop and idle the character press S, press W while idling and it will jump.
Now when you press A the characters walks left forever and if you press W it will jump while going left.

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. DIM maxJump AS INTEGER, minJump AS INTEGER
  18. maxJump = 50
  19. minJump = INT(maxJump / 7)
  20. bg = _LOADIMAGE("rpg/tileset/bg/bg.png")
  21. y = 500
  22. ic = 1 '<<<<<<<<<<<<<<<<<< direction facing 1 for right 2 for left?
  23. FOR i = 1 TO 15
  24.     idle&(i) = _LOADIMAGE("rpg/char/idle (" + LTRIM$(STR$(i)) + ").png")
  25. FOR i = 1 TO 15
  26.     walk&(i) = _LOADIMAGE("rpg/char/walk (" + LTRIM$(STR$(i)) + ").png")
  27. FOR i = 1 TO 15
  28.     jump&(i) = _LOADIMAGE("rpg/char/jump (" + LTRIM$(STR$(i)) + ").png")
  29. i = 0
  30. type$ = "idle"
  31. bgx = 0
  32. bgy = 0
  33. bgx2 = 800
  34. bgy2 = 600
  35.     CLS
  36.     k$ = UCASE$(INKEY$)
  37.  
  38.     _PUTIMAGE (bgx, bgy)-(bgx2, bgy2), bg
  39.  
  40.     ' IF k$ = "" AND type$ = "jump" THEN k$ = "W"
  41.     IF k$ = "S" THEN type$ = "idle"
  42.     IF k$ = "A" THEN type$ = "lwalk"
  43.     IF k$ = "D" THEN type$ = "rwalk"
  44.     IF k$ = "W" THEN jump = 1 '<<<<<<<<< change
  45.     'IF k$ <> "W" THEN
  46.     '    IF LEN(k$) = 0 THEN type$ = "idle"
  47.     'END IF
  48.     IF type$ = "idle" THEN
  49.         a = a + 1
  50.         IF a > 15 THEN a = 1
  51.         IF jump THEN
  52.             jump = jump + 1
  53.             IF jump = 15 THEN jump = 0
  54.             IF jump < 7 THEN
  55.                 dy = dy + minJump
  56.                 IF dy > maxJump THEN dy = maxJump
  57.             ELSE
  58.                 dy = dy - minJump
  59.                 IF dy < 0 THEN dy = 0
  60.             END IF
  61.             IF ic = 1 THEN
  62.                 _PUTIMAGE (x, y - dy)-(x + 100, y + 100 - dy), idle&(a)
  63.             ELSEIF ic = 2 THEN
  64.                 _PUTIMAGE (x + 100, y - dy)-(x, y + 100 - dy), idle&(a)
  65.             END IF
  66.         ELSE
  67.             IF ic = 1 THEN
  68.                 _PUTIMAGE (x, y)-(x + 100, y + 100), idle&(a)
  69.             ELSEIF ic = 2 THEN
  70.                 _PUTIMAGE (x + 100, y)-(x, y + 100), idle&(a)
  71.             END IF
  72.         END IF
  73.     ELSEIF type$ = "lwalk" THEN
  74.         ic = 2
  75.         b = b + 1
  76.         IF b > 14 THEN b = 1 ': type$ = "idle"
  77.         IF jump THEN
  78.             jump = jump + 1
  79.             IF jump = 15 THEN jump = 0
  80.             IF jump < 7 THEN
  81.                 dy = dy + minJump
  82.                 IF dy > maxJump THEN dy = maxJump
  83.             ELSE
  84.                 dy = dy - minJump
  85.                 IF dy < 0 THEN dy = 0
  86.             END IF
  87.             _PUTIMAGE (x + 100, y - dy)-(x, y + 100 - dy), walk&(b)
  88.         ELSE
  89.             _PUTIMAGE (x + 100, y)-(x, y + 100), walk&(b)
  90.         END IF
  91.         x = x - 5
  92.     ELSEIF type$ = "rwalk" THEN
  93.         ic = 1
  94.         c = c + 1
  95.         IF c > 14 THEN c = 1 ': type$ = "idle"
  96.         IF jump THEN
  97.             jump = jump + 1
  98.             IF jump = 15 THEN jump = 0
  99.             IF jump < 7 THEN
  100.                 dy = dy + minJump
  101.                 IF dy > maxJump THEN dy = maxJump
  102.             ELSE
  103.                 dy = dy - minJump
  104.                 IF dy < 0 THEN dy = 0
  105.             END IF
  106.             _PUTIMAGE (x, y - dy)-(x + 100, y + 100 - dy), walk&(c)
  107.         ELSE
  108.             _PUTIMAGE (x, y)-(x + 100, y + 100), walk&(c)
  109.         END IF
  110.         x = x + 5
  111.         'ELSEIF type$ = "jump" THEN
  112.         '    d = d + 1
  113.         '    IF d > 14 THEN d = 1: type$ = "idle"
  114.         '    IF d < 7 THEN
  115.         '        dy = dy + minJump
  116.         '        IF dy > maxJump THEN dy = maxJump
  117.         '    ELSE
  118.         '        dy = dy - minJump
  119.         '        IF dy < 0 THEN dy = 0
  120.         '    END IF
  121.         '    _PUTIMAGE (x, y - dy)-(x + 100, y + 100 - dy), jump&(d)
  122.     END IF
  123.     LOCATE 1, 1: PRINT x; y; type$
  124.     _LIMIT 30
  125.     _DISPLAY
  126.  
  127.  
« Last Edit: February 18, 2019, 10:36:27 am by bplus »

Offline TempodiBasic

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

about feedbacks

1 here I attach a version srhinked with one linear counter for image index and  looking just a little more into your code I code also for jumping leftward added to original jumping rightward

2. fixed the movement only changing the direction of looking of the character, now character moves toward if it looks already in that direction, changing direction no initial movement is made

3. when you change direction of looking of the character also if it x position changes only by 5 units it shifts more to left/right
But this is an image error.... if you see the sprites of the characters they have a large amount of white space on the right... except for the die routine.... so if you cut that white amount of space on the right making the same x dimension of each sprite of moving to left or right and to jump you can fix this bug
« Last Edit: February 18, 2019, 03:27:11 pm by TempodiBasic »
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 #7 on: February 18, 2019, 03:23:27 pm »
Hi Bplus
great mod of game engine!
this is one of the other ways to move a character that Prithak hasn't chosen but that I prefer if the game was mine.

« Last Edit: February 18, 2019, 03:26:50 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #8 on: February 18, 2019, 08:35:05 pm »
That's a lovely assignment, very clever teacher!

Here is my solution to the jump - I've changed "wasd" keys to arrow keys for my own convenience.

Code: QB64: [Select]
  1. ' & aka lil vince
  2. 'Prithak Adhikari
  3. 'Saugat Adhikari
  4. '---------------------------
  5. 'A Project From
  6. '2/16/2019
  7. 'To
  8. '-
  9. '---------------------------
  10. 'This project was created for
  11. 'us to collaborate with a game
  12. 'that we dreamt to made for
  13. 'some time!
  14. SCREEN _NEWIMAGE(800, 600, 32)
  15. DIM idle&(15)
  16. DIM walk&(15)
  17. DIM jump&(15)
  18.  
  19. bg = _LOADIMAGE("rpg/tileset/BG/BG.png")
  20. y = 500
  21. ic = 1
  22. FOR i = 1 TO 15
  23.     idle&(i) = _LOADIMAGE("rpg/char/Idle (" + LTRIM$(STR$(i)) + ").png")
  24. FOR i = 1 TO 15
  25.     walk&(i) = _LOADIMAGE("rpg/char/Walk (" + LTRIM$(STR$(i)) + ").png")
  26. FOR i = 1 TO 15
  27.     jump&(i) = _LOADIMAGE("rpg/char/Jump (" + LTRIM$(STR$(i)) + ").png")
  28. i = 0
  29. type$ = "Idle"
  30. bgx = 0
  31. bgy = 0
  32. bgx2 = 800
  33. bgy2 = 600
  34.  
  35. t = 0
  36. direction = 1
  37. base_y = y
  38. jump = 0
  39. jump_i = 0
  40.     CLS
  41.     k$ = UCASE$(INKEY$)
  42.     _PUTIMAGE (bgx, bgy)-(bgx2, bgy2), bg
  43.  
  44.         t = t + 1
  45.         if _keydown(19200) then
  46.                 x = x - 10
  47.                 if direction = 1 then direction = direction * -1
  48.         end if
  49.         if _keydown(19712) then
  50.                 x = x + 10
  51.                 if direction = -1 then direction = direction * -1
  52.         end if
  53.         if _keydown(18432) and jump = 0 then
  54.                 'start jump sequence
  55.                 jump_i = 5
  56.                 jump = 1
  57.         end if
  58.  
  59.  
  60.         if jump then
  61.                 if jump_i > 0 then
  62.                         jump_i = jump_i - 1
  63.                         y = y - 10
  64.                 else
  65.                         y = y + vy
  66.                         vy = vy + 0.5
  67.                 end if
  68.  
  69.                 if y >= base_y then
  70.                         vy = 0
  71.                         jump = 0
  72.                 end if
  73.  
  74.                 if direction = 1 then
  75.                         _PUTIMAGE (x, y-15)-(x + direction*100, y + 100), jump&(7)
  76.                 elseif direction = -1 then
  77.                         _PUTIMAGE (x + 50, y-15)-(x + 50 + direction*100, y + 100), jump&(7)
  78.                 end if
  79.         else
  80.                 if _keydown(19200) or _keydown(19712) then
  81.                         if direction = 1 then
  82.                                 _PUTIMAGE (x, y)-(x + direction*100, y + 100), walk&(t mod 15 + 1)
  83.                         elseif direction = -1 then
  84.                                 _PUTIMAGE (x + 50, y)-(x + 50 + direction*100, y + 100), walk&(t mod 15 + 1)
  85.                         end if
  86.                 else
  87.                         if direction = 1 then
  88.                                 _PUTIMAGE (x, y)-(x + direction*100, y + 100), idle&(t mod 15 + 1)
  89.                         elseif direction = -1 then
  90.                                 _PUTIMAGE (x + 50, y)-(x + 50 + direction*100, y + 100), idle&(t mod 15 + 1)
  91.                         end if
  92.                 end if
  93.         end if
  94.  
  95.     _LIMIT 30
  96.     _DISPLAY
  97.  

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 #9 on: February 18, 2019, 08:39:14 pm »
Hi Prithak

about feedbacks

1 here I attach a version srhinked with one linear counter for image index and  looking just a little more into your code I code also for jumping leftward added to original jumping rightward

2. fixed the movement only changing the direction of looking of the character, now character moves toward if it looks already in that direction, changing direction no initial movement is made

3. when you change direction of looking of the character also if it x position changes only by 5 units it shifts more to left/right
But this is an image error.... if you see the sprites of the characters they have a large amount of white space on the right... except for the die routine.... so if you cut that white amount of space on the right making the same x dimension of each sprite of moving to left or right and to jump you can fix this bug
This mod didn't work for me, it just goes downwards and not upwards. The previous one works perfectly fine! And I think I understand what's going on in that little snippet. I'll change the code and see what I can do to change my game.
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

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 #10 on: February 18, 2019, 08:40:38 pm »
OK I play with TempodiBasic's mod (nice start!)

Now when you press D the character walks right forever and if W is pressed it will jump.
Now to stop and idle the character press S, press W while idling and it will jump.
Now when you press A the characters walks left forever and if you press W it will jump while going left.

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. DIM maxJump AS INTEGER, minJump AS INTEGER
  18. maxJump = 50
  19. minJump = INT(maxJump / 7)
  20. bg = _LOADIMAGE("rpg/tileset/bg/bg.png")
  21. y = 500
  22. ic = 1 '<<<<<<<<<<<<<<<<<< direction facing 1 for right 2 for left?
  23. FOR i = 1 TO 15
  24.     idle&(i) = _LOADIMAGE("rpg/char/idle (" + LTRIM$(STR$(i)) + ").png")
  25. FOR i = 1 TO 15
  26.     walk&(i) = _LOADIMAGE("rpg/char/walk (" + LTRIM$(STR$(i)) + ").png")
  27. FOR i = 1 TO 15
  28.     jump&(i) = _LOADIMAGE("rpg/char/jump (" + LTRIM$(STR$(i)) + ").png")
  29. i = 0
  30. type$ = "idle"
  31. bgx = 0
  32. bgy = 0
  33. bgx2 = 800
  34. bgy2 = 600
  35.     CLS
  36.     k$ = UCASE$(INKEY$)
  37.  
  38.     _PUTIMAGE (bgx, bgy)-(bgx2, bgy2), bg
  39.  
  40.     ' IF k$ = "" AND type$ = "jump" THEN k$ = "W"
  41.     IF k$ = "S" THEN type$ = "idle"
  42.     IF k$ = "A" THEN type$ = "lwalk"
  43.     IF k$ = "D" THEN type$ = "rwalk"
  44.     IF k$ = "W" THEN jump = 1 '<<<<<<<<< change
  45.     'IF k$ <> "W" THEN
  46.     '    IF LEN(k$) = 0 THEN type$ = "idle"
  47.     'END IF
  48.     IF type$ = "idle" THEN
  49.         a = a + 1
  50.         IF a > 15 THEN a = 1
  51.         IF jump THEN
  52.             jump = jump + 1
  53.             IF jump = 15 THEN jump = 0
  54.             IF jump < 7 THEN
  55.                 dy = dy + minJump
  56.                 IF dy > maxJump THEN dy = maxJump
  57.             ELSE
  58.                 dy = dy - minJump
  59.                 IF dy < 0 THEN dy = 0
  60.             END IF
  61.             IF ic = 1 THEN
  62.                 _PUTIMAGE (x, y - dy)-(x + 100, y + 100 - dy), idle&(a)
  63.             ELSEIF ic = 2 THEN
  64.                 _PUTIMAGE (x + 100, y - dy)-(x, y + 100 - dy), idle&(a)
  65.             END IF
  66.         ELSE
  67.             IF ic = 1 THEN
  68.                 _PUTIMAGE (x, y)-(x + 100, y + 100), idle&(a)
  69.             ELSEIF ic = 2 THEN
  70.                 _PUTIMAGE (x + 100, y)-(x, y + 100), idle&(a)
  71.             END IF
  72.         END IF
  73.     ELSEIF type$ = "lwalk" THEN
  74.         ic = 2
  75.         b = b + 1
  76.         IF b > 14 THEN b = 1 ': type$ = "idle"
  77.         IF jump THEN
  78.             jump = jump + 1
  79.             IF jump = 15 THEN jump = 0
  80.             IF jump < 7 THEN
  81.                 dy = dy + minJump
  82.                 IF dy > maxJump THEN dy = maxJump
  83.             ELSE
  84.                 dy = dy - minJump
  85.                 IF dy < 0 THEN dy = 0
  86.             END IF
  87.             _PUTIMAGE (x + 100, y - dy)-(x, y + 100 - dy), walk&(b)
  88.         ELSE
  89.             _PUTIMAGE (x + 100, y)-(x, y + 100), walk&(b)
  90.         END IF
  91.         x = x - 5
  92.     ELSEIF type$ = "rwalk" THEN
  93.         ic = 1
  94.         c = c + 1
  95.         IF c > 14 THEN c = 1 ': type$ = "idle"
  96.         IF jump THEN
  97.             jump = jump + 1
  98.             IF jump = 15 THEN jump = 0
  99.             IF jump < 7 THEN
  100.                 dy = dy + minJump
  101.                 IF dy > maxJump THEN dy = maxJump
  102.             ELSE
  103.                 dy = dy - minJump
  104.                 IF dy < 0 THEN dy = 0
  105.             END IF
  106.             _PUTIMAGE (x, y - dy)-(x + 100, y + 100 - dy), walk&(c)
  107.         ELSE
  108.             _PUTIMAGE (x, y)-(x + 100, y + 100), walk&(c)
  109.         END IF
  110.         x = x + 5
  111.         'ELSEIF type$ = "jump" THEN
  112.         '    d = d + 1
  113.         '    IF d > 14 THEN d = 1: type$ = "idle"
  114.         '    IF d < 7 THEN
  115.         '        dy = dy + minJump
  116.         '        IF dy > maxJump THEN dy = maxJump
  117.         '    ELSE
  118.         '        dy = dy - minJump
  119.         '        IF dy < 0 THEN dy = 0
  120.         '    END IF
  121.         '    _PUTIMAGE (x, y - dy)-(x + 100, y + 100 - dy), jump&(d)
  122.     END IF
  123.     LOCATE 1, 1: PRINT x; y; type$
  124.     _LIMIT 30
  125.     _DISPLAY
  126.  
  127.  
THANK YOU!!! This was exactly what I needed! Now, let me change my code for instance!
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

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 #11 on: February 18, 2019, 08:42:51 pm »
That's a lovely assignment, very clever teacher!

Here is my solution to the jump - I've changed "wasd" keys to arrow keys for my own convenience.

Code: QB64: [Select]
  1. ' & aka lil vince
  2. 'Prithak Adhikari
  3. 'Saugat Adhikari
  4. '---------------------------
  5. 'A Project From
  6. '2/16/2019
  7. 'To
  8. '-
  9. '---------------------------
  10. 'This project was created for
  11. 'us to collaborate with a game
  12. 'that we dreamt to made for
  13. 'some time!
  14. SCREEN _NEWIMAGE(800, 600, 32)
  15. DIM idle&(15)
  16. DIM walk&(15)
  17. DIM jump&(15)
  18.  
  19. bg = _LOADIMAGE("rpg/tileset/BG/BG.png")
  20. y = 500
  21. ic = 1
  22. FOR i = 1 TO 15
  23.     idle&(i) = _LOADIMAGE("rpg/char/Idle (" + LTRIM$(STR$(i)) + ").png")
  24. FOR i = 1 TO 15
  25.     walk&(i) = _LOADIMAGE("rpg/char/Walk (" + LTRIM$(STR$(i)) + ").png")
  26. FOR i = 1 TO 15
  27.     jump&(i) = _LOADIMAGE("rpg/char/Jump (" + LTRIM$(STR$(i)) + ").png")
  28. i = 0
  29. type$ = "Idle"
  30. bgx = 0
  31. bgy = 0
  32. bgx2 = 800
  33. bgy2 = 600
  34.  
  35. t = 0
  36. direction = 1
  37. base_y = y
  38. jump = 0
  39. jump_i = 0
  40.     CLS
  41.     k$ = UCASE$(INKEY$)
  42.     _PUTIMAGE (bgx, bgy)-(bgx2, bgy2), bg
  43.  
  44.         t = t + 1
  45.         if _keydown(19200) then
  46.                 x = x - 10
  47.                 if direction = 1 then direction = direction * -1
  48.         end if
  49.         if _keydown(19712) then
  50.                 x = x + 10
  51.                 if direction = -1 then direction = direction * -1
  52.         end if
  53.         if _keydown(18432) and jump = 0 then
  54.                 'start jump sequence
  55.                 jump_i = 5
  56.                 jump = 1
  57.         end if
  58.  
  59.  
  60.         if jump then
  61.                 if jump_i > 0 then
  62.                         jump_i = jump_i - 1
  63.                         y = y - 10
  64.                 else
  65.                         y = y + vy
  66.                         vy = vy + 0.5
  67.                 end if
  68.  
  69.                 if y >= base_y then
  70.                         vy = 0
  71.                         jump = 0
  72.                 end if
  73.  
  74.                 if direction = 1 then
  75.                         _PUTIMAGE (x, y-15)-(x + direction*100, y + 100), jump&(7)
  76.                 elseif direction = -1 then
  77.                         _PUTIMAGE (x + 50, y-15)-(x + 50 + direction*100, y + 100), jump&(7)
  78.                 end if
  79.         else
  80.                 if _keydown(19200) or _keydown(19712) then
  81.                         if direction = 1 then
  82.                                 _PUTIMAGE (x, y)-(x + direction*100, y + 100), walk&(t mod 15 + 1)
  83.                         elseif direction = -1 then
  84.                                 _PUTIMAGE (x + 50, y)-(x + 50 + direction*100, y + 100), walk&(t mod 15 + 1)
  85.                         end if
  86.                 else
  87.                         if direction = 1 then
  88.                                 _PUTIMAGE (x, y)-(x + direction*100, y + 100), idle&(t mod 15 + 1)
  89.                         elseif direction = -1 then
  90.                                 _PUTIMAGE (x + 50, y)-(x + 50 + direction*100, y + 100), idle&(t mod 15 + 1)
  91.                         end if
  92.                 end if
  93.         end if
  94.  
  95.     _LIMIT 30
  96.     _DISPLAY
  97.  
OMG! THANK YOU SO MUCH! THIS WAS EXACTLY WHAT I HAD IN MY HEAD!!! Will you mind If I change the code a little bit and submit it to my teacher? :P
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #12 on: February 18, 2019, 09:00:33 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.  
« Last Edit: February 18, 2019, 09:33:24 pm by _vince »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #13 on: February 18, 2019, 09:28:59 pm »
Good one _vince, I don't like pressing those two keys at once but I sure do like that sidescroller method!
« Last Edit: February 18, 2019, 09:31:08 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to make a player jump in ONE LOOP!
« Reply #14 on: February 19, 2019, 04:06:19 am »
Hi _vince
very fine
1. the solution to use a switch variable for _putimage output
2. and the managment of 50 more white pixel in the sprites using a costant to shift those pixel so that image doesn't shift changing side of direction

and I agree that _KEYDOWN and _KEYHIT are more suitable for input toward INKEY$.

I'll go to see the other example that you have posted!
Programming isn't difficult, only it's  consuming time and coffee