Creating Snake Game

103 visualizaciones (últimos 30 días)
Andy
Andy el 23 de Abr. de 2011
Respondida: Deepika el 10 de Jul. de 2023
So like another person i'm making a snake game for matlab but i'm having trouble making the snake eat more than one piece of food, and follow the head of the snake every time it makes a turn. Any help would be extremely grateful.
function draw_snake(snake,food)
plot(snake(:,1),snake(:,2), 'ws',food(1,1),food(1,2), 'rs')%creates the vectors for the food and snake and plots them
whitebg([0 0 0])%creates black background
axis([0, 25, 0, 25])%creates the axis for gameplay
end
function checkpoint3()
global x y a b n
x =randi([10 20],1);%generates random x starting point for snake
y =randi([10 20],1);%generates random y starting point for snake
d =randi([1,4]);% generates random direction to start in for snake
a =randi([1 24],1);%generates random x coordinate for food
b =randi([1 24],1);%generates random y coordinate for food
snake=[x y];%defines the snake for x and y coordinates
food=[a b];%defines food for a and b coordinates
figure('KeyPressFcn',@my_callback);
function my_callback(splooge,event)%callback function for movement
switch event.Character
case 30%says if this arrow key is pressed
d = 1;%assign this value to that arrow key
case 31%says if this arrow key is pressed
d = 2;%assign this value to that arrow key
case 29 %says if this arrow key is pressed
d = 3;%assign this value to that arrow key
case 28 %says if this arrow key is pressed
d = 4;%assign this value to that arrow key
end
end
while (snake(1)>=0 && snake(2)>=0 && snake(1)<=25 && snake(2)<=25)%runs the snake as long as its within the game board
draw_snake(snake,food)
switch d%calling callback function
case 1%if this case is called on
snake(:,2)=snake(:,2)+1;%add value of 1 to y position
draw_snake(snake,food)%draws the snake
pause(.1)
case 2
snake(:,2)=snake(:,2)-1;%subtract value of 1 to y position
draw_snake(snake,food)%draws the snake
pause(.1)
case 3
snake(:,1)=snake(:,1)+1;%add value of 1 to x position
draw_snake(snake,food)%draws the snake
pause(.1)
case 4
snake(:,1)=snake(:,1)-1;%subtracts value of 1 to x position
draw_snake(snake,food)%draws the snake
pause(.1)
end
if snake(1)==food(1) && snake(2)==food(2)%if the snake and food are in the same position
n=length(snake); %creates variable for length of snake
switch d
case 1%if up is called on
snake(n,1)=food(1);%don't alter x position of new segment of snake
snake(n,2)=food(2)-1;%have new segment follow from bottom
case 2
snake(n,1)=food(1);%don't alter x position of new segment of snake
snake(n,2)=food(2)+1;%have new segment follow from the top
case 3
snake(n,1)=food(1)-1;%have new segment follow from left
snake(n,2)=food(2);%don't alter y position of new segment of snake
case 4
snake(n,1)=food(1)+1;%have new segment follow from right
snake(n,2)=food(2);%don't alter y position of new segment of snake
end
food(1) = randi([1 25]);%creates a new x position for the food
food(2) = randi([1 25]);%creates a new y position for the food
% b = randi([1 25]);
elseif snake(1)==0;%if snake exceeds boundaries display message box
msgbox('YOU LOSE HAHAHA')
elseif snake(2)==0;%if snake exceeds boundaries display message box
msgbox('YOU LOSE HAHAHA')
elseif snake(1)==25;%if snake exceeds boundaries display message box
msgbox('YOU LOSE HAHAHA')
elseif snake(2)==25;%if snake exceeds boundaries display message box
msgbox('YOU LOSE HAHAHA')
end
end
end
  2 comentarios
Moe Ebrahimi
Moe Ebrahimi el 7 de Dic. de 2020
i cant play it just sais cant find varieble snake
Walter Roberson
Walter Roberson el 7 de Dic. de 2020
In 2011 when this was posted, you would have had to put the two function into different files.
Starting from R2016b you could instead move the first function to after the second function.

