Snake: Difference between revisions

170 bytes added ,  4 days ago
(3 intermediate revisions by 3 users not shown)
Line 1,523:
9 IF Q#5 THEN GOTO 4:Q=Q*(B=Q): COLOR=T: IF Q=0 THEN PLOT X,Y: IF Q=0 THEN PRINT "GAME OVER";: IF Q THEN PRINT "DONE";:K= PEEK (J):B=0: END </syntaxhighlight>
==={{header|Locomotive Basic}}===
[[File:Snake locomotive basic.png|thumb|''Snake'' running in CPCBasic]]
Use the cursor keys to control movement direction. LowerIf you find gameplay too easy, lower the skill parameter in line 20 or increase ml (maximum snake length) if you find gameplay too easy.
 
If you are playing this in [https://benchmarko.github.io/CPCBasic/cpcbasic.html CPCBasic], firstpaste clickthe oncode into the CPCtop screenbox, sothen itclick getson keyboardthe input"Reset" and then enter "runRun" (clickingbuttons below and finally click on the RunCPC buttonscreen wouldso deselectit gets keyboard focus. (The frame around the screen againwill turn dark).
Use the cursor keys to control movement direction. Lower the skill parameter in line 20 or increase ml (maximum length) if you find gameplay too easy.
 
If you are playing this in [https://benchmarko.github.io/CPCBasic/cpcbasic.html CPCBasic], first click on the CPC screen so it gets keyboard input and then enter "run" (clicking the Run button would deselect the screen again).
<syntaxhighlight lang="locobasic">10 mode 1:randomize time
15 ink 0,0:ink 1,6:ink 2,18:ink 3,11
20 sx=20:sy=5:dx=1:dy=0:ml=20:dim ox(ml),oy(ml):oi=1:ll=4:skill=68
30 f$=chr$(228):w$=chr$(127):b$=chr$(231)
40 pen 3:print string$(40,w$);
50 for i=2 to 20:print w$;space$(38);w$;:next
60 print string$(40,w$);
70 locate 1011, 1211:print string$(20,w$);
80 gosub 260
140 locate sx,sy:pen 2:print chr$(224);:ox(oi)=sx:oy(oi)=sy
90 frame
100 if inkey(1)>-1 then dx=1:dy=0
110 if inkey(8)>-1 then dx=-1:dy=0
120 if inkey(0)>-1 then dx=0:dy=-1
130 if inkey(2)>-1 then dx=0:dy=1
140 locate sx,sy:print chr$(224);:ox(oi)=sx:oy(oi)=sy
150 oi=oi+1:if oi>ml then oi=1
160 nx=sx+dx:ny=sy+dy
170 locate nx,ny:a$=copychr$(#0)
180 if a$=w$ or a$=b$ then sound 2,62500/20,100:locate 13,6:print "You have died!":end
190 if a$=f$ then sound 2,62500/500,10: sound 12,62500/1000,10: sound 42,62500/2000,10:p=p+100:print " ";:gosub 260:if ll<ml then ll=ll+1
200 locate 1,24:print "SCORE:"p
210 for i=1 to skill:frame:next
100211 if inkey(1)>-1 then dx=1:dy=0
220 locate sx,sy:print b$;
110212 if inkey(8)>-1 then dx=-1:dy=0
120213 if inkey(0)>-1 then dx=0:dy=-1
130214 if inkey(2)>-1 then dx=0:dy=1
215 next
220 locate sx,sy:pen 2:print b$;
230 nn=1+((oi+ml-ll) mod ml)
240 if ox(nn)>0 then locate ox(nn),oy(nn):print " ";
250 sx=nx:sy=ny:goto 90140
260 fx=rnd*39+1:fy=rnd*19+1
270 locate fx,fy:a$=copychr$(#0)
280 if a$<>" " then 260
290 pen 1:print f$;
300 return</syntaxhighlight>
 
Line 2,295 ⟶ 2,296:
[https://easylang.dev/apps/snake.html Run it]
{{Trans|Craft Basic}}
<syntaxhighlight lang="easylang">
subr fruit
rx = (randomrandint 20 - 1) * 5 + 2.5
ry = (randomrandint 20 - 1) * 5 + 2.5
.
subr start
Line 2,305 ⟶ 2,306:
sx[] = [ 52.5 0 0 0 0 ]
sy[] = [ 52.5 0 0 0 0 ]
dir = randomrandint 4
timer 0
.
Line 2,318 ⟶ 2,319:
move 6 30
text "Space or click to to start"
#
on key
if game = 0 and keybkey = " "
Line 2,365 ⟶ 2,366:
move rx ry
circle 1.5
#
sx = sx[1] ; sy = sy[1]
if dir = 1
Line 5,120 ⟶ 5,121:
{{libheader|Wren-dynamic}}
An embedded program so we can ask the C host to call ncurses and another library function for us.
<syntaxhighlight lang="ecmascriptwren">/* snakeSnake.wren */
 
import "random" for Random
Line 5,258 ⟶ 5,259:
<br>
Now embed this script in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc snakeSnake.c -o snakeSnake -lncurses -lwren -lm */
 
#include <stdio.h>
Line 5,404 ⟶ 5,405:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "snakeSnake.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
47

edits