ZX81 SNAKE GAME

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Coggie
Posts: 14
Joined: Thu Oct 12, 2017 12:11 am
Location: Orkney Islands
Contact:

ZX81 SNAKE GAME

Post by Coggie » Thu Oct 12, 2017 11:35 am

Hi Dudes..
I've been making a snake eat numbers game...haven't programmed in ZX81 BASIC before but tried back in the day on the Speccy and wasn't too great at it then either...
I'm sure you experts can tell me how I can make this program more efficient, using different methods maybe...I knew before I started this project it would be terribly slow.. I started it before realizing there was no SCREEN$ Command..was hoping to avoid large Arrays.
The Game works if rather slowly...can anyone improve my program without using Machine Code....I'm sure it can be.

I'm using eightyone 1.0a & ZX81 with 16k RAM
Cheers.
Attachments
MYSNAKEP.P
(3.21 KiB) Downloaded 13 times

User avatar
XavSnap
Posts: 462
Joined: Sat May 10, 2008 3:23 pm

Re: ZX81 SNAKE GAME

Post by XavSnap » Thu Oct 12, 2017 3:01 pm

Well done Coggie !

I "tryed" to clean the Basic code ... and add personal values..
snake.jpg
Snake Picture [Coggie game]
snake.jpg (9.58 KiB) Viewed 322 times
It was a good code, but ... 8-)

Code: Select all

  
   250 LET K=CODE INKEY$
   270 IF K=115 THEN GOSUB 500
   271 IF K=114 THEN GOSUB 510
   272 IF K=113 THEN GOSUB 520
   273 IF K=112 THEN GOSUB 530
270 IF K>0 THEN GOSUB 500+10*(K-112)
(target lines renamed to point to k=112 >>> 500 ; K=113 >>> 510...)

The F flag is used one time! (deleted)

Code: Select all

   214 IF RND>.9 THEN LET F=1
   215 IF F=1 THEN GOSUB 600
=== 214 IF RND>.9 THEN GOSUB 600

Code: Select all

   405 LET TX=S(ST,1)
   406 LET TY=S(ST,2)
   407 LET G(TX,TY)=0
>>> LET G(S(ST,1),S(ST,2))=0 ... save memory and time...

Code: Select all

     
     5 DIM A$(1)
     6 DIM B$(1)
Fixe the string length to 1 character... don't know if it's better... regard variable string length.

Code: Select all

   700 PRINT AT 9,10;"ya«silly«cant"
   705 PRINT AT 11,4;"you«fell«in«the«dam«ditch"
:? Can be merged to free memory room...
700 PRINT AT 9,10;"ya«silly«cant";AT 11,4;"you«fell«in«the«dam«ditch";...
SNAKE2.P
Snake P file [Coggie game]
(3.13 KiB) Downloaded 9 times

Code: Select all

 
     1 LET HS=0
     2 GOSUB 800
     3 CLS 
     4 PRINT AT 1,1;"WORKING..."
     5 DIM A$(1)
     6 DIM B$(1)
     7 DIM C$(1)
     8 LET M=555
    10 DIM S(M,2)
    11 DIM G(21,31)
    14 LET SH=1
    15 LET ST=1
    18 LET SL=3
    19 LET X=11
    20 LET Y=16
    30 LET XD=0
    40 LET YD=1
    47 LET S=0
    50 LET A$="*"
    60 LET B$="x"
    70 LET C$="O"
   110 FOR I=1 TO 31
   120 PRINT AT 1,I;B$;AT 21,32-I;B$
   125 LET G(1,I)=10
   126 LET G(21,I)=10
   140 NEXT I
   150 FOR I=2 TO 20
   160 PRINT AT I,1;B$;AT 22-I,31;B$
   165 LET G(I,1)=10
   166 LET G(I,31)=10
   170 NEXT I
   190 PRINT AT 0,1;"SCORE=";S;AT 0,20;"HI SCORE=";HS
   200 PRINT AT X,Y;C$
   201 LET V=G(X,Y)
   203 IF V<>0 THEN GOSUB 550
   208 LET G(X,Y)=11
   210 LET S(SH,1)=X
   211 LET S(SH,2)=Y
   214 IF RND>.9 THEN GOSUB 600
   250 LET K=CODE INKEY$
   270 IF K>0 THEN GOSUB 500+10*(K-112)
   280 LET X=X+XD
   281 LET Y=Y+YD
   290 LET SH=SH+1
   295 IF SH>M THEN LET SH=1
   300 IF SL>0 THEN LET SL=SL-1
   310 IF SL=0 THEN GOSUB 400
   399 GOTO 200
   400 PRINT AT S(ST,1),S(ST,2);" "
   407 LET G(S(ST,1),S(ST,2))=0
   410 LET ST=ST+1
   420 IF ST>M THEN LET ST=1
   430 RETURN 
   500 IF XD=1 THEN RETURN 
   505 LET XD=-1
   506 LET YD=0
   507 RETURN
   510 IF XD=-1 THEN RETURN 
   515 LET XD=1
   516 LET YD=0
   517 RETURN 
   520 IF YD=1 THEN RETURN 
   525 LET YD=-1
   526 LET XD=0
   527 RETURN 
   530 IF YD=-1 THEN RETURN 
   535 LET YD=1
   536 LET XD=0
   537 RETURN 
   550 IF V=10 THEN GOTO 700
   555 IF V=11 THEN GOTO 720
   570 LET SL=SL+V+1
   572 LET S=S+V
   573 IF S>HS THEN LET HS=S
   578 PRINT AT 0,7;S
   599 RETURN 
   600 LET A=INT (RND*19)+2
   605 LET B=INT (RND*29)+2
   607 IF G(A,B)<>0 THEN GOTO 630
   610 LET N=INT (RND*9)+1
   620 PRINT AT A,B;CHR$(N+156)
   625 LET G(A,B)=N
   649 RETURN 
   700 PRINT AT 9,10;"ya«silly«cant"
   705 PRINT AT 11,4;"you«fell«in«the«dam«ditch"
   710 GOTO 750
   720 PRINT AT 9,9;"ya«daft«basket"
   725 PRINT AT 11,4;"you«ate«your«fecking«tail"
   750 PRINT AT 13,8;"play«again«‹y“nŒŠ"
   755 LET K$=INKEY$
   757 IF K$="Y" THEN GOTO 3
   758 IF K$="N" THEN GOTO 990
   759 GOTO 755
   777 GOTO 3
   800 PRINT AT 2,8;"hello«and«welcome"
   805 PRINT AT 4,15;"to"
   810 PRINT AT 6,1;"trakkie«the«amazing«wobbly«worm"
   815 PRINT AT 10,6;"USE ARROW KEYS TO MOVE"
   820 PRINT AT 20,7;"PRESS A KEY TO PLAY"
   830 IF INKEY$="" THEN GOTO 830
   835 RETURN 
   990 PRINT AT 15,2;"please«your«fecking«self«then"
   991 STOP 
   998  REM
  