Iniciar sesión para comentar.

Respuestas (3)

Andre Coelho
Andre Coelho el 28 de Feb. de 2017
Editada: Andre Coelho el 9 de Mzo. de 2017
I made some changes and improvements to your code. Check this:
function snake_game()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PRESS 'Q' TO EXIT GAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all
%OPTIONS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
difficulty=9; %difficulty: 1-10
bounds=0; %bounds? 1-yes 0-no
axis_limit= 15;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555555555
d=0;
x =round(axis_limit/2) %starting point
y =round(axis_limit/2) %starting point
d =randi([1,4]);% generates random direction to start in for snake
a =randi([1 axis_limit-1],1);%generates random x coordinate for food
b =randi([1 axis_limit-1],1);%generates random y coordinate for food
snake(1,1:2)=[x y];%defines the snake for x and y coordinates
size_snake=1;
ate=1; %snake ate food
ex=0; % used to exit game
food=[a b];%defines food for a and b coordinates
draw_snake(snake,food,size_snake,axis_limit)
figure('KeyPressFcn',@my_callback);
function my_callback(splooge,event)%callback function for movement
switch event.Character
case 'q'
ex=1;
case 30 % arrow direction
if(d~=2)
d = 1; %up d=1
end
case 31
if(d~=1)
d = 2; %down d=2
end
case 29
if(d~=4)
d = 3; %right d=3
end
case 28
if(d~=3)
d = 4; %left d=4
end
end
end
while (ex~=1)%runs the snake as long as q is not pressed
size_snake=size(snake);
size_snake=size_snake(1)
for l=size_snake+ate:-1:2
snake(l,:)=snake(l-1,:);
end
switch d %calling callback function
case 1
snake(1,2)=snake(1,2)+1;%add value of 1 to y position
case 2
snake(1,2)=snake(1,2)-1;%subtract value of 1 to y position
case 3
snake(1,1)=snake(1,1)+1;%add value of 1 to x position
case 4
snake(1,1)=snake(1,1)-1;%subtracts value of 1 to x position
end
draw_snake(snake,food,size_snake,axis_limit)%draws the snake
pause(max([(105-difficulty*10)/(10*axis_limit) .001])) %diffculty makes game faster;
if snake(1,1)==food(1) && snake(1,2)==food(2)%if the snake and food are in the same position
ate=1;
food(1) = randi([1 axis_limit-1]);%creates a new x position for the food
food(2) = randi([1 axis_limit-1]);%creates a new y position for the food
else
ate=0;
end
bounds
if bounds==1
snake(1,:)
if snake(1,1)==0 %if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
elseif snake(1,2)==0%if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
elseif snake(1,1)==axis_limit%if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
elseif snake(1,2)==axis_limit%if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
end
else
snake=snake-((snake>axis_limit).*(axis_limit+1));
snake=snake+((snake<0).*(axis_limit+1));
end
if (sum(snake(:, 1) ==snake(1, 1) & snake(:, 2) == snake(1, 2) )>1); %if snake hits itself
msgbox('YOU LOST! HAHA')
break
end
end
close all
end
function draw_snake(snake,food,size_snake,axis_limit)
for p = 1:size_snake
plot(snake(p,1),snake(p,2), 'wo')
hold on
end
plot(food(1,1),food(1,2), 'rs')%creates the vectors for the food and snake and plots them
whitebg([0 0 0])%creates black background
axis([0, axis_limit, 0, axis_limit])%creates the axis for gameplay
hold off
end
  1 comentario
Anand Sharma
Anand Sharma el 23 de Jul. de 2020
How do we keep the scores for each game so as to compare the results afterwards?

Iniciar sesión para comentar.


Sean de Wolski
Sean de Wolski el 23 de Abr. de 2011
Did you see my suggestion in the other thread?
  2 comentarios
