REMSTART
- Connect
-
REMEND
`App Settings
SET WINDOW ON:SET WINDOW LAYOUT 1,Pong,1:SET WINDOW POSITION SCREEN WIDTH()/2,SCREEN HEIGHT()/2
SET CURSOR SCREEN WIDTH()/2,SCREEN HEIGHT()/2:PRINT "pong.":WAIT 5000:CLS
RANDOMIZE TIMER():SET TEXT SIZE 35:INK RGB(255,255,255),RGB(255,255,255):SET TEXT FONT "Verdana":LOAD DLL "Kernel32",3:DISABLE SYSTEMKEYS:SET TEXT TO BOLD
INPUT "Host or Client(h/c): ",status$
INPUT "IP: ",IP$
PRINT "- connecting -"
SYNC ON:SYNC RATE 0
status$ = UPPER$(status$)
IF status$ = "H"
connected = THost(IP$,2,"Host")
ELSE
connected = TJoin(IP$,"Client")
ENDIF
IF connected
PRINT "connection established"
WAIT 2000
CLS
ELSE
PRINT "connection failed"
PRINT "ending program"
WAIT 4000
END
ENDIF
`Player Setup
TYPE playerStat
paddleY#
paddleHeight#
paddleWidth#
paddleSpeed#
paddleDist#
score
direction
upKeystate
downKeystate
ENDTYPE
DIM players(2) AS playerStat
FOR p = 1 TO 2
players(p).paddleHeight# = 50
players(p).paddleWidth# = 10
players(p).paddleSpeed# = .5
players(p).paddleY# = (SCREEN HEIGHT()/2)-(players(p).paddleHeight#/2)
NEXT x
players(1).paddleDist# = 10
players(1).upKeystate = 17
players(1).downKeystate = 31
players(2).paddleDist# = SCREEN WIDTH()-20
players(2).upKeystate = 200
players(2).downKeystate = 208
`Ball Setup
TYPE ballStat
hit
x#,y#
xSpeed#,ySpeed#
acceleration#
wallFreq
wallDur
paddleFreq
paddleDur
ENDTYPE
GLOBAL ball AS ballStat
ball.x# = SCREEN WIDTH()/2
ball.y# = SCREEN HEIGHT()/2
ball.wallFreq = 300
ball.wallDur = 30
ball.paddleFreq = 100
ball.paddleDur = 30
ball.acceleration# = 1.05
spawnBall()
`Line Setup
#CONSTANT lineTotal = 40
#CONSTANT lineGap = 2
`Other junk
GLOBAL width
GLOBAL height
GLOBAL maxScore
width = SCREEN WIDTH()
height = SCREEN HEIGHT()
maxScore = 10
`Main Loop
WHILE NOT ESCAPEKEY()
CLS
IF gameStarted
DEC players(1).paddleY#,(KEYSTATE(players(1).upKeystate)-KEYSTATE(players(1).downKeystate))*players(1).paddleSpeed#
IF TConnected()
TPutFloat (KEYSTATE(players(1).upKeystate)-KEYSTATE(players(1).downKeystate))
IF status$ = "H"
TPutFloat SCREEN WIDTH()-ball.x#
TPutFloat ball.y#
TPutFloat wallHit
TPutFloat paddleHit
TPutFloat players(1).score
TPutFloat players(2).score
ENDIF
TSendAll
REPEAT
msg = TGetMessage()
IF msg
DEC players(2).paddleY#,TGetFloat()*players(2).paddleSpeed#
IF status$ = "C"
ball.x# = TGetFloat()
ball.y# = TGetFloat()
wallHit = TGetFloat()
paddleHit = TGetFloat()
players(1).score = TGetFloat()
players(2).score = TGetFloat()
IF wallHit THEN CALL DLL 3,"Beep",ball.wallFreq,ball.wallDur
IF paddleHit THEN CALL DLL 3,"Beep",ball.paddleFreq,ball.paddleDur
ENDIF
ENDIF
UNTIL NOT msg
ENDIF
`Cycle through both players, handling movement/paddle display
FOR p = 1 TO 2
players(p).direction = KEYSTATE(players(p).upKeystate)-KEYSTATE(players(p).downKeystate)
IF players(p).paddleY# > HEIGHT-players(p).paddleHeight# THEN players(p).paddleY# = HEIGHT-players(p).paddleHeight#
IF players(p).paddleY# < 0 THEN players(p).paddleY# = 0
BOX players(p).paddleDist#,players(p).paddleY#,players(p).paddleDist#+players(p).paddleWidth#,players(p).paddleY#+players(p).paddleHeight#
NEXT p
IF status$ = "H"
`Move the ball
INC ball.x#,ball.xSpeed#
INC ball.y#,ball.ySpeed#
`Check if the ball is scored
IF ball.x# < 0 THEN ball.x# = 0:ball.xSpeed# = ABS(ball.xSpeed#):ball.x# = WIDTH/2:ball.y# = WIDTH/2:INC players(2).score,1:spawnBall()
IF ball.x# > WIDTH THEN ball.x# = WIDTH:ball.xSpeed# = -ball.xSpeed#:ball.x# = WIDTH/2:ball.y# = WIDTH/2:INC players(1).score,1:spawnBall()
`Check if the ball hits a top or bottom wall
IF ball.y# < 0 THEN ball.y# = 0:ball.ySpeed# = ABS(ball.ySpeed#):CALL DLL 3,"Beep",ball.wallFreq,ball.wallDur
IF ball.y# > HEIGHT THEN ball.y# = HEIGHT:ball.ySpeed# = -ball.ySpeed#:CALL DLL 3,"Beep",ball.wallFreq,ball.wallDur
`Check if the ball hits a paddle
FOR p = 1 TO 2
IF ball.x# > players(p).paddleDist# AND ball.x# < players(p).paddleDist#+players(p).paddleWidth#
IF ball.y# > players(p).paddleY# AND ball.y# < players(p).paddleY# + players(p).paddleHeight#
ball.xSpeed# = (-ball.xSpeed#)* ball.acceleration#
ball.ySpeed# = (ball.ySpeed# * ball.acceleration#)
IF players(p).direction
ball.ySpeed# = players(p).direction*(-(ABS(ball.ySpeed#)))
ENDIF
IF p = 1
ball.x# = players(p).paddleDist#+players(p).paddleWidth#
ELSE
ball.x# = players(p).paddleDist#-players(p).paddleWidth#
ENDIF
CALL DLL 3,"Beep",ball.paddleFreq,ball.paddleDur
ENDIF
ENDIF
NEXT p
ENDIF
FOR p = 1 TO 2
`Check if a player wins
IF players(p).score >= maxScore
CLS
PRINT "Player "+STR$(p)+" wins."
WAIT 3000
PRINT "...yay."
WAIT 3000
END
ENDIF
NEXT p
`Display the ball
BOX ball.x#-5,ball.y#-5,ball.x#+5,ball.y#+5
`Display the grid lines
FOR x = 1 TO lineTotal STEP lineGap
lineLength = HEIGHT/lineTotal
x1# = WIDTH/2
y1# = lineLength*x
x2# = x1#
y2# = y1# + lineLength
BOX x1#-2,y1#,x2#+2,y2#
NEXT x
`Display the player scores
TEXT 100,0,"P1: "+STR$(players(1).score)
TEXT WIDTH-100,0,"P2: "+STR$(players(2).score)
ELSE
IF status$ = "H"
spawnBall()
TEXT 0,0,"Press the spacekey to start the game"
IF SPACEKEY() THEN gameStarted = 1:TPutFloat gameStarted:TSendAll
ELSE
TEXT 0,0,"Waiting for the host to start the game"
msg = TGetMessage()
IF msg THEN gameStarted = TGetFloat()
ENDIF
ENDIF
`Refresh that there screen mkay
TSYNC
SYNC
`Repeat until the user presses escape
ENDWHILE
`Wipe everything
END
`Ball spawning function
FUNCTION spawnBall()
xDir = RND(1)
IF xDir
ball.xSpeed# = .3
ELSE
ball.xSpeed# = -.3
ENDIF
ball.ySpeed# = -.3+(.1*(RND(10)))
IF ball.xSpeed# = 0 THEN ball.xSpeed# = .1
IF ball.ySpeed# = 0 THEN ball.ySpeed# = .1
ENDFUNCTION