import GUI
setscreen ("graphics:700;max,position:center;center,nobuttonbar,offscreenonly")
var x, y, button, paddlex, paddley := 100
var cpux, cpuy := maxx div 2 % x and y value of paddle
var ballx, bally : real := 100 % x and y value of ball
var dx, dy : real := 0 % difference in x and y value for ball
var cpuxd, cpuyd := 1 % difference in x and y value for comp paddle
var maxspeed := 3 % maximum speed for comp paddle
var numofhits : int := 0
var maxballspeed : int := 3
var a, b : int := 0
var paddler, cpupaddler : int := 35
var ballr : int := 15
var px, py : int := 0
var hit : boolean := false
var ex := 0
ballx := maxx div 2
bally := maxy div 2
proc help
locate (1, 1)
put "You are trying to score in the black box (net) on the computer's side (top) and"
put "trying to prevent the computer from scoring in you net at the bottom."
put "Move the paddle around your side of the table with the mouse and hit the ball when"
put "it comes to you. You are playing a game against the computer up to 15. To make it"
put "more challenging, your paddle will get smaller each time you score and you will"
put "find that you are hitting the ball with less power."
end help
proc start
ex := 1
end start
proc Exit
ex := 2
end Exit
proc beginning
loop
var instructions := GUI.CreateButton (maxx div 2, maxy div 7 * 3, 0, "Help", help)
var start := GUI.CreateButton (maxx div 2, maxy div 3, 0, "Start", start)
var exitbut := GUI.CreateButton (maxx div 2, maxy div 4, 0, "Exit", Exit)
exit when GUI.ProcessEvent or ex > 0
View.Update
end loop
end beginning
function MathDistance (x1, y1, x2, y2 : real) : real
var dx : real := x1 - x2
var dy : real := y1 - y2
result sqrt (dx * dx + dy * dy)
end MathDistance
function MathDistancePointLine (px, py, x1, y1, x2, y2 : real) : real
var lineSize : real := MathDistance (x1, y1, x2, y2)
if lineSize = 0 then
result MathDistance (px, py, x1, y1)
end if
var u : real := ((px - x1) * (x2 - x1) +
(py - y1) * (y2 - y1)) / (lineSize * lineSize)
if u < 0.0 then
result MathDistance (px, py, x1, y1)
elsif u > 1.0 then
result MathDistance (px, py, x2, y2)
else
var ix : real := x1 + u * (x2 - x1)
var iy : real := y1 + u * (y2 - y1)
result MathDistance (px, py, ix, iy)
end if
end MathDistancePointLine
proc drawscreen
drawfillbox (0, 0, maxx, maxy, red) % Border
drawfillbox (20, 20, maxx - 20, maxy - 20, 1) %Background
drawfillbox (20, maxy div 2 - 150, maxx - 20, maxy div 2 - 155, red) % bottom line from center
drawfillbox (20, maxy div 2 + 150, maxx - 20, maxy div 2 + 155, red) % top line from center
drawfillbox (20, 20, maxx - 20, 30, grey) % bottom
drawfillbox (maxx div 2 - 70, 20, maxx div 2 + 70, 40, black) % net
drawfillbox (20, maxy - 20, maxx - 20, maxy - 30, grey) % top
drawfillbox (maxx div 2 - 70, maxy - 20, maxx div 2 + 70, maxy - 40, black) % net
drawfillbox (20, maxy div 2, maxx - 20, maxy div 2 + 5, grey) %center line
%Center Circle
drawfilloval (maxx div 2, maxy div 2, 80, 80, grey)
drawfilloval (maxx div 2, maxy div 2, 70, 70, 1)
drawfilloval (maxx div 2, maxy div 2, 10, 10, grey)
% Left top circle
drawfilloval (maxx div 2 - 200, maxy div 2 + 250, 70, 70, grey)
drawfilloval (maxx div 2 - 200, maxy div 2 + 250, 60, 60, 1)
% Right top circle
drawfilloval (maxx div 2 + 200, maxy div 2 + 250, 70, 70, grey)
drawfilloval (maxx div 2 + 200, maxy div 2 + 250, 60, 60, 1)
% Right bottom
drawfilloval (maxx div 2 + 200, maxy div 2 - 250, 70, 70, grey)
drawfilloval (maxx div 2 + 200, maxy div 2 - 250, 60, 60, 1)
% Left bottom
drawfilloval (maxx div 2 - 200, maxy div 2 - 250, 70, 70, grey)
drawfilloval (maxx div 2 - 200, maxy div 2 - 250, 60, 60, 1)
%ball
if ballx > 35 and ballx < maxx - 35 and bally > 35 and bally < maxy - 35 then
drawfilloval (round (ballx), round (bally), ballr, ballr, black)
end if
%paddle
drawfilloval (paddlex, paddley, paddler, paddler, 12)
drawfilloval (cpux, cpuy, cpupaddler, cpupaddler, 12)
locate (1, 1)
put "Computer score:", a
locate (1, 30)
put "Player score:", b
View.Update
end drawscreen
proc detection
%if the ball hits either end then make it go in the opposite direction
if ballx > maxx - 35 then
dx := abs (dx) * -1
elsif ballx < 35 then
dx := abs (dx)
end if
%if the ball hits either side then make it go in the opposite direction
if bally > maxy - 35 then
dy := abs (dy) * -1
elsif bally < 35 then
dy := abs (dy)
end if
%if the distance between the ball and the paddle is less than 50 (touching) then change direction
if MathDistancePointLine (ballx, bally, paddlex, paddley, px, py) < paddler + ballr then
if not hit then
dx := (ballx - px) / 5
dy := (bally - py) / 5
ballx := paddlex
bally := paddley
end if
hit := true
end if
if (sqrt ((ballx - cpux) ** 2 + (bally - cpuy) ** 2)) < cpupaddler + ballr then
dx := (ballx - cpux) / 5
dy := (bally - cpuy) / 5
numofhits := numofhits + 1
end if
end detection
beginning
loop
exit when ex = 2
paddler := 35 - b * 2
cpuy := maxy - 100
if bally < maxy div 2 then
numofhits := 0
end if
if numofhits > 3 then
cpux := maxx div 2
cpuy := maxy - 110
end if
if MathDistancePointLine (ballx, bally, paddlex, paddley, px, py) > paddler + ballr then
hit := false
end if
%USER PADDLE
mousewhere (x, y, button)
%if not hit then
if y + paddler > maxy div 2 then %if the mouse is above the center line, the paddle stays at center line
paddley := maxy div 2 - paddler
elsif y < paddler + 20 then
paddley := paddler + 20
else
paddley := y
end if
if x > maxx - paddler + 20 then %if the mouse is off the screen to the right then make the paddle be at the
paddlex := maxx - (paddler + 20) %furthest point horizontally on the screen (maxx)
elsif x < paddler + 20 then %**same as above but for the left side**
paddlex := paddler + 20
else
paddlex := x
end if
%end if
%BALL MOVEMENT
ballx += dx %make the ball move
bally += dy %""
%COMPUTER PADDLE
cpux := cpux + cpuxd % makes comp paddle move
if cpux > ballx then % checks if cpu x coordinate is greater than ball x coordinate
cpuxd := cpuxd - 1 % comp paddle x value moves negative direction
elsif
cpux < ballx then % checks if cpu x coordinate is less than ball x coordinate
cpuxd := cpuxd + 1 % comp paddle x value moves positive direction
end if
% Computer paddle speed doesn't go past assigned maximum speed
if cpuxd > maxspeed then %
cpuxd := maxspeed
elsif cpuxd < maxspeed * -1 then
cpuxd := maxspeed * -1
end if
% Keeps computer paddle inside the table
if cpux > maxx - 100 then
cpux := maxx - 100
elsif
cpux < 100 then
cpux := 100
end if
detection
if ballx > maxx div 2 - 70 and ballx < maxx div 2 + 70 and bally < 35 then
a := a + 1
ballx := maxx div 2
bally := maxy div 2
delay (500)
elsif ballx > maxx div 2 - 70 and ballx < maxx div 2 + 70 and bally > maxy - 35 then
b := b + 1
ballx := maxx div 2
bally := maxy div 2
delay (500)
end if
drawscreen
if a = 15 then
cls
put "Computer Wins. Please play again."
exit
elsif b = 15 then
cls
put "YOU WIN!"
exit
end if
if not hit then
px := paddlex
py := paddley
end if
end loop |