:D
Have fun !

Coggie
Posts: 14
Joined: Thu Oct 12, 2017 12:11 am
Location: Orkney Islands
Contact:

Re: ZX81 SNAKE GAME

Post by Coggie » Thu Oct 12, 2017 5:49 pm

WOW! Thanks XavSnap...thats great stuff...

Clever KeysSub's change (I should've notice a pattern when working out the Arrow Key Codes ha)

and the other tweaks make the game run much smoother & faster

Not only is this nice nostalgia...but the game is even Addictive!....hee.

Cheerz Man :)

(P.S. My Friends name is Tacey (Trakkie) she has been known 2 take a drink & a Tumble sometimes..(hence the game name) lol)

But I drink lyk a Fish 2 ha... :)

User avatar
siggi
Posts: 773
Joined: Thu May 08, 2008 8:30 am
Location: Dauernheim, Germany
Contact:

Re: ZX81 SNAKE GAME

Post by siggi » Fri Oct 13, 2017 8:48 am

The main loop seems to start at line 200?

You could speed up your game, if you move all the stuff for initializing to the "end" (and put a GOSUB "end" at line 1).
If the main loop is next to the beginning of the program, a GOTO "mainloop" takes less time to search that line (200).

Regards
Siggi
My ZX81 web-server: online since 2007
http://zx81-siggi.endoftheinternet.org/index.html

Coggie
Posts: 14
Joined: Thu Oct 12, 2017 12:11 am
Location: Orkney Islands
Contact:

Re: ZX81 SNAKE GAME

Post by Coggie » Fri Oct 13, 2017 4:14 pm

OK siggi & Xav...I have made the modifications...

Also Xav...I had to change
IF K>0 THEN.....
to
IF K>36 THEN....

Because CODE INKEY$ sometimes returns 33,34,35,36 (keys 5,6,7,8) when I use Arrow Keys on my keyboard.

Is this an Emulator(eightyone 1.0a) problem...or does a REAL ZX81 keyboard sometime return these scan codes(Un-SHIFTED 5,6,7,8) ??
Attachments
SNAKE3.P
(3.08 KiB) Downloaded 6 times

Coggie
Posts: 14
Joined: Thu Oct 12, 2017 12:11 am
Location: Orkney Islands
Contact:

Re: ZX81 SNAKE GAME

Post by Coggie » Fri Oct 13, 2017 5:40 pm

I have now change All variables to single letter.,,

Coggie :)
Attachments
SNAKE4.P
(3.03 KiB) Downloaded 7 times

User avatar
XavSnap
Posts: 462
Joined: Sat May 10, 2008 3:23 pm

Re: ZX81 SNAKE GAME

Post by XavSnap » Fri Oct 13, 2017 10:12 pm

:D
Something wrong. (regard a professional purpose!)
When you retrieve a string value "B$" or "C$" the ROM had to scan the VARs memory room.
In the game:

Code: Select all

  1060 LET B$="x"
  1070 LET C$="O"
...
  1110 PRINT AT 1,I;B$;AT 21,32-I;B$
  1160 PRINT AT 22-I,1;B$;AT I,31;B$
...
  200 PRINT AT X,Y;C$
