To run online, click here: RUN ONLINE
Listing:
Listing:
Code:
' PROGRAM Pong
' (c) 2023 by Zarsoft
' Written by Ze Oliveira
' Demo for the Pascalated language
' Pascalated Sinclair BASIC (c) 1987 by ZarSoft
' Pascalated BASIC Converter (c) 2021 by Zarsoft
' Pascalated Boriel (c) 2023 by ZarSoft
' ZX BASIC Compiler (c) 2008 by Boriel
#include <input.bas> ' number = VAL INPUT(12)
#include <screen.bas> ' SCREEN$ function
#include <attr.bas> ' ATTR function
'--- Pascalated Boriel ---
'#define CONST CONST
#define VAR DIM
#define INTEGER LONG
#define REAL FLOAT
#define CHAR STRING
'#define STRING STRING
#define BOOLEAN UBYTE
#define TYPE AS
'#define WHILE WHILE
#define REPEAT DO
#define UNTIL LOOP UNTIL
#define PROCEDURE SUB
#define PROGRAM REM
CONST TRUE TYPE BOOLEAN = 1
CONST FALSE TYPE BOOLEAN = 0
' CONSTant declarations
CONST Period TYPE INTEGER = 8
' VAR - Global variables
VAR ScorePlayer TYPE INTEGER = 0
VAR ScoreComputer TYPE INTEGER = 0
VAR PlayerFirst TYPE BOOLEAN = TRUE
VAR Handicap TYPE INTEGER = 14
VAR Goal TYPE BOOLEAN
VAR ComputerPos TYPE INTEGER = 15
VAR PlayerPos TYPE INTEGER = 15
VAR BallLin TYPE INTEGER = 19-1
VAR BallCol TYPE INTEGER = PlayerPos
VAR DL TYPE INTEGER = -1
VAR DC TYPE INTEGER = -1 + INT (RND*3)
VAR Clock,Clock0 TYPE INTEGER
PROCEDURE GetReady
LET Goal = FALSE
IF BallLin = 2 THEN LET ScorePlayer = ScorePlayer+1
IF BallLin = 19 THEN LET ScoreComputer = ScoreComputer+1
PRINT AT 0,7;ScoreComputer
PRINT AT 21,7;ScorePlayer
IF BallLin = 2
LET PlayerFirst = TRUE
ELSEIF BallLin = 19
LET PlayerFirst = FALSE
ENDIF
IF PlayerFirst
LET BallLin = 19-1
LET BallCol = PlayerPos
LET DL = -1
LET DC = -1 + INT (RND*3)
ELSE
LET BallLin = 2+1
LET BallCol = ComputerPos
LET DL = 1
LET DC = -1 + INT (RND*3)
ENDIF
PRINT AT 19,PlayerPos-2;INK 4;" \''\''\'' ";
PRINT AT 2,ComputerPos-2;INK 3;" \..\..\.. ";
PRINT AT BallLin,BallCol;"\A"
IF ScorePlayer-ScoreComputer >= 2 THEN IF Handicap < 15 THEN LET Handicap = 15
IF ScorePlayer-ScoreComputer >= 3 THEN IF Handicap < 16 THEN LET Handicap = 16
IF ScorePlayer-ScoreComputer >= 4 THEN IF Handicap < 17 THEN LET Handicap = 17
IF ScorePlayer-ScoreComputer >= 5 THEN IF Handicap < 18 THEN LET Handicap = 18
IF ScorePlayer-ScoreComputer >= 6 THEN IF Handicap < 19 THEN LET Handicap = 19
IF ScorePlayer-ScoreComputer >= 7 THEN IF Handicap < 20 THEN LET Handicap = 20
END PROCEDURE
PROCEDURE DrawField
BORDER 0: PAPER 0: INK 7: CLS
LET Goal = FALSE
FOR N=1 TO 30: PRINT AT 1,N;"\..";AT 20,N;"\''";: NEXT N
FOR N=2 TO 19: PRINT AT N,0;"\ :";AT N,31;"\: ";: NEXT N
PRINT AT 1,0;"\ .";
PRINT AT 1,31;"\. ";
PRINT AT 20,0;"\ '";
PRINT AT 20,31;"\' ";
PRINT AT 0,25;INK 6;"PONG"
PRINT AT 0,0;"Score: ";0;
PRINT AT 21,0;"Score: ";0;
PRINT AT 19,PlayerPos-2;INK 4;" \''\''\'' ";
PRINT AT 21,11;INK 1;"Pascalated BASIC demo"
END PROCEDURE
FUNCTION GetKey$ TYPE CHAR
VAR result$ TYPE CHAR = ""
VAR key$ TYPE CHAR
VAR elapsed TYPE INTEGER
REPEAT
key$ = INKEY$
IF key$ = "5" OR key$ = "8" THEN LET result$ = key$
LET Clock = PEEK 23672
LET elapsed = Clock-Clock0: IF elapsed<0 THEN LET elapsed = elapsed+256
UNTIL elapsed >= Period
LET Clock0 = Clock0 + Period: IF Clock0>255 THEN LET Clock0 = Clock0-256
RETURN result$
END FUNCTION
PROCEDURE WaitForUser
REPEAT
PAUSE 1
LET k$ = INKEY$
UNTIL k$ = ""
PAUSE 0
BEEP .05,20
LET k$ = INKEY$
REM this give advantage; canceled
REM IF k$ = "5" THEN LET DC = -1
REM IF k$ = "8" THEN LET DC = 1
END PROCEDURE
'----------------- game -------------------
PROCEDURE ProcessPlayer
LET k$ = GetKey$
IF k$="5" THEN IF PlayerPos>3 THEN LET PlayerPos = PlayerPos-1
IF k$="8" THEN IF PlayerPos<28 THEN LET PlayerPos = PlayerPos+1
PRINT AT 19,PlayerPos-2;INK 4;" \''\''\'' ";
END PROCEDURE
PROCEDURE ProcessComputer
IF BallLin < Handicap THEN LET ComputerPos = ComputerPos + SGN(BallCol-ComputerPos)
IF ComputerPos < 3 THEN LET ComputerPos = 3
IF ComputerPos > 28 THEN LET ComputerPos = 28
PRINT AT 2,ComputerPos-2;INK 3;" \..\..\.. ";
END PROCEDURE
PROCEDURE ProcessBall
PRINT AT BallLin,BallCol;INK 6;" "
LET BallCol = BallCol + DC
LET BallLin = BallLin + DL
PRINT AT BallLin,BallCol;INK 6;"\A"
IF BallCol = 1 OR BallCol = 30 THEN LET DC = -DC: BEEP .05,10
END PROCEDURE
PROCEDURE TestGameStatus
IF BallLin = 2 OR BallLin = 19 THEN LET Goal = TRUE: BEEP .1,0
IF BallLin = 3 THEN IF ABS(BallCol-ComputerPos)<=1 THEN LET DL = -DL: LET DC = SGN(RND*10-5): BEEP .05,20
IF BallLin = 18 THEN IF ABS(BallCol-PlayerPos)<=1 THEN LET DL = -DL: BEEP .05,20
END PROCEDURE
'----------------- end game -------------------
PROCEDURE InitVariables
POKE 23609,50
LET Goal = FALSE
LET ComputerPos = 15
LET PlayerPos = 15
LET BallLin = 19-1
LET BallCol = PlayerPos
LET DL = -1
LET DC = -1 + INT (RND*3)
END PROCEDURE
PROCEDURE InitGraphics
REM \A
POKE USR "A"+0,BIN 00111100
POKE USR "A"+1,BIN 01111110
POKE USR "A"+2,BIN 11111111
POKE USR "A"+3,BIN 11111111
POKE USR "A"+4,BIN 11111111
POKE USR "A"+5,BIN 11111111
POKE USR "A"+6,BIN 01111110
POKE USR "A"+7,BIN 00111100
END PROCEDURE
PROCEDURE Game
LET Clock0 = PEEK 23672
REPEAT
ProcessPlayer
ProcessComputer
ProcessBall
TestGameStatus
UNTIL Goal
END PROCEDURE
PROCEDURE MainRoutine
InitGraphics
InitVariables
DrawField
REPEAT
GetReady
IF PlayerFirst THEN WaitForUser
Game
UNTIL FALSE
END PROCEDURE
PROGRAM Pong
MainRoutine
' last 2 lines are going to be deleted
PRINT AT 23,0;
END PROGRAM