Author Topic: How do I make collision work between the player and the level?  (Read 3179 times)

0 Members and 1 Guest are viewing this topic.

Offline Mudkip In Disguise

  • Newbie
  • Posts: 3
    • View Profile
I am trying to make a game where the player walks through rooms.
I can't figure out how to prevent the player from walking through the walls.
Move with WASD
if the screen is black when ran, press WASD

***Here is the code***
Code: QB64: [Select]
  1. 'COLOR VARIBLES AND STUFF
  2. MAPX = 15
  3. MAPY = 15
  4. PC = 7
  5. SC = 8
  6. FC = 6
  7. CC = 4
  8. x = 3
  9. y = 3
  10.     'This makes the map
  11.     jail:
  12.     DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  13.     DATA 1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,2
  14.     DATA 1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1
  15.     DATA 1,0,0,0,1,0,0,0,1,1,1,1,1,0,1,1
  16.     DATA 1,3,0,3,1,3,3,3,1,0,0,0,0,0,0,1
  17.     DATA 1,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1
  18.     DATA 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1
  19.     DATA 1,1,1,1,1,1,2,1,1,1,1,1,0,1,1,1
  20.     DATA 1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1
  21.     DATA 1,0,0,0,0,0,0,0,1,0,1,1,1,1,0,1
  22.     DATA 1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,1
  23.     DATA 1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1
  24.     DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  25.     DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  26.     DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  27.     DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  28.  
  29.     'KEYPRESSES
  30.     PRINT MID$(PSCANY$, y - 1, y - 1)
  31.     K$ = LCASE$(INKEY$)
  32.     IF K$ = "w" THEN
  33.         y = y - 1: u = 1
  34.         IF a = y AND u = 1 AND num = 1 THEN
  35.             y = y + 1
  36.         END IF
  37.     END IF
  38.     IF K$ = "s" THEN
  39.         y = y + 1: d = 1
  40.         IF a = y AND d = 1 AND num = 1 THEN
  41.             y = y - 1
  42.             d = 0
  43.         END IF
  44.     END IF
  45.     IF K$ = "a" THEN x = x - 1: l = 1
  46.     IF K$ = "d" THEN x = x + 1: r = 1
  47.     IF K$ = "w" OR K$ = "s" OR K$ = "a" OR K$ = "d" THEN
  48.         'RENDER/UPDATE ROOM WHEN MOVEMENT IS DETECTED
  49.         PLAY "MB O1 GL64"
  50.         RESTORE jail:
  51.         DIM room(15, 15)
  52.         FOR a = 0 TO MAPX
  53.             FOR b = 0 TO MAPY
  54.                 READ room
  55.                 num = room
  56.                 IF num = 0 THEN
  57.                     LOCATE a + 1, b + 1
  58.                     COLOR CC, FC
  59.                     PRINT "_"
  60.  
  61.  
  62.                 END IF
  63.  
  64.                 IF num = 1 THEN
  65.                     LOCATE a + 1, b + 1
  66.                     COLOR PC, SC
  67.                     PRINT "°"
  68.                 END IF
  69.  
  70.                 IF num = 2 THEN
  71.                     LOCATE a + 1, b + 1
  72.                     COLOR 9, 12
  73.                     PRINT ""
  74.  
  75.  
  76.                 END IF
  77.                 IF num = 3 THEN
  78.                     LOCATE a + 1, b + 1
  79.                     COLOR 3, FC
  80.                     PRINT "Ñ"
  81.  
  82.  
  83.                 END IF
  84.                 u = 0
  85.  
  86.  
  87.                 LOCATE 20, 10
  88.             NEXT b
  89.  
  90.         NEXT a
  91.  
  92.         'DRAW PLAYER
  93.         LOCATE y, x
  94.         COLOR 14, 0
  95.         PRINT ""
  96.         _DISPLAY
  97.         CLS
  98.  
  99.  
  100.     END IF
  101.  
« Last Edit: March 22, 2020, 02:59:06 pm by Mudkip In Disguise »

Marked as best answer by Mudkip In Disguise on March 22, 2020, 11:29:40 am

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: How do I make collision work between the player and the level?
« Reply #1 on: March 22, 2020, 03:10:33 pm »
This is just a quick hack to show you the use of using an array to hold your map level in. With the changes I made to your code everything is managed within the array.

(I had to change the code, this is the second change. You had ASCII characters embedded using the ALT123 method which was causing havoc with the code box)

Code: QB64: [Select]
  1. TYPE ROOMS '                each room in level
  2.     fcolor AS INTEGER '     foreground color
  3.     bcolor AS INTEGER '     background color
  4.     char AS STRING * 1 '    room character
  5.  
  6. '_FULLSCREEN
  7. 'COLOR VARIBLES AND STUFF
  8. MAPX = 15
  9. MAPY = 15
  10. PC = 7
  11. SC = 8
  12. FC = 6
  13. CC = 4
  14.  
  15. '** Create the level
  16.  
  17. DIM Map(MAPX, MAPY) AS ROOMS '              create 2D array to hold the level full of rooms
  18. FOR y = 0 TO MAPY '                         cycle horizontally
  19.     FOR x = 0 TO MAPX '                     cycle vertically
  20.         READ room '                         read in the next room
  21.         SELECT CASE room '                  which room is it?
  22.             CASE 0 '                        room type 0
  23.                 Map(x, y).fcolor = CC '     set characteristics of this room
  24.                 Map(x, y).bcolor = FC
  25.                 Map(x, y).char = "_"
  26.             CASE 1 '                        room type 1
  27.                 Map(x, y).fcolor = PC
  28.                 Map(x, y).bcolor = SC
  29.                 Map(x, y).char = CHR$(176)
  30.             CASE 2 '                        room type 2
  31.                 Map(x, y).fcolor = 9
  32.                 Map(x, y).bcolor = 12
  33.                 Map(x, y).char = CHR$(8)
  34.             CASE 3 '                        room type 3
  35.                 Map(x, y).fcolor = 3
  36.                 Map(x, y).bcolor = FC
  37.                 Map(x, y).char = CHR$(209)
  38.         END SELECT
  39.     NEXT x
  40.  
  41. x = 2 '  character location
  42. y = 2
  43. px = 2 ' previous character location
  44. py = 2
  45.  
  46.     _LIMIT 30 ' 30 frames per second (don't want to hog CPU)
  47.     CLS
  48.  
  49.     'KEYPRESSES
  50.     PRINT MID$(PSCANY$, y - 1, y - 1)
  51.     K$ = LCASE$(INKEY$)
  52.  
  53.     px = x
  54.     py = y
  55.  
  56.     IF K$ = "w" AND Map(x, y - 1).char = "_" THEN
  57.         y = y - 1
  58.     END IF
  59.     IF K$ = "s" AND Map(x, y + 1).char = "_" THEN
  60.         y = y + 1
  61.     END IF
  62.     IF K$ = "a" AND Map(x - 1, y).char = "_" THEN
  63.         x = x - 1
  64.     END IF
  65.     IF K$ = "d" AND Map(x + 1, y).char = "_" THEN
  66.         x = x + 1
  67.     END IF
  68.  
  69.     Map(px, py).char = "_"
  70.     Map(x, y).char = CHR$(2)
  71.  
  72.     IF K$ = "w" OR K$ = "s" OR K$ = "a" OR K$ = "d" THEN
  73.         'RENDER/UPDATE ROOM WHEN MOVEMENT IS DETECTED
  74.         PLAY "MB O1 GL64"
  75.  
  76.         '** draw the level
  77.  
  78.         FOR b = 0 TO MAPY
  79.             FOR a = 0 TO MAPX
  80.                 IF Map(a, b).char = CHR$(2) THEN
  81.                     COLOR 14, 0
  82.                 ELSE
  83.                     COLOR Map(a, b).fcolor, Map(a, b).bcolor
  84.                 END IF
  85.                 PRINT Map(a, b).char;
  86.             NEXT a
  87.             PRINT
  88.         NEXT b
  89.  
  90.         _DISPLAY
  91.  
  92.     END IF
  93.  
  94. DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  95. DATA 1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,2
  96. DATA 1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1
  97. DATA 1,0,0,0,1,0,0,0,1,1,1,1,1,0,1,1
  98. DATA 1,3,0,3,1,3,3,3,1,0,0,0,0,0,0,1
  99. DATA 1,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1
  100. DATA 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1
  101. DATA 1,1,1,1,1,1,2,1,1,1,1,1,0,1,1,1
  102. DATA 1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1
  103. DATA 1,0,0,0,0,0,0,0,1,0,1,1,1,1,0,1
  104. DATA 1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,1
  105. DATA 1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1
  106. DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  107. DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  108. DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  109. DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  110.  
« Last Edit: March 22, 2020, 03:20:18 pm by TerryRitchie »
In order to understand recursion, one must first understand recursion.

Offline Mudkip In Disguise

  • Newbie
  • Posts: 3
    • View Profile
Re: How do I make collision work between the player and the level?
« Reply #2 on: March 22, 2020, 03:25:43 pm »
Thank you!

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: How do I make collision work between the player and the level?
« Reply #3 on: March 22, 2020, 03:42:14 pm »
Thank you!

You're welcome :-)  If you would like a more thorough explanation of what I did I can post that. I'm busy with a home project right now and during a break saw your post and responded.
In order to understand recursion, one must first understand recursion.

Offline Mudkip In Disguise

  • Newbie
  • Posts: 3
    • View Profile
Re: How do I make collision work between the player and the level?
« Reply #4 on: March 22, 2020, 04:18:03 pm »
This looks very interesting. I would like to see a bit of an in-depth explanation about how this works.