Ooops
November 24, 2020, 09:34:55 AM

Author Topic: [bb] 2d RPG player and map example by Pakz [ 1+ years ago ]  (Read 639 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 2d RPG player and map example
Author : Pakz
Posted : 1+ years ago

Description : Someone asked me how the collision was done for one of my monkeyx game video's but in blitzplus. I then decided to make a example where I can link to.

The example shows a map with walls and trees/bushes and gold. You can move the player and get the gold items. They wil be removed from the map if you take them. The trees/bushes you can move through of.

This should be a useable starting point to begin your own rpg.


Code :
Code: BlitzBasic
  1. AppTitle "Topdown RPG player map collision - cursor keys to move."
  2. Graphics 640,480,16,2
  3. SetBuffer BackBuffer()
  4.  
  5. Global mw = 20
  6. Global mh = 15
  7. Global tw = 32
  8. Global th = 32
  9. Global px = 0
  10. Global py = 0
  11. Global pw = tw
  12. Global ph = th
  13.  
  14. Dim map(mw,mh)
  15.  
  16. readlevel()
  17.  
  18. While KeyDown(1) = False
  19.         Cls
  20.         moveplayer
  21.         playeritemcollision
  22.         drawlevel
  23.         drawplayer
  24.         Flip
  25. Wend
  26. End
  27.  
  28. Function moveplayer()
  29.         Local x=0
  30.         Local y=0
  31.         If KeyDown(200) ; up
  32.                 y=-1
  33.         End If
  34.         If KeyDown(205) ; right
  35.                 x=1
  36.         End If
  37.         If KeyDown(208) ; down
  38.                 y=1
  39.         End If
  40.         If KeyDown(203) ; left
  41.                 x=-1
  42.         End If
  43.         If px+x < 0 Then x=0
  44.         If px+x > mw*tw Then x=0
  45.         If py+y < 0 Then y=0
  46.         If py+y > mh*th Then y=0
  47.         If playermapcollision(px+x,py) = False
  48.                 px=px+x
  49.         End If
  50.         If playermapcollision(px,py+y) = False
  51.                 py=py+y
  52.         End If
  53.  
  54. End Function
  55.  
  56. Function playermapcollision(x1,y1)
  57.         Local cx=x1/tw
  58.         Local cy=y1/th
  59.         For y2=cy-1 To cy+1
  60.         For x2=cx-1 To cx+1
  61.                 If x2>=0 And x2<=mw And y2>=0 And y2<=mh
  62.                 If map(x2,y2) = 1 ; is the map around the player a 1 value
  63.                 If RectsOverlap(x2*tw,y2*th,tw,th,x1,y1,pw,ph)
  64.                         ;
  65.                         ; Here the player is inside a wall
  66.                         ; a value 1 on the map
  67.                         ;
  68.                         Return True
  69.                         ;
  70.                 End If
  71.                 End If
  72.                 End If
  73.         Next
  74.         Next
  75.         ; no collision occured
  76.         Return False
  77. End Function
  78.  
  79. Function playeritemcollision()
  80.         Local cx=px/tw
  81.         Local cy=py/th
  82.         For y2=cy-1 To cy+1
  83.         For x2=cx-1 To cx+1
  84.                 If x2>=0 And x2<=mw And y2>=0 And y2<=mh
  85.                 If map(x2,y2) = 3 ; is the map around the player a 3 value
  86.                 If RectsOverlap(x2*tw+8,y2*th+8,tw-16,th-16,px,py,pw,ph)
  87.                         ;
  88.                         ; Here the player touches a map item (3)
  89.                         ; We remove it from the map
  90.                         ;
  91.                         map(x2,y2) = 0
  92.                 End If
  93.                 End If
  94.                 End If
  95.         Next
  96.         Next
  97. End Function
  98.  
  99.  
  100.  
  101. Function drawplayer()
  102.         Color 0,0,255
  103.         Oval px,py,pw,ph
  104. End Function
  105.  
  106. Function drawlevel()
  107.         For y=0 To mh-1
  108.         For x=0 To mw-1
  109.                 If map(x,y) = 1
  110.                         Color 255,255,255
  111.                         Rect x*tw,y*th,tw,th,True
  112.                 End If
  113.                 If map(x,y) = 2
  114.                         Color 0,255,0
  115.                         x1=x*tw
  116.                         y1=y*th
  117.                         Line x1+tw/2,y1,x1+tw,y1+th
  118.                         Line x1+tw,y1+th,x1,y1+th
  119.                         Line x1,y1+th,x1+tw/2,y1
  120.                 End If
  121.                 If map(x,y) = 3
  122.                         Color 200,180,10
  123.                         Oval x*tw+4,y*th+4,tw-8,th-8
  124.                 End If
  125.         Next
  126.         Next
  127. End Function
  128.  
  129. Function readlevel(level=1)
  130.         Select level
  131.                 Case 1
  132.                         Restore level1
  133.         End Select
  134.         For y=0 To mh-1
  135.         For x=0 To mw-1
  136.                 Read a
  137.                 map(x,y) = a
  138.         Next
  139.         Next
  140. End Function
  141.  
  142. .level1
  143. Data 0,0,0,3,3,3,0,1,1,1,1,0,1,1,1,1,1,1,1,1
  144. Data 0,1,1,1,0,0,0,1,0,0,1,0,1,0,0,0,0,3,3,1
  145. Data 0,1,1,3,0,2,0,1,0,0,1,0,1,0,0,0,0,3,3,1
  146. Data 0,1,1,0,0,0,0,1,0,1,1,0,1,1,0,1,1,1,1,1
  147. Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
  148. Data 0,1,0,0,1,0,0,0,0,0,2,0,0,0,0,0,0,1,0,0
  149. Data 0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1
  150. Data 0,1,0,3,1,0,0,2,0,0,0,0,0,0,1,0,0,0,0,0
  151. Data 0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
  152. Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
  153. Data 0,0,1,1,1,1,1,1,1,1,0,0,2,0,1,0,0,0,0,0
  154. Data 0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0
  155. Data 0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0
  156. Data 0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,0,0,0,0,0
  157. Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0


Comments :


Guy Fawkes(Posted 1+ years ago)

 This is awesome, Pakz! Would you consider using an example animated 8-movement character and being able to scroll the map and switch to another map?! That would be INSANELY awesome!Thanks!~GF


Pakz(Posted 1+ years ago)

 I started on it : <a href="https://github.com/Pakz001/blitzplusexamples/blob/master/RPG 2D scrolling multi map.bb" target="_blank">https://github.com/Pakz001/blitzplusexamples/blob/master/RPG%202D%20scrolling%20multi%20map.bb[/url]Switches to another map if you hit the border of the screen.I will need more time to make a animated character.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal