Author Topic: last thing to ask today, i promise...  (Read 3417 times)

0 Members and 1 Guest are viewing this topic.

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
last thing to ask today, i promise...
« on: February 16, 2020, 08:33:19 pm »
what do i need to do so that my character stops moving when it hits the corner of the screen?
i already set up some variables such as x speed, y speed, x max, y max, etc.

FellippeHeitor

  • Guest
Re: last thing to ask today, i promise...
« Reply #1 on: February 16, 2020, 08:35:14 pm »
Show the code you already have first, so people can help debug it.

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
Re: last thing to ask today, i promise...
« Reply #2 on: February 16, 2020, 08:36:18 pm »
Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3.  
  4. DATA 00,00,00,00,00,00,00,00,14,14,14,14,14,00,00,00,00,00,00,00,00
  5. DATA 00,00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,00,00,00
  6. DATA 00,00,00,00,00,00,14,14,14,14,00,14,14,14,14,00,00,00,00,00,00
  7. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,00,00,00,00
  8. DATA 00,00,00,00,00,00,14,14,14,14,14,14,00,00,00,00,00,00,14,14,00
  9. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,14,14,14,14
  10. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,14,14,00,00,14,14,14,14
  11. DATA 00,00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,14,14,00
  12. DATA 00,00,00,00,00,00,00,00,14,14,14,14,14,00,00,00,00,00,14,14,00
  13. DATA 00,00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,14,14,00
  14. DATA 00,00,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,00
  15. DATA 00,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,00,00
  16. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  17. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  18. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  19. DATA 14,14,14,14,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  20. DATA 14,14,14,14,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  21. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  22. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  23. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  24. DATA 00,00,00,00,00,14,14,14,14,14,00,14,14,14,14,14,00,00,00,00,00
  25. DATA 00,00,00,00,00,14,14,14,14,00,00,00,14,14,14,14,00,00,00,00,00
  26. DATA 00,00,00,00,14,14,14,14,14,00,00,00,14,14,14,14,00,00,00,00,00
  27. DATA 00,00,00,00,14,14,14,14,00,00,00,00,00,14,14,14,14,00,00,00,00
  28. DATA 00,00,00,14,14,14,14,14,00,00,00,00,00,14,14,14,14,00,00,00,00
  29. DATA 00,00,00,14,14,14,14,00,00,00,00,00,00,14,14,14,14,00,14,14,00
  30. DATA 00,00,14,14,14,14,14,00,00,00,00,00,00,14,14,14,14,14,14,14,00
  31. DATA 00,00,00,14,14,14,14,14,14,00,00,00,00,14,14,14,14,14,14,14,00
  32. DATA 00,00,00,00,14,14,14,14,14,00,00,00,14,14,14,14,14,14,00,00,00
  33. DATA 00,00,00,00,00,14,14,14,00,00,00,00,00,14,14,00,00,00,00,00,00
  34.  
  35. FOR y = 1 TO 30
  36.     FOR x = 0 TO 20
  37.         READ sprite%: PSET (x, y), sprite%
  38.     NEXT x
  39.  
  40. DIM player%(500)
  41.  
  42. GET (0, 1)-(20, 30), player%()
  43.  
  44. xp = 64
  45. yp = 70
  46. xb = 400
  47. yb = 300
  48. xmax = 639
  49. ymax = 349
  50. xspd = 0
  51. yspd = 0
  52.  
  53.  
  54.  
  55. tecla:
  56. PUT (xp, yp), player%()
  57. WAIT &H3DA, 8
  58. PUT (xp, yp), player%()
  59. te$ = INKEY$
  60. IF te$ = "d" THEN xp = xp + 2
  61. IF te$ = "a" THEN xp = xp - 2
  62. IF te$ = "w" THEN yp = yp - 2
  63. IF te$ = "s" THEN yp = yp + 2
  64. IF te$ = "q" THEN END
  65. GOTO tecla:
  66.  
« Last Edit: February 16, 2020, 08:44:37 pm by odin »

FellippeHeitor

  • Guest
