November 24, 2020, 09:08:50 AM

Author Topic: [bb] 2d scrolling Platformer by Pakz [ 1+ years ago ]  (Read 511 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2d scrolling Platformer by Pakz [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : 2d scrolling Platformer
Author : Pakz
Posted : 1+ years ago

Description : A simple 2d scrolling platformer with jumping and falling.

The speed of the player movement can be set in the main loop. It is currently set at fast (for i=0 to 5).


Code :
Code: BlitzBasic
  1. ; 2d Platformer with scrolling by pakz
  2. ;
  3.  
  4. Graphics 640,480,32,2
  5. SetBuffer BackBuffer()
  6.  
  7. Const mapwidth = 39
  8. Const mapheight = 29
  9. Const cellwidth = 32
  10. Const cellheight = 32
  11. Global mapx = 0
  12. Global mapy = 0
  13. Global mapsx = 0
  14. Global mapsy = 0
  15.  
  16. Dim map(mapwidth,mapheight)
  17.  
  18. Global playerx = 2*cellwidth
  19. Global playery = 2*cellheight
  20. Global playerwidth = 32
  21. Global playerheight = 32
  22. Global playerfalling = False
  23. Global playerjumping = False
  24. Global pjs# = 0.0 ; player jumping speed
  25. Global pfs# = 0.0 ; player falling speed
  26.  
  27. readlevel()
  28.  
  29. While KeyDown(1) = False
  30.         Cls
  31.         For i=0 To 5
  32.                 moveplayer()
  33.                 playergravity()
  34.                 movemap()
  35.         Next
  36.         drawlevel()
  37.         drawplayer()
  38.         Flip
  39. Wend
  40. End
  41.  
  42. Function playergravity()
  43.         If playerjumping = False And playerfalling = False
  44.                 If pmapcollision(playerx,playery+1) = False
  45.                         playerfalling = True
  46.                 End If
  47.         End If
  48.         If playerfalling = True
  49.                 If pfs < 3 Then pfs = pfs + 0.1
  50.                 For i=0 To pfs
  51.                         If pmapcollision(playerx,playery+1) = False Then
  52.                                 playery = playery + 1
  53.                                 Else
  54.                                 playerfalling = False
  55.                                 Exit
  56.                         End If
  57.                 Next           
  58.         End If
  59.         If playerjumping = True
  60.                 pjs = pjs - 0.1
  61.                 If pjs < 0
  62.                         playerjumping = False
  63.                         playerfalling = True
  64.                         pfs = 0.0
  65.                 End If
  66.                 For i=0 To pjs
  67.                         If pmapcollision(playerx,playery-1) = False Then
  68.                                 playery = playery - 1                          
  69.                         Else
  70.                                 playerjumping = False
  71.                                 playerfalling = True
  72.                                 pfs = 0.0                              
  73.                                 Exit
  74.                         End If
  75.                 Next
  76.         End If
  77. End Function
  78.  
  79. Function pmapcollision(px,py)
  80.         Local pcx = mapx + (px / cellwidth)
  81.         Local pcy = mapy + (py / cellheight)
  82.         For y=-2 To 2
  83.         For x=-2 To 2
  84.                 If pcx + x > 0 And pcx + x < mapwidth And pcy + y > 0 And pcy + y < mapheight
  85.                         If map(pcx+x,pcy+y) = 1
  86.                                 xx = ((pcx-mapx)+x)*cellwidth+mapsx-cellwidth
  87.                                 yy = ((pcy-mapy)+y)*cellheight+mapsy-cellheight
  88.                                 If RectsOverlap(px,py,playerwidth,playerheight,xx,yy,cellwidth,cellheight)
  89.                                         Return True
  90.                                 End If
  91.                         End If
  92.                 End If
  93.         Next
  94.         Next
  95. End Function
  96.  
  97. Function movemap()
  98.         If playerx > GraphicsWidth() / 2
  99.                 If mapx < mapwidth - GraphicsWidth() / cellwidth
  100.                         mapsx = mapsx - 1
  101.                         playerx = playerx - 1
  102.                         If mapsx < 0
  103.                                 mapsx = cellwidth
  104.                                 mapx = mapx + 1
  105.                         End If
  106.                 End If
  107.         End If
  108.         If playerx < GraphicsWidth() / 2
  109.                 If mapx > 0
  110.                         mapsx = mapsx + 1
  111.                         playerx = playerx + 1
  112.                         If mapsx > cellwidth
  113.                                 mapsx = 0
  114.                                 mapx = mapx - 1
  115.                         End If
  116.                 End If
  117.         End If
  118.         If playery > GraphicsHeight() / 2
  119.                 If mapy < mapheight - GraphicsHeight() / cellheight
  120.                         If pmapcollision(playerx,playery-1) = False
  121.                         mapsy = mapsy - 1
  122.                         playery = playery - 1
  123.                         If mapsy < 0
  124.                                 mapsy = cellheight
  125.                                 mapy = mapy + 1
  126.                         End If
  127.                         End If
  128.                 End If 
  129.         End If
  130.         If playery < GraphicsHeight() / 2
  131.                 If mapy > 0
  132.                         If pmapcollision(playerx,playery+1) = False
  133.                         mapsy = mapsy + 1
  134.                         playery = playery + 1
  135.                         If mapsy > cellheight
  136.                                 mapsy = 0
  137.                                 mapy = mapy - 1
  138.                         End If
  139.                         End If
  140.                 End If
  141.         End If
  142.        
  143. End Function
  144.  
  145. Function moveplayer()
  146.         Local px = playerx
  147.         Local py = playery
  148.         If KeyDown(203) And pmapcollision(px-1,py) = False ; left
  149.                 px = px - 1
  150.         End If
  151.         If KeyDown(205) And pmapcollision(px+1,py) = False ; right
  152.                 px = px + 1
  153.         End If
  154.         If KeyDown(57) And playerjumping = False And playerfalling = False
  155.                 pjs = 4.0
  156.                 playerjumping =  True
  157.         End If
  158.         playerx = px
  159.         playery = py
  160. End Function
  161.  
  162. Function drawplayer()
  163.         Color 0,0,255
  164.         Rect playerx,playery,playerwidth,playerheight,True
  165. End Function
  166.  
  167. Function drawlevel()
  168.         Color 255,255,255
  169.         For y=0 To GraphicsHeight() / cellheight
  170.         For x=0 To GraphicsWidth() / cellwidth
  171.                 Select map(x+mapx,y+mapy)
  172.                         Case 1
  173.                                 Rect x*cellwidth-cellwidth+mapsx,y*cellheight-cellheight+mapsy,cellwidth,cellheight,True
  174.                 End Select
  175.         Next
  176.         Next
  177. End Function
  178.  
  179. Function readlevel()
  180.         For y=0 To mapheight
  181.         For x=0 To mapwidth
  182.                 Read a
  183.                 Select a
  184.                 Case 1
  185.                 map(x,y) = a
  186.                 End Select
  187.         Next
  188.         Next
  189. End Function
  190.  
  191. .leveldata
  192. Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  193. Data 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0
  194. Data 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1,1,1,1,0
  218. Data 0,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0
  219. Data 0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0
  220. Data 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0
  221. Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1


Comments :


Darkseid2.0(Posted 1+ years ago)

 I like this, I can make just a few adjustments and turn it into a simple platform game.Great work. Keep it up.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal