Snake: Difference between revisions

Content added Content deleted
Line 1,669: Line 1,669:
}
}
</syntaxhighlight>
</syntaxhighlight>


=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">'Snake example game for Craft Basic
'by Gemino Smothers 2022

define gfxx = 330, gfxy = 296
define upkey = 0, rightkey = 0, downkey = 0, leftkey = 0, esckey = 0
define speed = 0, delay = 0, score = 0, game = 1
define maxsize = 1000, size = 9, direction = int: (rnd) * 4 + 1
define rx = ( int: (rnd) * ( gfxx - 24 ) ) + 12
define ry = ( int: (rnd) * ( gfxy - 40 ) ) + 25

dim sx[maxsize]
dim sy[maxsize]

define fn (bounds) as sx[0] <= 1 or sx[0] >= gfxx or sy[0] <= 0 or sy[0] >= gfxy
define fn (collision) as sx[0] + 15 >= rx and sx[0] <= rx + 15 and sy[0] + 15 >= ry and sy[0] <= ry + 15
define fn (eatself) as sx[0] = sx[i] and sy[0] = sy[i]

let sx[0] = gfxx / 2
let sy[0] = gfxy / 2

title "Snake!"

resize 0, 0, gfxx + 12, gfxy + 50
center

fill on
bgcolor 0, 80, 0
cls

alert "Snake! by Gemino Smothers 2022"
alert "Get the gray little rodents and avoid hitting yourself or walls."
alert "Use arrow keys to move. Esc to exit."

input "Enter game speed between 0 to 100+", speed

do

button upkey, 38
button rightkey, 39
button downkey, 40
button leftkey, 37
button esckey, 27

if upkey = 1 and direction <> 3 then

let direction = 1

endif

if rightkey = 1 and direction <> 4 then

let direction = 2

endif

if downkey = 1 and direction <> 1 then

let direction = 3

endif

if leftkey = 1 and direction <> 2 then

let direction = 4

endif


let i = size + 1

relhdc

fgcolor 0, 80, 0
oval sx[size],sy[size],15,15

do

let i = i - 1
let c = i - 1

if (eatself) = 1 then

let game = 0

endif

let sx[i] = sx[c]
let sy[i] = sy[c]

fgcolor 0, 255, 0
oval sx[i], sy[i], 15, 15

loop i > 1

fgcolor 0, 0, 255
oval sx[0] + 5, sy[0] + 5, 3, 3
oval sx[0] + 9, sy[0] + 5, 3, 3

if direction = 1 then

let sy[0] = sy[0] - 15

endif

if direction = 2 then

let sx[0] = sx[0] + 15

endif

if direction = 3 then

let sy[0] = sy[0] + 15

endif

if direction = 4 then

let sx[0] = sx[0] - 15

endif

if (bounds) = 1 then

let game = 0

endif

if (collision) = 1 then

cls

let rx = ( int: (rnd) * ( gfxx - 12 ) ) + 6
let ry = ( int: (rnd) * ( gfxy - 40 ) ) + 25

let size = size + 3
let score = score + 1

endif

fgcolor 100,100,100
oval rx, ry, 15, 15

fgcolor 0, 0, 255
line gfxx, 0, gfxx, gfxy
line 0, gfxy, gfxx, gfxy

fgcolor 255,255,0
cursor 1,1
print "Score: ", score

relhdc

let delay = (clock)

do

wait

loop (clock) < delay + speed

loop esckey <> 1 and game = 1

alert "Game over! Score: ", score

end</syntaxhighlight>

=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
{{libheader| Winapi.Windows}}