_Title "Reflective Pong" 'try a modern structured approach 2019-06-20 by B+ ' 2020-08-28 fix how ball starts
' 2021-07-31 fix AI play when ball is behind paddle, just give up the point too ugly otherwise
Const xmax
= 800, ymax
= 600
initGame
playGame
Sub initGame
'most of theses remain constant start with table table.x = 50: table.y = 150: table.w = 700: table.h = 400: table.clr = &HFF005046
computer.id = "Computer": computer.pts = 0 ' computer on the left
computer.pdl.w = 15: computer.pdl.clr = &HFF0000FF
computer.pdl.x = table.x + 3 * computer.pdl.w
human.id = "Human": human.pts = 0 '
tryAgain: ' human player on the right
Print " Levels, Enter:" + l
+ " 1 for Super Pro" + l
+ " 2 for Pro" + l
+ " 3 Human" + l
+ " 4 Handicapped" + l
+ " 5 Bigger Handicap" + l
+ " 6 Help Me!" + l
Case 1: human.id
= "Super Pro" Case 3: human.id
= "Human" Case 4: human.id
= "Handicapped" Case 5: human.id
= "Bigger Handicap" Case 6: human.id
= "Help Me!": mult
= 12 human.pdl.w = mult * 5: human.pdl.h = 40: human.pdl.clr = &HFFFF0000
ball.s = 5: ball.r = 5: ball.clr = &HFFFFFFFF ' ball
midC = table.x + .25 * table.w
Print:
Print:
Print " Move your mouse to position the red circle (paddle)..."
getBallRolling
inbounds = 1
If my
> table.y
And my
<= table.y
+ table.h
Then ' update human paddle If mx
> table.x
+ .5 * table.w
And mx
< table.x
+ table.w
Then human.pdl.x = mx: human.pdl.y = my
cx
= cx
+ _Pi(1 / 80): cy
= cy
+ _Pi(1 / 40) If ball.x
> computer.pdl.x
Then computer.pdl.x
= midC
+ (.5 * midC
- 10) * Sin(cx
) computer.pdl.y
= ball.y
+ 20 * Sin(cy
)
If computer.pdl.y
- computer.pdl.h
< table.y
Then computer.pdl.y
= table.y
+ computer.pdl.h
If computer.pdl.y
+ computer.pdl.h
> table.y
+ table.h
Then computer.pdl.y
= table.y
+ table.h
- computer.pdl.h
ball.x
= ball.x
+ ball.s
* Cos(ball.a
): ball.y
= ball.y
+ ball.s
* Sin(ball.a
) ' update ball 'past the table edge? or paddle edge
If dist
(human.pdl.x
, human.pdl.y
, ball.x
, ball.y
) < ball.r
+ human.pdl.w
Then 'ball finds human paddle ball.a
= _Atan2(ball.y
- human.pdl.y
, ball.x
- human.pdl.x
) ball.x
= ball.x
+ 2 * ball.s
* Cos(ball.a
) ball.y
= ball.y
+ 2 * ball.s
* Sin(ball.a
)
ElseIf dist
(computer.pdl.x
, computer.pdl.y
, ball.x
, ball.y
) < ball.r
+ computer.pdl.w
Then 'ball finds AI paddle ball.a
= _Atan2(ball.y
- computer.pdl.y
, ball.x
- computer.pdl.x
) ball.x
= ball.x
+ 2 * ball.s
* Cos(ball.a
) ball.y
= ball.y
+ 2 * ball.s
* Sin(ball.a
)
ElseIf ball.x
<= computer.pdl.x
- 2 * computer.pdl.w
Then ' ball paste left edge inbounds = 0: human.pts = human.pts + 1
ElseIf ball.x
+ ball.r
>= table.x
+ table.w
Then ' ball paste right edge inbounds = 0: computer.pts = computer.pts + 1
ball.a = -ball.a
ball.y = table.y + ball.r
ElseIf ball.y
+ ball.r
>= table.y
+ table.h
Then ' bottom edge ball.a = -ball.a
ball.y = table.y + table.h - ball.r
drawGame
Dim d
, ra
, s$
, tstart
, my
, mx
computer.pdl.y = table.y + (table.h - computer.pdl.h) / 2 ' center paddles y
ball.x = table.x + .5 * table.w
computer.pdl.x = table.x + .1 * table.w
computer.pdl.x = table.x + computer.pdl.w
ball.y = table.y + table.h / 2
While Timer(.001) - tstart
< 2 ' <<<<<<<<<<<<<<<< This allows player to get es paddle alligned with mouse If my
> table.y
And my
<= table.y
+ table.h
Then ' update human paddle If mx
> table.x
+ .5 * table.w
And mx
< table.x
+ table.w
Then human.pdl.x = mx: human.pdl.y = my - human.pdl.h / 2
drawGame
s$ = "Get ready for random serve..."
_PrintString (table.x
+ (table.w
- 8 * Len(s$
)) / 2, table.y
+ table.h
/ 2 - 8), s$
_PrintString (table.x
+ 120, table.y
/ 2 - 4), computer.id
+ ":" + Str$(computer.pts
) s$
= human.id
+ ":" + Str$(human.pts
) Line (table.x
, table.y
)-Step(table.w
, table.h
), table.clr
, BF
'table Line (table.x
, table.y
)-Step(table.w
, table.h
), &HFFFFFFFF, B
'table Line (table.x
+ table.w
/ 2, table.y
)-Step(0, table.h
), &HFFFFFFFF fcirc computer.pdl.x, computer.pdl.y, computer.pdl.w, computer.pdl.clr
fcirc human.pdl.x, human.pdl.y, human.pdl.w, human.pdl.clr
fcirc ball.x, ball.y, ball.r, ball.clr
dist = ((x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5
Radius
= Abs(R
): RadiusError
= -Radius: X
= Radius: Y
= 0 Line (CX
- X
, CY
)-(CX
+ X
, CY
), C
, BF
RadiusError = RadiusError + Y * 2 + 1
Line (CX
- Y
, CY
- X
)-(CX
+ Y
, CY
- X
), C
, BF
Line (CX
- Y
, CY
+ X
)-(CX
+ Y
, CY
+ X
), C
, BF
X = X - 1
RadiusError = RadiusError - X * 2
Y = Y + 1
Line (CX
- X
, CY
- Y
)-(CX
+ X
, CY
- Y
), C
, BF
Line (CX
- X
, CY
+ Y
)-(CX
+ X
, CY
+ Y
), C
, BF