Snake: Difference between revisions

m
 
(5 intermediate revisions by 4 users not shown)
Line 1,223:
rem by Gemino Smothers 2022
rem www.lucidapogee.com
 
title "Snake!"
 
define gfxx = 330, gfxy = 296
Line 1,237 ⟶ 1,239:
let sy[0] = gfxy / 2
 
fill on
title "Snake!"
bgcolor 128, 64, 0
cls graphics
 
resize 0, 0, gfxx + 10, gfxy + 56
center
 
formid 1
fill on
staticform 1, 1, 100, 14
bgcolor 128, 64, 0
fgcolor 255, 255, 0
cls
bgcolor 0, 80, 0
colorform
 
alert "Snake! by Gemino Smothers 2022"
Line 1,368 ⟶ 1,374:
 
fgcolor 255, 255, 0
cursor 1,formid 1
printformtext "Score: ", score
updateform
 
let delay = clock
Line 1,382 ⟶ 1,389:
 
playwave "examples\boom.wav"
alert "Game over! Score: ", score</syntaxhighlight>
 
alert "Game over! Score: ", score
 
end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 2,289 ⟶ 2,293:
 
=={{header|EasyLang}}==
[https://easylang.dev/apps/_snakesnake.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
call fruit
game = 1
sx[] = [ 52.5 0 0 0 0 ]
sy[] = [ 52.5 0 0 0 0 ]
dir = randomrandint 4
timer 0
.
Line 2,310 ⟶ 2,314:
text "SNAKE"
textsize 5
move 106 40
text "ArrowKeys keysor mouse for controlcontrolling"
move 106 30
text "PressSpace spaceor click to to start"
#
on key
if keybkeygame = "ArrowUp"0 and dirkeybkey <>= 3" "
dir = 1start
return
elif keybkey = "ArrowRight" and dir <> 4
dir = 2.
elifif keybkeydir =mod "ArrowDown"2 and dir <>= 1
dirif keybkey = 3"ArrowRight"
elif keybkey = "ArrowLeft" and dir <>= 2
direlif keybkey = 4"ArrowLeft"
elif keybkey = " " and gamedir = 04
call start.
else
if keybkey = "ArrowUp"
dir = 1
elif keybkey = "ArrowRightArrowDown" and dir <> 4
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
.
.
.
Line 2,331 ⟶ 2,360:
clear
color 997
move 2 9695
text "Score: " & 10 * len sx[] - 50
color 966
move rx ry
circle 1.5
#
sx = sx[1] ; sy = sy[1]
if dir = 1
Line 2,379 ⟶ 2,408:
len sx[] len sx[] + 3
len sy[] len sy[] + 3
call fruit
.
sx[1] = sx ; sy[1] = sy
Line 2,387 ⟶ 2,416:
color 997
move 10 10
text "PressSpace spaceor forclick new game"
.
.
Line 3,261 ⟶ 3,290:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using MakieGLMakie, GeometryBasics
Makie version in 99 lines.
<syntaxhighlight lang="julia">using Makie
 
mutable struct SnakeGame
height::Int
width::Int
snake::Vector{CartesianIndex{2}}
food::CartesianIndex{2}
end
 
Line 3,307 ⟶ 3,335:
 
function play(;n=10,t=0.5)
game = NodeObservable(SnakeGame(;width=n,height=n))
scene = Scene(resolution = (1000, 1000), raw = true, camera = campixel!)
display(scene)
Line 3,328 ⟶ 3,356:
 
score_text = @lift("Score: $(length($game.snake)-1)")
text!(scene, score_text, color=:gray, position = @lift((widths($area)[1]/2, widths($area)[2])), textsizefontsize = 50, align = (:center, :top))
 
direction = Ref{Any}(nothing)
 
on(events(scene).events.keyboardbuttonskeyboardbutton) do but
if ispressed(but,.action == Keyboard.press || but.action == Keyboard.left)repeat
direction[]if but.key == CartesianIndex(-1,0)Keyboard.left
direction[] = CartesianIndex(-1,0)
elseif ispressed(but, Keyboard.up)
direction[]elseif but.key == CartesianIndex(0,1)Keyboard.up
direction[] = CartesianIndex(0,1)
elseif ispressed(but, Keyboard.down)
direction[]elseif but.key == CartesianIndex(0,-1)Keyboard.down
direction[] = CartesianIndex(0,-1)
elseif ispressed(but, Keyboard.right)
direction[]elseif but.key == CartesianIndex(1,0)Keyboard.right
direction[] = CartesianIndex(1,0)
end
end
end
Line 5,090 ⟶ 5,120:
{{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,228 ⟶ 5,258:
<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,374 ⟶ 5,404:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "snakeSnake.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
1,969

edits