Coder le jeu du Snake avec PICO-8 en LUA

YouTube player

LE GRAND CLASSIQUE DES ANNEES 80

On ne présente plus ce jeu simple mais d’une efficacité redoutable.

Nokia est célèbre pour inclure le Snake dans la majorité de ses téléphones dans les années 80.

Dans ce tutoriel vous apprendrez a coder le jeu du Snake From Scratch (de A à Z) avec Pico-8 et le langage LUA. Pico-8 intègre un éditeur de code, mais aussi un éditeur de sprite et de son, ce qui vous permet de tout coder dans le même moteur.

LES REGLES

Vous dirigez le Snake, il ne peut pas sortir de l’écran, si rentrer en collision avec lui même. 

Lorsque vous manger une pomme , le Snake grandit et vous récupérez 1 point.

TESTEZ LE JEU DU TUTORIEL

PICO-8 permet l’export en version Windows, Linux, Mac, Rpi, mais aussi en HTML.

Lien Itch.io : https://pareinjeanphilippe.itch.io/snake-pico-8

CODE SOURCE

Comme dans toute cartouche PICO-8 le code est accessible avec le jeu, mais voici le script du jeu :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-- the snake
-- by parein jean-philippe
function _init()
--snake head
head ={
x=64,
y=64,
prevx,
prevy,
sp=5,
dx=8,
dy=0,
spd=5
}
--snake body
snake={}
snake[1]=head
--generate pos grid multiple 8
mult8 = {}
for i=0,120,8 do
mult8[#mult8+1]=i
end
--apple
apple={
x=mult8[flr(rnd(#mult8)+1)],
y=mult8[flr(rnd(#mult8)+1)]}
f=0 --frame
game_over=false --end of game
score=0 --apples count
end
------------------------------
function _update()
f-=1
if game_over then
for i=#snake,1,-1 do
if f<0 then
make_particles(
snake[i].x,
snake[i].y)
del(snake,snake[i])
sfx(3)
f=2
end
end
if btnp(❎) then _init() end
return
end
if f<0 then
f=head.spd --limit speed
--move snake
head.prevx=head.x
head.prevy=head.y
--sanke grow
for i=#snake,2,-1 do
snake[i].x=snake[i-1].x
snake[i].y=snake[i-1].y
end
head.x+=head.dx
head.y+=head.dy
end
if btn(⬅️) and head.dx!=8 then
head.dx=-8
head.dy=0
head.sp=4
elseif btn(➡️) and head.dx!=-8 then
head.dx=8
head.dy=0
head.sp=5
elseif btn(⬆️) and head.dy!=8 then
head.dx=0
head.dy=-8
head.sp=2
elseif btn(⬇️) and head.dy!=-8 then
head.dx=0
head.dy=8
head.sp=3
end
--collision with apple
if(head.x==apple.x and
head.y==apple.y) then
apple={
x=mult8[flr(rnd(#mult8)+1)],
y=mult8[flr(rnd(#mult8)+1)]}
--snake grow
score+=1
sfx(0)
body={
x=head.prevx,y=head.prevy}
add(snake,body)
--speed up
if #snake-1==10 then
head.spd=4
sfx(2)
elseif #snake-1==20 then
head.spd=3
sfx(2)
elseif #snake-1==30 then
head.spd=2
sfx(2)
elseif #snake-1==50 then
sfx(2)
head.spd=1
end
end
--snake out of screen
if head.x<0 or head.x>120 or head.y<0 or head.y>120 then
game_over=true
end
--collision with snake
for i=2,#snake do
if snake[i].x==head.x and snake[i].y==head.y then
game_over=true
end
end
end
------------------------------
function _draw()
cls(1)
--game over
if game_over then
print("press ❎ to restart", 28,60)
end
--draw apple
spr(1, apple.x, apple.y)
--draw bodys
for s in all (snake) do
spr(6,s.x, s.y)
end
--draw snake head
if not game_over then
spr(head.sp,head.x, head.y)
end
--draw score
print("score "..score,1,1,7)
--draw particles
col=3
for p in all (parts) do
pset(p.x,p.y,col)
p.x+=(rnd(2)-1)*10
p.y+=(rnd(2)-1)*10
p.lt-=1
col=flr(rnd(15))
if(p.lt==0) then del(parts,p) end
end
end
--particles
parts={}
function make_particles(xx,yy)
for i=1,10 do
p = {x=xx,y=yy,lt=15}
add(parts,p)
end
end
-- the snake -- by parein jean-philippe function _init() --snake head head ={ x=64, y=64, prevx, prevy, sp=5, dx=8, dy=0, spd=5 } --snake body snake={} snake[1]=head --generate pos grid multiple 8 mult8 = {} for i=0,120,8 do mult8[#mult8+1]=i end --apple apple={ x=mult8[flr(rnd(#mult8)+1)], y=mult8[flr(rnd(#mult8)+1)]} f=0 --frame game_over=false --end of game score=0 --apples count end ------------------------------ function _update() f-=1 if game_over then for i=#snake,1,-1 do if f<0 then make_particles( snake[i].x, snake[i].y) del(snake,snake[i]) sfx(3) f=2 end end if btnp(❎) then _init() end return end if f<0 then f=head.spd --limit speed --move snake head.prevx=head.x head.prevy=head.y --sanke grow for i=#snake,2,-1 do snake[i].x=snake[i-1].x snake[i].y=snake[i-1].y end head.x+=head.dx head.y+=head.dy end if btn(⬅️) and head.dx!=8 then head.dx=-8 head.dy=0 head.sp=4 elseif btn(➡️) and head.dx!=-8 then head.dx=8 head.dy=0 head.sp=5 elseif btn(⬆️) and head.dy!=8 then head.dx=0 head.dy=-8 head.sp=2 elseif btn(⬇️) and head.dy!=-8 then head.dx=0 head.dy=8 head.sp=3 end --collision with apple if(head.x==apple.x and head.y==apple.y) then apple={ x=mult8[flr(rnd(#mult8)+1)], y=mult8[flr(rnd(#mult8)+1)]} --snake grow score+=1 sfx(0) body={ x=head.prevx,y=head.prevy} add(snake,body) --speed up if #snake-1==10 then head.spd=4 sfx(2) elseif #snake-1==20 then head.spd=3 sfx(2) elseif #snake-1==30 then head.spd=2 sfx(2) elseif #snake-1==50 then sfx(2) head.spd=1 end end --snake out of screen if head.x<0 or head.x>120 or head.y<0 or head.y>120 then game_over=true end --collision with snake for i=2,#snake do if snake[i].x==head.x and snake[i].y==head.y then game_over=true end end end ------------------------------ function _draw() cls(1) --game over if game_over then print("press ❎ to restart", 28,60) end --draw apple spr(1, apple.x, apple.y) --draw bodys for s in all (snake) do spr(6,s.x, s.y) end --draw snake head if not game_over then spr(head.sp,head.x, head.y) end --draw score print("score "..score,1,1,7) --draw particles col=3 for p in all (parts) do pset(p.x,p.y,col) p.x+=(rnd(2)-1)*10 p.y+=(rnd(2)-1)*10 p.lt-=1 col=flr(rnd(15)) if(p.lt==0) then del(parts,p) end end end --particles parts={} function make_particles(xx,yy) for i=1,10 do p = {x=xx,y=yy,lt=15} add(parts,p) end end
-- the snake
-- by parein jean-philippe

function _init()
	 
 --snake head
	head ={
	x=64,
	y=64,
	prevx,
	prevy,
	sp=5, 
	dx=8, 
	dy=0,
	spd=5
	}
	
	--snake body
	snake={}
 snake[1]=head	

	--generate pos grid multiple 8
	mult8 = {}
	for i=0,120,8 do
		mult8[#mult8+1]=i
	end
		
	--apple
	apple={
	x=mult8[flr(rnd(#mult8)+1)],
 y=mult8[flr(rnd(#mult8)+1)]}
 	
	f=0 --frame
	
	game_over=false --end of game
	
	score=0 --apples count
end

------------------------------
function _update()
 
	f-=1
 
 if game_over then	
 
 	for i=#snake,1,-1 do
 		if f<0 then
 		make_particles(
 			snake[i].x,
 			snake[i].y)
 			del(snake,snake[i])
 			sfx(3) 			
 			f=2
 		end
 	end 	
 	
 	if btnp(❎) then _init() end
 	
 	return 
 
 end
 
	if f<0 then 
	
		f=head.spd --limit speed	
		
		--move snake
		head.prevx=head.x
		head.prevy=head.y		
		--sanke grow
		for i=#snake,2,-1 do 
		  snake[i].x=snake[i-1].x
		  snake[i].y=snake[i-1].y
		end
		
		head.x+=head.dx
		head.y+=head.dy	
	
	end	
	
		if btn(⬅️) and head.dx!=8 then
			head.dx=-8
			head.dy=0
			head.sp=4
		elseif btn(➡️) and head.dx!=-8 then
			head.dx=8
			head.dy=0
			head.sp=5
		elseif btn(⬆️) and head.dy!=8 then
			head.dx=0
			head.dy=-8
			head.sp=2
		elseif btn(⬇️) and head.dy!=-8 then
			head.dx=0
			head.dy=8
			head.sp=3
		end	
		
		--collision with apple
		if(head.x==apple.x and
			head.y==apple.y) then
				
				apple={
				x=mult8[flr(rnd(#mult8)+1)],
	 		y=mult8[flr(rnd(#mult8)+1)]}
	 		
	 		--snake grow
	 		score+=1
	 		sfx(0)	 		
	 		body={
	 		x=head.prevx,y=head.prevy}
	 		add(snake,body)
	 		
	 		--speed up
	 		if #snake-1==10 then 
		 		head.spd=4
		 		sfx(2)
	 		elseif #snake-1==20 then 
		 		head.spd=3
		 		sfx(2)
	 		elseif #snake-1==30 then 
		 		head.spd=2
		 		sfx(2)
	 		elseif #snake-1==50 then 
		 		sfx(2)
		 		head.spd=1	 		
	 		end
	 		
 	end	
 	
 	--snake out of screen
 	if head.x<0 or head.x>120 or head.y<0 or head.y>120 then
 		game_over=true
 	end
 			
 	--collision with snake
 	for i=2,#snake do
 		if snake[i].x==head.x and snake[i].y==head.y then
 			game_over=true
 		end
 	end
 	
end
------------------------------
function _draw()
	
		cls(1)	
		--game over
		if game_over then
			print("press ❎ to restart", 28,60)
		end
		
		--draw apple
	 spr(1, apple.x, apple.y)
	 
	 --draw bodys
	 for s in all (snake) do	 
	 	spr(6,s.x, s.y)
	 end
	 
		--draw snake	head
		if not game_over then
	 spr(head.sp,head.x, head.y)
		end 
		 
		--draw score
		print("score "..score,1,1,7)
		
		--draw particles
		col=3
 	for p in all (parts) do
 		pset(p.x,p.y,col)
 		p.x+=(rnd(2)-1)*10
 		p.y+=(rnd(2)-1)*10
 		p.lt-=1
 		col=flr(rnd(15))
 		if(p.lt==0) then del(parts,p) end
 	end
 	 
end

--particles

parts={}

function make_particles(xx,yy)
	
	for i=1,10 do
		p = {x=xx,y=yy,lt=15}
		add(parts,p)
	end

end
A propos de upln 280 Articles
En informatique le problème se situe souvent entre la chaise et le clavier !

Soyez le premier à commenter

Poster un Commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.