Andy
Andy el 23 de Abr. de 2011
I did but I didn't really understand your approach, nor how it would be applied to the code I had already written. I'm honestly not savvy with computer code at all so I apologize for making you answer this question twice
Sean de Wolski
Sean de Wolski el 23 de Abr. de 2011
My suggestion leaves you with a matrix of zeros and integers representing the snake. The larger the integer, the further toward the tail it is. Thus if the snake eats food on an iteration, it doesn't cut off the tail, else it does.

Iniciar sesión para comentar.


Deepika
Deepika el 10 de Jul. de 2023
function snake_game()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PRESS 'Q' TO EXIT GAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all
%OPTIONS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
difficulty=9; %difficulty: 1-10
bounds=0; %bounds? 1-yes 0-no
axis_limit= 15;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555555555
d=0;
x =round(axis_limit/2) %starting point
y =round(axis_limit/2) %starting point
d =randi([1,4]);% generates random direction to start in for snake
a =randi([1 axis_limit-1],1);%generates random x coordinate for food
b =randi([1 axis_limit-1],1);%generates random y coordinate for food
snake(1,1:2)=[x y];%defines the snake for x and y coordinates
size_snake=1;
ate=1; %snake ate food
ex=0; % used to exit game
food=[a b];%defines food for a and b coordinates
draw_snake(snake,food,size_snake,axis_limit)
figure('KeyPressFcn',@my_callback);
function my_callback(splooge,event)%callback function for movement
switch event.Character
case 'q'
ex=1;
case 30 % arrow direction
if(d~=2)
d = 1; %up d=1
end
case 31
if(d~=1)
d = 2; %down d=2
end
case 29
if(d~=4)
d = 3; %right d=3
end
case 28
if(d~=3)
d = 4; %left d=4
end
end
end
while (ex~=1)%runs the snake as long as q is not pressed
size_snake=size(snake);
size_snake=size_snake(1)
for l=size_snake+ate:-1:2
snake(l,:)=snake(l-1,:);
end
switch d %calling callback function
case 1
snake(1,2)=snake(1,2)+1;%add value of 1 to y position
case 2
snake(1,2)=snake(1,2)-1;%subtract value of 1 to y position
case 3
snake(1,1)=snake(1,1)+1;%add value of 1 to x position
case 4
snake(1,1)=snake(1,1)-1;%subtracts value of 1 to x position
end
draw_snake(snake,food,size_snake,axis_limit)%draws the snake
pause(max([(105-difficulty*10)/(10*axis_limit) .001])) %diffculty makes game faster;
if snake(1,1)==food(1) && snake(1,2)==food(2)%if the snake and food are in the same position
ate=1;
food(1) = randi([1 axis_limit-1]);%creates a new x position for the food
food(2) = randi([1 axis_limit-1]);%creates a new y position for the food
else
ate=0;
end
bounds
if bounds==1
snake(1,:)
if snake(1,1)==0 %if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
elseif snake(1,2)==0%if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
elseif snake(1,1)==axis_limit%if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
elseif snake(1,2)==axis_limit%if snake exceeds boundaries display message box
msgbox('YOU LOST! HAHA')
ex=1
end
else
snake=snake-((snake>axis_limit).*(axis_limit+1));
snake=snake+((snake<0).*(axis_limit+1));
end
if (sum(snake(:, 1) ==snake(1, 1) & snake(:, 2) == snake(1, 2) )>1); %if snake hits itself
msgbox('YOU LOST! HAHA')
break
end
end
close all
end
function draw_snake(snake,food,size_snake,axis_limit)
for p = 1:size_snake
plot(snake(p,1),snake(p,2), 'wo')
hold on
end
plot(food(1,1),food(1,2), 'rs')%creates the vectors for the food and snake and plots them
whitebg([0 0 0])%creates black background
axis([0, axis_limit, 0, axis_limit])%creates the axis for gameplay
hold off
end

Categorías

Más información sobre Strategy & Logic en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by