1110 PRINT AT 1,I;B$;AT 21,32-I;"x"
1160 PRINT AT 22-I,1;B$;AT I,31;"x"
200 PRINT AT X,Y;"O"
Don't scan the Vars, and seed up the display!

Another optimization:
Put the most used variables in the first place in the Vars memory.
X,Y
G(X,Y), S(H,1) ...
to speedup variable access in the VARS!
We use CLEAR to reset the variable table... but the Z value (high-score) is reset.
We had to store the Z value in the unused printer buffer at 16444 (PRbuff)...and retrieve it after the CLEAR command.

New release:
SNAKE5.P
(3.11 KiB) Downloaded 6 times

User avatar
XavSnap
Posts: 462
Joined: Sat May 10, 2008 3:23 pm

Re: ZX81 SNAKE GAME

Post by XavSnap » Fri Oct 13, 2017 11:37 pm

Note:

Code: Select all

1 LET L=125
2 GOTO 10
5 LET L=0
10 IF L THEN PRINT "TRUE"
20 IF NOT L THEN PRINT "FALSE"
300 IF L>0 THEN LET L=L-1
310 IF L=0 THEN GOSUB 400
=============================
300 IF L THEN LET L=L-1
310 IF NOT L THEN GOSUB 400

User avatar
XavSnap
Posts: 462
Joined: Sat May 10, 2008 3:23 pm

Re: ZX81 SNAKE GAME

Post by XavSnap » Sat Oct 14, 2017 12:35 am

Note 2:
To save memory...

Code: Select all

550 IF V=10 OR V=11 THEN GOTO 700
(...)
700 PRINT AT 9,10;"ya«silly«cant" AND V=10;AT 11,4;"you«fell«in«the«dam«ditch" AND V=10;AT 9,9;"ya«daft«basket" AND V=11;AT 11,4;"you«ate«your«feckin«tail" AND V=11;AT 13,8;"play«again«‹y“nŒŠ"

Code: Select all

   203 IF NOT V THEN GOTO 208
   204 IF V=10 OR V=11 THEN GOTO 700
   205 LET L=L+V+1
   206 LET S=S+V
   207 PRINT AT 0,7;S
   208 LET G(X,Y)=11
GOSUB deleted.
Was : 203 IF V<>0 THEN GOSUB...
SNAKE6.P
(11.81 KiB) Downloaded 8 times

User avatar
XavSnap
Posts: 462
Joined: Sat May 10, 2008 3:23 pm

Re: ZX81 SNAKE GAME

Post by XavSnap » Sat Oct 14, 2017 12:54 am

You can optimize the seed using the D_FILE relocation!
Just point it to a string array G$ and change the values as strings.
The LET function display values and you don't need PRINT command!
:shock:
G$ array DIM G$(26,33):
$76,$76
G$(2,0-32),$76
G$(3,0-32),$76
...
LET G$(2,10)="O" display it to the screen.
8-)

Have a look to this demo:
DEMO.P
(2.09 KiB) Downloaded 5 times

Code: Select all

     1 DIM A$(18,16)
     2 FOR A=1 TO 16
     3 LET A$(A)=CHR$ (118)
     4 LET A$(17,A)=CHR$ (118)
     6 NEXT A
     7 LET A$(18)=CHR$ (118)+CHR$ (118)
     8 POKE 16418,10
     9 LET DES=8+PEEK (16400)+256*PEEK (16401)
    10  REM SAVE DFILE
    13 POKE 16507,PEEK (16396)
    14 POKE 16508,PEEK (16397)
    16 POKE 16396,DES-INT (DES/256)*256
    17 POKE 16397,INT (DES/256)
    18 POKE 16444,PEEK (16396)
    19 POKE 16445,PEEK (16397)
    20 FOR A=1 TO 15
    21 LET A$(A,A+1)=CHR$ (A+156)
    22 NEXT A
   30  REM DEMO
    31 FOR A=1 TO 15
    32 LET A$(A,A+1)=CHR$ (A+28)
    33 NEXT A
    34 FOR A=1 TO 15
    35 LET A$(A,A+1)=CHR$ (A+156)
    36 NEXT A
    45 FOR B=1 TO 8
    50 FOR A=1 TO 15
    55 LET A$(A)=CHR$ (118)+A$(A,3 TO )+A$(A,2)
    56 NEXT A
    57 NEXT B
    59 FOR A=1 TO 14
    60 POKE 16396+SIN PI+SIN PI+SIN PI,PEEK (16507)
    61 POKE 16397,PEEK (16508)
    62 PRINT ".........HELLO WORLD........."
    63 POKE 16396+SIN PI+SIN PI+SIN PI,PEEK (16444)
    64 POKE 16397,PEEK (16445)
    65 NEXT A
    71 FOR B=1 TO 15 STEP 3
    72 LET A$(B,2 TO )=".ZX81.16X15.SCR.."
    73 FOR A=1 TO 15
    74 LET A$(B)=CHR$ (118)+A$(B,3 TO )+A$(B,2)
    75 NEXT A
    76 NEXT B
   100 POKE 16396,PEEK (16507)
   110 POKE 16397,PEEK (16508)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests