Code: Select all
'Pong physics test 1
'by SJ Zero
'09 Oct 2018
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif
screen 13,8,2
Const PI As Double = 3.1415926535897932
dim quit as Integer
dim Active_Screen as Integer
dim Passive_Screen as Integer
dim Blocks (0 to 10, 0 to 5) as Integer
dim x as Integer
dim y as integer
for x = 0 to 10
for y = 0 to 5
blocks (x,y) = int(rnd(x+y+timer) * 5)
next
next
type ball
x as Double
y as Double
xaccel as Double
yaccel as Double
mass as Double
radius as Double
refractAngleX as double
refractAngleY as double
end type
Type paddle
x as Double
y as Double
length as double
Angle as double
end type
dim ball as ball
dim paddle as paddle
dim distance as double
ball.x = 100
ball.y = 100
ball.xaccel = 1
ball.yaccel = 1
ball.mass = 1
ball.radius = 4
paddle.y = 180
paddle.x = 150
paddle.length = 40
while quit = 0
'The high tech controls
dim keykey as String
dim lastkey as String
lastkey = trim(inkey)
while inkey <> ""
wend
If MultiKey(SC_LEFT ) Then
if paddle.x > 10 then paddle.x -= 4
end if
If MultiKey(SC_RIGHT) Then
if paddle.x + paddle.length < 310 then paddle.x += 4
end if
select case lastkey
case "q"
quit = 1
case ""
case chr(255)+"K" 'Left arrow
case chr(255)+"M" 'right arrow
case else
print lastkey
print asc(lastkey,1),asc(lastkey,2)
end select
' The high tech graphics
cls
line (paddle.x,paddle.y)-(paddle.x + paddle.length,paddle.y),2
circle (ball.x,ball.y),ball.radius,1,,,,f
ball.refractAngleX = Ball.xAccel + ((ball.x - (paddle.x + (paddle.length / 2))) * 0.06)
ball.refractAngleY = -Ball.yAccel
line (10,10)-(10,310)
line (10,10)-(10,200)
line (310,200)-(310,10)
line (10,10)-(310,10)
for x = 0 to 10
for y = 0 to 5
if blocks (x,y) > 0 then
line (50 + (x * 20),(50 + (y * 10)))-(50 + ((x+1) * 20),(50 + ((y+1) * 10))), blocks (x,y),bf
line (50 + (x * 20),(50 + (y * 10)))-(50 + ((x+1) * 20),(50 + ((y+1) * 10))), ,b
end if
next
next
if Active_Screen = 1 then Active_Screen = 0 else Active_Screen = 1
if Active_Screen = 1 then Passive_Screen = 0 else Passive_Screen = 1
Screen , Active_Screen, Passive_Screen
'The high tech physics
for x = 0 to 10
for y = 0 to 5
if blocks (x,y) > 0 then
dim blockx as Double
dim blocky as double
dim blockx1 as Double
dim blocky1 as Double
blockX = 50 + (x * 20)
BlockY = 50 + (y * 10)
BlockX1 = 50 + ((x+1) * 20)
BlockY1 = 50 + ((y+1) * 10)
if ball.x + ball.xaccel + ball.radius > blockX and ball.x + ball.xaccel - ball.radius < BlockX1 then
if ball.y + ball.yaccel + ball.radius > blocky and ball.y + ball.yaccel - ball.radius < Blocky1 then
blocks(x,y) -= 1
if ball.x + ball.xaccel + ball.radius > blockX and ball.x + ball.xaccel - ball.radius < BlockX1 and _
ball.y + ball.radius > blocky and ball.y - ball.radius < Blocky1 then
Ball.xaccel = -ball.xaccel
end if
if ball.x + ball.radius > blockX and ball.x - ball.radius < BlockX1 and _
ball.y + ball.yaccel + ball.radius > blocky and ball.y + ball.yaccel - ball.radius < Blocky1 then
ball.yaccel = -ball.yaccel
end if
exit for
end if
end if
end if
next
next
ball.x = ball.x + ball.xaccel
ball.y = ball.y + ball.yaccel
distance = abs(paddle.x + (paddle.length / 2) - ball.x)
if (distance < (ball.radius + (paddle.length / 2))) and (ball.y + ball.radius) > paddle.y then
ball.xaccel = ball.refractAngleX
ball.yAccel = Ball.RefractAngleY
end if
if (ball.x - ball.radius) < 10 then
ball.xaccel = -ball.xaccel
end if
if (ball.y - ball.radius) < 10 then
ball.yaccel = -ball.yaccel
end if
if (ball.x + ball.radius) > 310 then
ball.xaccel = -ball.xaccel
end if
if (ball.y + ball.radius) > 200 then
screen ,0, 0
cls
print "You died."
sleep
end 'dead!
end if
dim won as Integer
won = 1
for x = 0 to 10
for y = 0 to 5
if blocks(x,y) > 0 then won = 0
next
next
if won = 1 then
screen ,0, 0
cls
print "You won. You monster."
sleep
end 'dead!
end if
'The high tech timing
sleep 15
wend