Code: Select all
screenres 800, 600, 32
Dim shared as integer ball_x = 400, ball_y = 300
Dim shared as integer paddle_x = 350, paddle_y = 550
Dim as integer ball_dx = 1, ball_dy = 1
Dim shared as integer score = 0
Dim shared as integer ai_offset = 40
Dim As String key
Dim As Long fps
Declare Sub delay(ByVal amt As Single, ByVal thr As Ulong = 2)
Sub delay(ByVal amt As Single, ByVal thr As Ulong)
Dim As Double t1 = Timer
Dim As Double t2 = t1 + amt / 1000
If amt > thr + 0.5 Then Sleep amt - thr, 1
Do
Loop Until Timer >= t2
End Sub
Do
key =Inkey
' Update AI paddle position
If ball_x > (paddle_x + ai_offset + 10) then
paddle_x += 3
elseIf ball_x < (paddle_x + ai_offset - 10) then
paddle_x -= 3
endIf
' Update ball position
ball_x += ball_dx
ball_y += ball_dy
' Check collision with paddle
If ball_y > paddle_y then
If ball_x > paddle_x and ball_x < (paddle_x + 100) then
ball_dy = -ball_dy ' Bounce the ball
score += 1
else
' Game over
sleep
exit do
endIf
endIf
' Check collision with walls
If ball_x < 0 or ball_x > 800 then
ball_dx = -ball_dx ' Bounce the ball
endIf
If ball_y < 0 then
ball_dy = -ball_dy ' Bounce the ball
endIf
' Render
Screenlock
cls
line(0, 590)-(800, 590), rgb(255, 255, 255), bf
line(ball_x, ball_y)-(ball_x + 10, ball_y + 10), rgb(255, 255, 255), bf
line(paddle_x, paddle_y)-(paddle_x + 100, paddle_y + 10), rgb(255, 255, 255), bf
Screenunlock
'adjust as needed
delay 1.6
Loop Until (key = Chr(27)) Or (key = Chr(255) & "k")