Snake: Difference between revisions
Content deleted Content added
→{{header|Rust}}: minimization and renaming + screenshot |
|||
(20 intermediate revisions by 7 users not shown) | |||
Line 1,171:
REM E Y-DELTA
REM F FOOD COLOR= 4 DARK GREEN
REM H HIT= 127
REM I KEYBOARD= 49152
Line 1,197 ⟶ 1,196:
REM UPPER LOWER KEY BEHEST
REM 155 ESC QUIT
REM 139 UP UP
REM 193 225 A UP
REM 201 233 I UP
REM 138 DOWN DOWN
REM 218 250 Z DOWN
REM 203 235 K DOWN
REM 136
REM 202 234 J LEFT
REM 149
REM 204 236 L RIGHT
Line 1,210 ⟶ 1,211:
2 IF C THEN U = INT ( RND (T) * L):V = INT ( RND (T) * L): ON SCRN( U,V) > 0 GOTO 2: COLOR= F: PLOT U,V
3 K = PEEK (I):B = PEEK (J * (K > H)):B = K(K): GOTO
4 COLOR= T: PLOT X,Y: READ Q$: DATA5,20,20,1,-1,1,-1,13,9,4,10,1,40,1599,127,49152,49168,4608,6400,5,4,4,4,4,4,3,3,3,3,3,2,2,2,1,1,1,DONE,GAME OVER
5 IF Q = 5 THEN PRINT Q$;: END : HOME :Q = Q * (B = Q):B = 0: ON Q = 5 GOTO : RUN
6 LOMEM: 24576
7 DIM K(255),D(4),E(4),C(15): READ Q,X,Y,D(1),D(2),E(3),E(4),S,R,F,W,T,L,M,H,I,J,P,Z,K(155),K(139),K(193),K(225),K(201),K(233),K(138),K(218),K(250),K(203),K(235),K(136),K(202),K(234),K(149),K(204),K(236),Q$
8 DEF FN X(I) = PEEK (P + I): DEF FN Y(I) = PEEK (Z + I):B = RND (0):C(S) = T:C(R) = T:C(W) = T:B = INT ( RND (T) * 4) + T:D = D(B):E = E(B): POKE P + O,X: POKE Z + O,Y
9 GR : HOME : COLOR= W: VLIN 0,39 AT 0: VLIN 0,39 AT 39: HLIN 1,38 AT 0: HLIN 1,38 AT 39: COLOR= F: PLOT X + D,Y + E: GOTO</syntaxhighlight>
Line 1,222 ⟶ 1,223:
rem by Gemino Smothers 2022
rem www.lucidapogee.com
title "Snake!"
define gfxx = 330, gfxy = 296
Line 1,236 ⟶ 1,239:
let sy[0] = gfxy / 2
fill on
bgcolor 128, 64, 0
cls graphics
resize 0, 0, gfxx + 10, gfxy + 56
center
formid 1
staticform 1, 1, 100, 14
fgcolor 255, 255, 0
bgcolor 0, 80, 0
colorform
alert "Snake! by Gemino Smothers 2022"
Line 1,367 ⟶ 1,374:
fgcolor 255, 255, 0
updateform
let delay = clock
Line 1,381 ⟶ 1,389:
playwave "examples\boom.wav"
alert "Game over! Score: ", score</syntaxhighlight>
==={{header|FreeBASIC}}===
Line 1,518 ⟶ 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 arrow keys to control movement direction. If you find gameplay too easy, lower the skill parameter in line 20 or increase ml (maximum snake length).
If you would like to play this in [https://benchmarko.github.io/CPCBasic/cpcbasic.html CPCBasic], paste the code into the box at the top in CPCBasic, then click on the "Reset" and "Run" buttons below and finally click on the CPC screen so it gets keyboard focus. (This will turn the frame around the screen dark).
<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=8
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
80 gosub 260
140 locate sx,sy:pen 2: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
200 locate 1,24:print "SCORE:"p
210 for i=1 to skill:frame
211 if inkey(1)>-1 then dx=1:dy=0
212 if inkey(8)>-1 then dx=-1:dy=0
213 if inkey(0)>-1 then dx=0:dy=-1
214 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
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,286 ⟶ 2,292:
end
</syntaxhighlight>
=={{header|EasyLang}}==
[https://easylang.online/apps/snake.html Run it]
{{Trans|Craft Basic}}
<syntaxhighlight lang=text>
subr fruit
rx = (random 20 - 1) * 5 + 2.5
ry = (random 20 - 1) * 5 + 2.5
.
subr start
fruit
game = 1
sx[] = [ 52.5 0 0 0 0 ]
sy[] = [ 52.5 0 0 0 0 ]
dir = random 4
timer 0
.
background 242
move 30 70
clear
color 997
text "SNAKE"
textsize 5
move 6 40
text "Keys or mouse for controlling"
move 6 30
text "Space or click to to start"
#
on key
if game = 0 and keybkey = " "
start
return
.
if dir mod 2 = 1
if keybkey = "ArrowRight"
dir = 2
elif keybkey = "ArrowLeft"
dir = 4
.
else
if keybkey = "ArrowUp"
dir = 1
elif keybkey = "ArrowDown"
dir = 3
.
.
.
on mouse_down
if game = 0
start
return
.
if dir mod 2 = 1
if mouse_x < sx
dir = 4
else
dir = 2
.
else
if mouse_y < sy
dir = 3
else
dir = 1
.
.
.
on timer
clear
color 997
move 2 95
text "Score: " & 10 * len sx[] - 50
color 966
move rx ry
circle 1.5
#
sx = sx[1] ; sy = sy[1]
if dir = 1
sy += 5
elif dir = 2
sx += 5
elif dir = 3
sy -= 5
elif dir = 4
sx -= 5
.
if sx < 0 or sx > 100 or sy < 0 or sy > 100
game = 0
.
color 494
for i = len sx[] downto 2
if sx = sx[i] and sy = sy[i]
game = 0
.
sx[i] = sx[i - 1]
sy[i] = sy[i - 1]
if sx[i] > 0
move sx[i] sy[i]
circle 2.5
.
.
move sx sy
circle 2.5
color 000
if dir = 2 or dir = 4
move sx sy + 1
circle 0.5
move sx sy - 1
circle 0.5
else
move sx + 1 sy
circle 0.5
move sx - 1 sy
circle 0.5
.
if sx = rx and sy = ry
len sx[] len sx[] + 3
len sy[] len sy[] + 3
fruit
.
sx[1] = sx ; sy[1] = sy
if game = 1
timer 0.15
else
color 997
move 10 10
text "Space or click new game"
.
.
</syntaxhighlight>
=={{header|Emacs Lisp}}==
[[File:Snake emacs lisp.png|thumb|''Snake'' in Emacs]]
GNU Emacs has a built-in Snake game in Lisp that is started with
<syntaxhighlight>M-x snake</syntaxhighlight>
and which uses the same tile set as the included [[Tetris]] game.
=={{header|F Sharp}}==
Line 3,156 ⟶ 3,297:
=={{header|Julia}}==
<syntaxhighlight lang="julia">using GLMakie, GeometryBasics
mutable struct SnakeGame
height::Int
width::Int
snake::Vector{CartesianIndex{2}}
food::CartesianIndex{2}
end
Line 3,202 ⟶ 3,342:
function play(;n=10,t=0.5)
game =
scene = Scene(resolution = (1000, 1000), raw = true, camera = campixel!)
display(scene)
Line 3,223 ⟶ 3,363:
score_text = @lift("Score: $(length($game.snake)-1)")
text!(scene, score_text, color=:gray, position = @lift((widths($area)[1]/2, widths($area)[2])),
direction = Ref{Any}(nothing)
on(events(scene).
if
direction[] = CartesianIndex(-1,0)
direction[] = CartesianIndex(0,1)
direction[] = CartesianIndex(0,-1)
direction[] = CartesianIndex(1,0)
end
end
end
Line 4,604 ⟶ 4,746:
<syntaxhighlight lang="rust">/* add to file Cargo.toml:
[dependencies]
winsafe = "0.0.8" # IMHO: before the appearance of winsafe="0.1" it is not worth raising the version here
rand = "0.8"
derive-new = "0.5"
Line 4,612 ⟶ 4,754:
use derive_new::new;
use rand::{thread_rng, Rng};
use std::{cell::RefCell, rc::Rc};
use winsafe::{co, gui, prelude::*, COLORREF, HBRUSH, HPEN, RECT, SIZE};
Line 4,626 ⟶ 4,768:
#[derive(new)]
struct Context {
wnd: gui::WindowMain,
#[new(default) ] snake: Vec<i32>, // [rect_ids] where rect_id = y * TW + x (where x, y: nSTEPs)
#[new(value = "[ID0; 6]")] r: [i32; 6], // ID 6 rect to color in next frame (bg, tail, turn, body, food, head)
Line 4,645 ⟶ 4,787:
let wnd = gui::WindowMain::new(gui::WindowMainOpts {
title: "Snake - Start: Space, then press W-A-S-D".to_string(),
size:
class_bg_brush: brushes[bg],
..Default::default()
Line 4,659 ⟶ 4,801:
let hdc = ctx.wnd.hwnd().BeginPaint(&mut ps)?;
hdc.SelectObjectPen(HPEN::CreatePen(co::PS::NULL, 0, COLORREF::new(0, 0, 0))?)?;
for (&
let [left, top] = [id % TW, id / TW].map(|i| i * STEP - (STEP * GCW + SNAKE_W) / 2);
let rect = RECT { left, top, right: left + SNAKE_W, bottom: top + SNAKE_W };
hdc.SelectObjectBrush(brush)?;
hdc.RoundRect(rect, SIZE::new(SNAKE_W / 2, SNAKE_W / 2))?;
}
Line 4,701 ⟶ 4,842:
ctx.r[food] = *(grid.iter())
.filter(|i| **i != new_h && snake_cells.binary_search(i).is_err())
.nth(
.
} else if grid.binary_search(&new_h).is_err() || snake_cells.contains(&&new_h) {
ctx.wnd.hwnd().KillTimer(1)?; // Stop
Line 4,720 ⟶ 4,861:
}</syntaxhighlight>
{{out}}
[[File:Snake rust.png
=={{header|Sidef}}==
Line 4,986 ⟶ 5,127:
{{libheader|Wren-dynamic}}
An embedded program so we can ask the C host to call ncurses and another library function for us.
<syntaxhighlight lang="
import "random" for Random
Line 5,124 ⟶ 5,265:
<br>
Now embed this script in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc
#include <stdio.h>
Line 5,270 ⟶ 5,411:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
|