const chatPort : int := 5055
var choice : int
var paddley1, paddley2, ballx, bally : int
var left, right, up, down, up2, down2, ballin : boolean := false
setscreen ("graphics:651;551, nobuttonbar, offscreenonly")
var scorep1, scorep2 : int
var randomness : int
loop
put "Enter 1 to run server"
put "Enter 2 to connect to server"
put "Choice: " ..
View.Update
get choice
exit when choice = 1 or choice = 2
end loop
var netStream : int
var netAddress : string
if choice = 1 then
put "This machine is '", Net.LocalName, "' at net addresss ", Net.LocalAddress
put "Waiting for a connection... "
View.Update
netStream := Net.WaitForConnection (chatPort, netAddress)
else
put "Enter the address to connect to: " ..
get netAddress
netStream := Net.OpenConnection (netAddress, chatPort)
if netStream <= 0 then
put "Unable to connect to ", netAddress
return
end if
end if
procedure drawboard
cls
locate (1, 1)
put " ", scorep1, " ", scorep2
drawfillbox (324, 0, 326, maxy, white)
drawfillbox (10, paddley1, 15, paddley1 + 100, white)
drawfillbox (maxx - 10, paddley2, maxx - 15, paddley2 + 100, white)
drawfilloval (ballx, bally, 5, 5, white)
View.Update
end drawboard
procedure balldirection
randint (randomness, 1, 4)
if randomness = 1 then
up := true
up2 := false
left := true
down := false
down2 := false
right := false
end if
if randomness = 2 then
up := true
up2 := false
left := false
down := false
down2 := false
right := true
end if
if randomness = 3 then
up := false
up2 := false
down2 := false
left := true
down := true
right := false
end if
if randomness = 4 then
up := false
up2 := false
down2 := false
left := false
down := true
left := true
end if
end balldirection
procedure startupball
if ballin = false then
balldirection
ballx := 325
randint (bally, 5, maxy - 5)
ballin := true
end if
end startupball
procedure boundaries
if paddley1 + 100 > maxy then
paddley1 := maxy - 100
end if
if paddley2 + 100 > maxy then
paddley2 := maxy - 100
end if
if paddley1 < 0 then
paddley1 := 0
end if
if paddley2 < 0 then
paddley2 := 0
end if
if ballx - 5 < 10 and bally >= paddley1 and bally <= paddley1 + 25 then
left := false
up2 := false
down2 := true
right := true
down := false
up := false
end if
if ballx - 5 < 10 and bally >= paddley1 + 26 and bally <= paddley1 + 50 then
left := false
up2 := false
down2 := false
right := true
down := true
up := false
end if
if ballx - 5 < 10 and bally >= paddley1 + 51 and bally <= paddley1 + 75 then
left := false
up2 := false
down2 := false
right := true
down := false
up := true
end if
if ballx - 5 < 10 and bally >= paddley1 + 76 and bally <= paddley1 + 100 then
left := false
up2 := true
down2 := false
right := true
down := false
up := false
end if
if ballx + 5 > maxx - 10 and bally >= paddley2 and bally <= paddley2 + 25 then
left := true
right := false
up2 := false
down2 := true
down := false
up := false
end if
if ballx + 5 > maxx - 10 and bally >= paddley2 + 26 and bally <= paddley2 + 50 then
left := true
right := false
up2 := false
down2 := false
down := true
up := false
end if
if ballx + 5 > maxx - 10 and bally >= paddley2 + 51 and bally <= paddley2 + 75 then
left := true
right := false
up2 := false
down2 := false
down := false
up := true
end if
if ballx + 5 > maxx - 10 and bally >= paddley2 + 76 and bally <= paddley2 + 100 then
left := true
right := false
up2 := true
down2 := false
down := false
up := false
end if
if bally > maxy and up2 = true then
up2 := false
down2 := true
end if
if bally > maxy and up = true then
up := false
down := true
end if
if bally < 0 and down2 = true then
up2 := true
down2 := false
end if
if bally < 0 and down = true then
up := true
down := false
end if
if ballx < 0 then
scorep2 := scorep2 + 1
ballin := false
startupball
end if
if ballx > maxx then
scorep1 := scorep1 + 1
ballin := false
startupball
end if
end boundaries
procedure ballmove
if left = true then
ballx := ballx - 2
end if
if right = true then
ballx := ballx + 2
end if
if up = true then
bally := bally + 2
end if
if down = true then
bally := bally - 2
end if
if up2 = true then
bally := bally + 4
end if
if down2 = true then
bally := bally - 4
end if
if left = right then
randint (randomness, 1, 2)
if randomness = 1 then
right := false
else
left := false
end if
end if
if up = down then
randint (randomness, 1, 2)
if randomness = 1 then
up := false
else
down := false
end if
end if
end ballmove
Text.Cls
var ch : char
View.Set ("noecho")
startupball
drawboard
loop
% If there is a character waiting ..
if hasch then
% Read the character from the keyboard.
ch := getchar
if choice = 1 then
if ch = 'w' then
paddley1 := paddley1 + 2
end if
if ch = 's' then
paddley1 := paddley1 - 2
end if
end if
if choice = 2 then
if ch = 'i' then
paddley2 := paddley2 + 2
end if
if ch = 'k' then
paddley2 := paddley2 - 2
end if
end if
exit when Error.Last not= 0
end if
if Net.CharAvailable (netStream) then
% Read the character from the net stream
get : netStream, ch
if choice = 2 then
if ch = 'w' then
paddley1 := paddley1 + 2
end if
if ch = 's' then
paddley1 := paddley1 - 2
end if
end if
if choice = 1 then
if ch = 'i' then
paddley2 := paddley2 + 2
end if
if ch = 'k' then
paddley2 := paddley2 - 2
end if
end if
end if
ballmove
drawboard
boundaries
delay (5)
exit when scorep1 = 15
exit when scorep2 = 15
exit when ch = '\'
exit when Error.Last not= 0
end loop
Net.CloseConnection (netStream)
|