Re: last thing to ask today, i promise...
« Reply #3 on: February 16, 2020, 08:47:33 pm »
Use IF to limit xp and yp to the screen boundaries:

Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3.  
  4. DATA 00,00,00,00,00,00,00,00,14,14,14,14,14,00,00,00,00,00,00,00,00
  5. DATA 00,00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,00,00,00
  6. DATA 00,00,00,00,00,00,14,14,14,14,00,14,14,14,14,00,00,00,00,00,00
  7. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,00,00,00,00
  8. DATA 00,00,00,00,00,00,14,14,14,14,14,14,00,00,00,00,00,00,14,14,00
  9. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,14,14,14,14
  10. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,14,14,00,00,14,14,14,14
  11. DATA 00,00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,14,14,00
  12. DATA 00,00,00,00,00,00,00,00,14,14,14,14,14,00,00,00,00,00,14,14,00
  13. DATA 00,00,00,00,00,00,00,14,14,14,14,14,14,14,00,00,00,00,14,14,00
  14. DATA 00,00,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,00
  15. DATA 00,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,00,00
  16. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  17. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  18. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  19. DATA 14,14,14,14,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  20. DATA 14,14,14,14,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  21. DATA 00,14,14,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  22. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  23. DATA 00,00,00,00,00,00,14,14,14,14,14,14,14,14,14,00,00,00,00,00,00
  24. DATA 00,00,00,00,00,14,14,14,14,14,00,14,14,14,14,14,00,00,00,00,00
  25. DATA 00,00,00,00,00,14,14,14,14,00,00,00,14,14,14,14,00,00,00,00,00
  26. DATA 00,00,00,00,14,14,14,14,14,00,00,00,14,14,14,14,00,00,00,00,00
  27. DATA 00,00,00,00,14,14,14,14,00,00,00,00,00,14,14,14,14,00,00,00,00
  28. DATA 00,00,00,14,14,14,14,14,00,00,00,00,00,14,14,14,14,00,00,00,00
  29. DATA 00,00,00,14,14,14,14,00,00,00,00,00,00,14,14,14,14,00,14,14,00
  30. DATA 00,00,14,14,14,14,14,00,00,00,00,00,00,14,14,14,14,14,14,14,00
  31. DATA 00,00,00,14,14,14,14,14,14,00,00,00,00,14,14,14,14,14,14,14,00
  32. DATA 00,00,00,00,14,14,14,14,14,00,00,00,14,14,14,14,14,14,00,00,00
  33. DATA 00,00,00,00,00,14,14,14,00,00,00,00,00,14,14,00,00,00,00,00,00
  34.  
  35. FOR y = 1 TO 30
  36.     FOR x = 0 TO 20
  37.         READ sprite%: PSET (x, y), sprite%
  38.     NEXT x
  39.  
  40. DIM player%(500)
  41.  
  42. GET (0, 1)-(20, 30), player%()
  43.  
  44. xp = 64
  45. yp = 70
  46. xb = 400
  47. yb = 300
  48. xmax = 639
  49. ymax = 349
  50. xspd = 0
  51. yspd = 0
  52.  
  53.  
  54.  
  55.     CLS
  56.     IF xp > _WIDTH - 20 THEN xp = _WIDTH - 20
  57.     IF xp < 0 THEN xp = 0
  58.     IF yp > _HEIGHT - 30 THEN yp = _HEIGHT - 30
  59.     IF yp < 0 THEN yp = 0
  60.     PUT (xp, yp), player%(), _CLIP
  61.     _DISPLAY
  62.     te$ = INKEY$
  63.     IF te$ = "d" THEN xp = xp + 2
  64.     IF te$ = "a" THEN xp = xp - 2
  65.     IF te$ = "w" THEN yp = yp - 2
  66.     IF te$ = "s" THEN yp = yp + 2
  67.     IF te$ = "q" THEN END
  68.  
  69.  

_DISPLAY is used here to stop the flickering, and instead of a GOTO, I added a DO/LOOP block.

PS: When you post code in the forum, make sure to add [code=qb64] before and [/code] after your code block.
« Last Edit: February 16, 2020, 08:50:57 pm by FellippeHeitor »

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
Re: last thing to ask today, i promise...
« Reply #4 on: February 17, 2020, 07:06:13 pm »
just wanna check if it runs on dos box or something before i put it in my code

edit: it doesnt

c'mon, i know someone has the solution...

edit2: i tried it by myself, and now my character dies when going straight to the right

i dont even need to put the entire code, so...

Code: QB64: [Select]
  1. xp = 64
  2. yp = 70
  3. xb = 400
  4. yb = 300
  5. xmax = 639
  6. ymax = 349
  7.  
  8.  
  9.  
  10. tecla:
  11. PUT (xp, yp), player%()
  12. PUT (xp, yp), player%()
  13. IF xp <= 1 THEN xp = 1
  14. IF xp > 639 THEN xp = xmax
  15. IF yp <= 1 THEN yp = 1
  16. IF yp >= ymax THEN yp = ymax
  17. te$ = INKEY$
  18. IF te$ = "d" THEN xp = xp + 2
  19. IF te$ = "a" THEN xp = xp - 2
  20. IF te$ = "w" THEN yp = yp - 2
  21. IF te$ = "s" THEN yp = yp + 2
  22. IF te$ = "q" THEN END
  23. GOTO tecla:
  24.  

« Last Edit: February 17, 2020, 07:32:32 pm by codevsb12 »

FellippeHeitor

  • Guest
Re: last thing to ask today, i promise...
« Reply #5 on: February 17, 2020, 07:21:30 pm »
Quote
c'mon, i know someone has the solution...

Love your attitude.

When is your homework due?
« Last Edit: February 17, 2020, 07:51:01 pm by FellippeHeitor »

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: last thing to ask today, i promise...
« Reply #6 on: February 17, 2020, 08:24:08 pm »
The bounds checking should be immediately after you change the variables.
The way you've got it, if that code is in a loop, you'll get PUTs with invalid
parameters.  It probably won't crash QB64, but why take a chance?
It works better if you plug it in.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: last thing to ask today, i promise...
« Reply #7 on: February 18, 2020, 09:20:10 am »
Quote
just wanna check if it runs on dos box or something before i put it in my code

Are you using QB64 or some other QB? Dosbox shouldn't matter if you're using QB64, amirite?
You're not done when it works, you're done when it's right.