Please explain the occuring problems. "there's errors" does not allow to give a meaningful advice.
I need help to make this work!! snake game
3 次查看(过去 30 天)
显示 更早的评论
* This how I have it but there's errors I can't find
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
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!