Post by Aaditya Parashar on Oct 4, 2023 16:12:38 GMT
A basic coloured raycaster.
Works in qb4.5
Works in qb4.5
Type Vec2
X As Single
Y As Single
End Type
Dim WorldSizeW, WorldSizeH, Player As Vec2
WorldSizeW = 24
WorldSizeH = 24
Player.X = 2
Player.Y = 2
Const pi = 22 / 7
Dim World(1 To WorldSizeW, 1 To WorldSizeH) As Integer
For i = 1 To WorldSizeW
For j = 1 To WorldSizeH
Read World(i, j)
Next j, i
Data 1,2,3,4,3,2,1,2,3,4,3,2,1,2,3,4,3,2,1,2,3,4,3,2
Data 2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 3,0,1,5,5,0,4,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
Data 4,0,2,0,6,0,7,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3
Data 3,0,3,0,7,0,4,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4
Data 2,0,0,0,8,0,6,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3
Data 1,0,9,0,0,0,6,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
Data 2,0,3,2,1,2,3,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
Data 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3
Data 3,0,0,0,0,0,0,0,3,0,0,0,3,3,3,3,0,0,0,0,0,0,0,4
Data 2,0,0,0,0,0,0,0,3,0,0,0,4,0,0,3,0,0,0,0,0,0,0,3
Data 1,0,0,0,0,0,0,0,3,0,0,0,4,0,0,3,0,0,0,0,0,0,0,2
Data 2,0,0,0,0,0,0,0,3,0,0,0,0,0,0,3,0,0,0,0,0,0,0,1
Data 3,0,0,0,0,0,0,0,3,0,0,0,0,0,0,3,0,0,0,0,0,0,0,2
Data 4,0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,3
Data 3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4
Data 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,2
Data 2,0,0,0,0,0,0,0,0,0,0,0,3,4,5,6,7,8,9,2,1,0,0,1
Data 3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
Data 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3
Data 3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4
Data 2,3,4,3,2,1,2,3,4,3,2,1,2,3,4,3,2,1,2,3,4,3,2,1
Const FOV = 90
Screen 12
ms = 0.2
playerangle = 0
Dim keys(1 To 32) As Integer
Do
Line (0, 0)-(640, 480), 11, BF
Delay 1 / 30
k = Inp(&H60)
If k = 17 Then
If World(Int(Player.X + Cos(playerangle * pi / 180) / 2), Int(Player.Y)) = 0 Then Player.X = Player.X + Cos(playerangle * pi / 180) * ms
If World(Int(Player.X), Int(Player.Y + Sin(playerangle * pi / 180) / 2)) = 0 Then Player.Y = Player.Y + Sin(playerangle * pi / 180) * ms
End If
If k = 31 Then
If World(Int(Player.X - Cos(playerangle * pi / 180) / 2), Int(Player.Y)) = 0 Then Player.X = Player.X - Cos(playerangle * pi / 180) * ms
If World(Int(Player.X), Int(Player.Y - Sin(playerangle * pi / 180) / 2)) = 0 Then Player.Y = Player.Y - Sin(playerangle * pi / 180) * ms
End If
If k = 30 Then playerangle = playerangle - 3.5
If k = 32 Then playerangle = playerangle + 3.5
If k = 1 Then End
For x = -320 To 320 Step 1
angle = FOV * x / 640 + playerangle
xx = Player.X
yy = Player.Y
dist! = 0
d = 0.01
Do
xx = xx + Cos(angle * pi / 180) * d
yy = yy + Sin(angle * pi / 180) * d
dist! = dist! + d
If dist! > 3 And dist! < 11 Then d = dist! / 20 - 0.05
If xx > WorldSizeW Or xx < 1 Or yy > WorldSizeH Or yy < 1 Then Exit Do
If World(xx, yy) <> 0 Then Exit Do
Loop
y = 240 / dist!
If xx > WorldSizeW Or xx < 1 Or yy > WorldSizeH Or yy < 1 Then _Continue
Line (x + 321, 240 - y)-(x + 321, 240 + y), World(xx, yy)
Next x
_Display
Loop
Sub Delay (d!)
s! = Timer
Do While s! + d! >= Timer
If s! > Timer Then s! = s! - 86400
Loop
End Sub