[bb] Scrolling Platformer by Pakz [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Scrolling Platformer
Author : Pakz
Posted : 1+ years ago

Description : Someone asked if I could post the code for this on Blitz Basic. This example is a scrolling platformer with enemies and shooting. The bullets kill the enemy blocks.

controls : cursor left and right, space and c key.

Here is a video of the example : <a href="https://www.youtube.com/watch?v=r95DP0-suk4" target="_blank">https://www.youtube.com/watch?v=r95DP0-suk4</a>


Code :
Code (blitzbasic) Select
; Scrolling platformer with enemies and shooting (c key) by Rudy van Etten
;

Graphics 640,480,32,2
SetBuffer BackBuffer()

Const tilewidth = 32
Const tileheight = 32

Const mapwidth = 39
Const mapheight = 29

Dim map(mapwidth,mapheight)

Global mapx = 0
Global mapy = 0
Global mapsx = 0
Global mapsy = 0

Global firedelay
Global playerx = 4*32
Global playery = 2*32
Global playerdirection
Const playerwidth = 48
Const playerheight = 32
Global pfalling = False
Global pjumping = False
Global pjs# = 0.0
Global pfs# = 0.0

Const enemywidth = 32
Const enemyheight = 32

Type enemy
Field x#,y#
Field mx#
Field enemydirection
Field deleteme
End Type

Const bulletwidth = 8
Const bulletheight = 8

Type bullet
Field x#,y#,mx#
Field bulletdirection
Field deleteme
Field timeout
End Type


readlevel()

While KeyDown(1) = False
Cls
For i=0 To 2
moveplayer()
movemap()
playergravity()
Next
updateenemies()
updatebullets()
playerbulletcollision()
drawlevel()
drawenemies()
drawbullets()
drawplayer()
Flip
Wend
End

Function playerbulletcollision()
For this.bullet = Each bullet
For that.enemy = Each enemy
If RectsOverlap(thisx,thisy,bulletwidth,bulletheight,thatx,thaty,enemywidth,enemyheight) = True Then
thisdeleteme = True
thatdeleteme = True
End If
Next
Next
End Function

Function updatebullets()
Local tx#,ty#
For this.bullet = Each bullet
tx = thisx
ty = thisy
tx=tx+thismx
this imeout = this imeout + 1
If this imeout > 300
thisdeleteme = True
End If
If bmapcollision(tx-32,ty-32) = True Then
thisdeleteme = True
End If
thisx = tx
thisy = ty
Next
For that.bullet = Each bullet
If thatdeleteme = True Then
Delete that
End If
Next
End Function

Function drawbullets()
For this.bullet = Each bullet
Color 0,0,255
Oval thisx-(mapx*tilewidth)+mapsx-tilewidth,thisy-(mapy*tileheight)+mapsy-tileheight,bulletwidth,bulletheight,True
Next
End Function

Function updateenemies()
Local tx,ty,goahead
For this.enemy = Each enemy
tx = thisx
ty = thisy
If thisenemydirection = -1 Then
tx=tx - 1
Else
tx=tx + 1
End If
goahead = True
Select thisenemydirection
Case -1
If emapcollision(tx-32,ty-32) = True Or emapcollision(tx-64,ty) = False Then
thisenemydirection = 1
goahead = False
End If
Case 1
If emapcollision(tx-32,ty-32) = True Or emapcollision(tx,ty) = False Then
thisenemydirection = -1
goahead = False
End If
End Select
If goahead = True Then
thisx = tx
thisy = ty
End If
Next
For that.enemy = Each enemy
If thatdeleteme = True Then
Delete that
End If
Next
End Function

Function drawenemies()
For this.enemy = Each enemy
Color 255,255,0
Rect thisx-(mapx*tilewidth)+mapsx-tilewidth,thisy-(mapy*tileheight)+mapsy-tileheight,enemywidth,enemyheight,True
Next
End Function

Function playergravity()
If pmapcollision(playerx,playery+1) = False
If pfalling = False And pjumping = False
pfalling = True
pfs = 0.0
End If
End If
If pfalling = True Then
If pfs < 2
pfs = pfs + 0.1
End If
For i=0 To pfs
If pmapcollision(playerx,playery+1) = True
pfalling = False
pfs = 0.0
Else
playery = playery + 1
End If
Next
End If
If pjumping = True Then
pjs = pjs - .1
If pjs < 0 Then
pjumping = False
pfalling = True
pfs = 0.0
End If
For i=0 To pjs
If pmapcollision(playerx,playery-1) = True
pjumping = False
pfalling = True
pfs = 0.0
Exit
Else
playery = playery - 1
End If
Next
End If
End Function

Function bmapcollision(px,py)
Local pcx = px / tilewidth
Local pcy = py / tileheight

For y=-2 To 3
For x=-2 To 3
If pcx + x > 0 And pcx+x <mapwidth And pcy + y > 0 And pcy+y<mapheight
If map(pcx+x,pcy+y) = 1
xx = ((pcx)+x)*tilewidth-tilewidth
yy = ((pcy)+y)*tileheight-tileheight
If RectsOverlap(px,py,bulletwidth,bulletheight,xx,yy,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
End Function


Function emapcollision(px,py)
Local pcx = px / tilewidth
Local pcy = py / tileheight

For y=-2 To 3
For x=-2 To 3
If pcx + x > 0 And pcx+x <mapwidth And pcy + y > 0 And pcy+y<mapheight
If map(pcx+x,pcy+y) = 1
xx = ((pcx)+x)*tilewidth-tilewidth
yy = ((pcy)+y)*tileheight-tileheight
If RectsOverlap(px,py,enemywidth,enemyheight,xx,yy,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
End Function

Function pmapcollision(px,py)
Local pcx = mapx + (px / tilewidth)
Local pcy = mapy + (py / tileheight)

For y=-2 To 3
For x=-2 To 3
If pcx + x > 0 And pcx+x <mapwidth And pcy + y > 0 And pcy+y<mapheight
If map(pcx+x,pcy+y) = 1
xx = ((pcx-mapx)+x)*tilewidth+mapsx-tilewidth
yy = ((pcy-mapy)+y)*tileheight+mapsy-tileheight
If RectsOverlap(px,py,playerwidth,playerheight,xx,yy,tilewidth,tileheight) = True
Return True
End If
End If
End If
Next
Next
End Function

Function movemap()
If playerx > GraphicsWidth()/2
If mapx < (mapwidth - GraphicsWidth() / tilewidth)
mapsx = mapsx - 1
playerx= playerx - 1
If mapsx < 0 Then
mapsx = tilewidth
mapx = mapx + 1
End If
End If
End If
If playerx < GraphicsWidth()/2
If mapx > 0
mapsx = mapsx + 1
playerx = playerx + 1
If mapsx > tilewidth
mapsx = 0
mapx = mapx - 1
End If
End If
End If
If playery > GraphicsHeight() / 2
If mapy < (mapheight - GraphicsHeight() / tileheight)
If pmapcollision(playerx,playery-1) = False
mapsy = mapsy - 1
playery = playery - 1
If mapsy < 0
mapsy = tileheight
mapy = mapy + 1
End If
End If
End If
End If
If playery < GraphicsHeight() / 2
If mapy > 0
If pmapcollision(playerx,playery+1) = False
mapsy = mapsy + 1
playery = playery + 1
If mapsy > tileheight
mapsy = 0
mapy = mapy - 1
End If
End If
End If
End If
End Function

Function moveplayer()
Local px = playerx
Local py = playery
If KeyDown(203) ; left
If pmapcollision(px-1,py) = False
px = px - 1
End If
playerdirection = -1
End If
If KeyDown(205) ; right
If pmapcollision(px+1,py) = False
px = px + 1
End If
playerdirection = 1
End If
If KeyDown(57) ; space
If pjumping = False And pfalling = False
pjumping = True
pjs = 4.0
End If
End If
If KeyDown(46) ; c fire key
If firedelay < MilliSecs()
this.bullet = New bullet
Select playerdirection
Case -1
thisx = playerx+tilewidth-8+(mapx*tilewidth)
thisy = playery+(mapy*tileheight)-mapsy+tileheight
thismx = -1
thisulletdirection = -1
Case 1
thisx = playerx + playerwidth+tilewidth+(mapx*tilewidth)
thisy = playery+(mapy*tileheight)-mapsy+tileheight
thismx = 1
thisulletdirection = 1
End Select
firedelay = MilliSecs()+500
End If
End If

playerx = px
playery = py
End Function

Function drawplayer()
Color 255,0,0
Rect playerx,playery,playerwidth,playerheight,True
End Function

Function drawlevel()
For y=0 To GraphicsHeight() / tileheight
For x=0 To GraphicsWidth() / tilewidth
If map(x+mapx,y+mapy) = 1 Then
Color 255,255,255
Rect x*tilewidth+mapsx-tilewidth,y*tileheight+mapsy-tileheight,32,32,True
End If
Next
Next
End Function

Function readlevel()
For y=0 To mapheight
For x=0 To mapwidth
Read a
Select a
Case 1
map(x,y) = 1
Case 2
this.enemy = New enemy
thisx = x*tilewidth+tilewidth
thisy = y*tileheight
thisenemydirection = -1
thismx = 0.1
End Select
Next
Next
End Function

.leveldata
Data 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,1,1
Data 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,1,1
Data 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,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,0,0,2,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1,0
Data 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,1,1
Data 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,1,1


Comments :


Guy Fawkes(Posted 1+ years ago)

 Why is it so laggy?


Pakz(Posted 1+ years ago)

 The player block does not move that fluent if that is what you mean. I have no idea why it happens.


Darkseid2.0(Posted 1+ years ago)

 Works perfectly, I had no issues running the example.Great work. Keep it up.