本项目在8*8点阵中实现,蛇的初始长度为7(数组1-7),可以自行更改初始长度,蛇头为7,食物为数组0,最外面一圈设置为墙(数组8-35),吃食增加蛇长的部分为数组36-44(可以自行更改最大增加长度),可以通过按键实现上下左右的移动,并且有撞墙和自杀检测功能。模块解释如下:
1.首先是生成1HZ移动信号,蛇一秒移动一次
--1HZ信号
P0:process(clk)
begin
if(clk'event and clk = '1') then
count <= count + 1;
if(count<25000000) then
clk1<='0';
elsif(count>=25000000 and count<50000000) then
clk1<='1';
elsif(count=50000000) then
count<=0;
end if;
end if;
end process;
2. 按键控制信号,从左到右的按键分别是上下左右
--控制方向
P1:process(clk,start_stop)
begin
if(clk'event and clk = '0') then
if(key = "0111") then
dir<="00";--上
elsif(key = "1011") then
dir<="01";--下
elsif(key = "1101") then
dir<="10";--左
elsif(key = "1110") then
dir<="11";--右
end if;
end if;
if(start_stop = '0') then
dir<="10";--左
end if;
end process;
3. 按键检测后蛇下一步移动方向,这里的snake_m是蛇正在移动的方向,检测到按键的按下,正在移动的方向就会改变
--蛇下一步移动的方向
P2:process(clk,start_stop)
begin
if(clk'event and clk = '0') then
if(snake_m = "00") then
if(dir="00") then
snake_m<="00";
elsif(dir="01") then
snake_m<="00";
elsif(dir="10") then
snake_m<="10";
elsif(dir="11") then
snake_m<="11";
end if;
elsif(snake_m = "01") then
if(dir="00") then
snake_m<="01";
elsif(dir="01") then
snake_m<="01";
elsif(dir="10") then
snake_m<="10";
elsif(dir="11") then
snake_m<="11";
end if;
elsif(snake_m = "10") then
if(dir="00") then
snake_m<="00";
elsif(dir="01") then
snake_m<="01";
elsif(dir="10") then
snake_m<="10";
elsif(dir="11") then
snake_m<="10";
end if;
elsif(snake_m = "11") then
if(dir="00") then
snake_m<="00";
elsif(dir="01") then
snake_m<="01";
elsif(dir="10") then
snake_m<="11";
elsif(dir="11") then
snake_m<="11";
end if;
end if;
end if;
if(start_stop = '0') then
snake_m<="10";
end if;
end process;
4. 用动态扫描来在8*8点阵中显示,小于36的部分是属于蛇初始长度和墙的扫描,大于等于36的是蛇吃食物后增加的身体部分,为什么我这里要用两部分来写,因为蛇身增加的部分之前是不能显示的,初始化的时候点阵不能显示,我就想点阵高电平有效的部分全部为低电平,低电平有效的部分全部为高电平,这样就不会显示,如下两段程序
这里两段截取的是count1 >= 36是的动态扫描,可以与前面count1 <36进行一个对比
when "111" => row<="11111111";
when "111" => col<="00000000";
点阵上生成的完整程序如下:
--在点阵上显示生成
P3:process(clk_yd)
begin
if(clk_yd'event and clk_yd = '1') then
if(count1 > 44) then
count1 <= 0;
else
count1 <= count1 + 1;
end if;
if(count1 <36) then
case snake_x(count1) is
when "000" => row<="01111111";
when "001" => row<="10111111";
when "010" => row<="11011111";
when "011" => row<="11101111";
when "100" => row<="11110111";
when "101" => row<="11111011";
when "110" => row<="11111101";
when "111" => row<="11111110";
when others => row<="11111111";
end case;
case snake_y(count1) is
when "000" => col<="10000000";
when "001" => col<="01000000";
when "010" => col<="00100000";
when "011" => col<="00010000";
when "100" => col<="00001000";
when "101" => col<="00000100";
when "110" => col<="00000010";
when "111" => col<="00000001";
when others => col<="00000000";
end case;
elsif(count1 >= 36) then
case snake_x(count1) is
when "000" => row<="01111111";
when "001" => row<="10111111";
when "010" => row<="11011111";
when "011" => row<="11101111";
when "100" => row<="11110111";
when "101" => row<="11111011";
when "110" => row<="11111101";
when "111" => row<="11111111";
when others => row<="11111111";
end case;
case snake_y(count1) is
when "000" => col<="10000000";
when "001" => col<="01000000";
when "010" => col<="00100000";
when "011" => col<="00010000";
when "100" => col<="00001000";
when "101" => col<="00000100";
when "110" => col<="00000010";
when "111" => col<="00000000";
when others => col<="00000000";
end case;
end if;
end if;
end process;
5.接下来就是蛇身移动模块了,一秒移动一次,也就是进程里面必须有clk1这个1HZ的信号,然后就是start_stop(开始启停信号), die(死亡检测信号),victory(胜利信号),也就是意味着拨下开始键,并且蛇还没撞墙和自杀,还没胜利之前,蛇都是移动的
(1)吃食计分部分:当蛇头【snake_y(7),snake_x(7)】和食物【snake_y(0),snake_x(0)】重合的时候,说明吃到了食物,吃到食物进行计分(计分到“1001”说明完成游戏,将‘1’送给victory),然后根据蛇头更新食物信息,还要防止食物生成在墙上,因为用的是同一个8*8点阵
--蛇吃食计分,生成新的食物
if(snake_y(7) = snake_y(0) and snake_x(7) = snake_x(0)) then
score <= score + 1;
if(score = "1001") then
victory <= '1';
end if;
snake_y(0) <= snake_y(7) - 1;
snake_x(0) <= snake_x(7) + 3;
end if;
--防止食物生成在墙上
for i in 8 to 35 loop
if(snake_y(0) = snake_y(i) and snake_x(0) = snake_x(i)) then
snake_y(0) <= snake_y(0) - 3;
snake_x(0) <= snake_x(0) - 4;
end if;
end loop;
(2)蛇自杀检测:当和自己撞到是,将‘1’给die,这里考虑的蛇身是1-6和36-44
--蛇自杀检测
for i in 1 to 6 loop
if(snake_y(7) = snake_y(i) and snake_x(7) = snake_x(i)) then
die <= '1';
end if;
end loop;
for i in 36 to 44 loop
if(snake_y(7) = snake_y(i) and snake_x(7) = snake_x(i)) then
die <= '1';
end if;
end loop;
(3)蛇撞墙检测:当撞到墙,将‘1’给die
--蛇撞墙检测
for i in 8 to 35 loop
if(snake_y(7) = snake_y(i) and snake_x(7) = snake_x(i)) then
die <= '1';
end if;
end loop;
(4)蛇开始的初始化
if(start_stop = '0') then
--蛇身
snake_y(7) <= "011";
snake_y(6) <= "011";
snake_y(5) <= "011";
snake_y(4) <= "011";
snake_y(3) <= "100";
snake_y(2) <= "101";
snake_y(1) <= "101";
--食物
snake_y(0) <= "100";
--蛇身
snake_x(7) <= "011";
snake_x(6) <= "100";
snake_x(5) <= "101";
snake_x(4) <= "110";
snake_x(3) <= "110";
snake_x(2) <= "110";
snake_x(1) <= "101";
--食物
snake_x(0) <= "101";
--墙(纵坐标)
snake_y(8) <= "000";
snake_y(9) <= "000";
snake_y(10) <= "000";
snake_y(11) <= "000";
snake_y(12) <= "000";
snake_y(13) <= "000";
snake_y(14) <= "000";
snake_y(15) <= "000";
snake_y(16) <= "001";
snake_y(17) <= "001";
snake_y(18) <= "010";
snake_y(19) <= "010";
snake_y(20) <= "011";
snake_y(21) <= "011";
snake_y(22) <= "100";
snake_y(23) <= "100";
snake_y(24) <= "101";
snake_y(25) <= "101";
snake_y(26) <= "110";
snake_y(27) <= "110";
snake_y(28) <= "111";
snake_y(29) <= "111";
snake_y(30) <= "111";
snake_y(31) <= "111";
snake_y(32) <= "111";
snake_y(33) <= "111";
snake_y(34) <= "111";
snake_y(35) <= "111";
--墙(横坐标)
snake_x(8) <= "000";
snake_x(9) <= "001";
snake_x(10) <= "010";
snake_x(11) <= "011";
snake_x(12) <= "100";
snake_x(13) <= "101";
snake_x(14) <= "110";
snake_x(15) <= "111";
snake_x(16) <= "000";
snake_x(17) <= "111";
snake_x(18) <= "000";
snake_x(19) <= "111";
snake_x(20) <= "000";
snake_x(21) <= "111";
snake_x(22) <= "000";
snake_x(23) <= "111";
snake_x(24) <= "000";
snake_x(25) <= "111";
snake_x(26) <= "000";
snake_x(27) <= "111";
snake_x(28) <= "000";
snake_x(29) <= "001";
snake_x(30) <= "010";
snake_x(31) <= "011";
snake_x(32) <= "100";
snake_x(33) <= "101";
snake_x(34) <= "110";
snake_x(35) <= "111";
--分数
score <= "0000";
--死亡标志
die <= '0';
--胜利标志
victory <= '0';
--长的身体
snake_x(36) <= "111";
snake_x(37) <= "111";
snake_x(38) <= "111";
snake_x(39) <= "111";
snake_x(40) <= "111";
snake_x(41) <= "111";
snake_x(42) <= "111";
snake_x(43) <= "111";
snake_x(44) <= "111";
snake_y(36) <= "111";
snake_y(37) <= "111";
snake_y(38) <= "111";
snake_y(39) <= "111";
snake_y(40) <= "111";
snake_y(41) <= "111";
snake_y(42) <= "111";
snake_y(43) <= "111";
snake_y(44) <= "111";
end if;
(5)检测到死亡和检测到胜利做出的反应
--检测死亡
if(die = '1') then
snake_y(7) <= "010";
snake_y(6) <= "110";
snake_y(5) <= "100";
snake_y(4) <= "101";
snake_y(3) <= "100";
snake_y(2) <= "101";
snake_y(1) <= "010";
snake_y(0) <= "110";
snake_x(7) <= "010";
snake_x(6) <= "010";
snake_x(5) <= "011";
snake_x(4) <= "010";
snake_x(3) <= "100";
snake_x(2) <= "101";
snake_x(1) <= "101";
snake_x(0) <= "101";
snake_x(36) <= "111";
snake_x(37) <= "111";
snake_x(38) <= "111";
snake_x(39) <= "111";
snake_x(40) <= "111";
snake_x(41) <= "111";
snake_x(42) <= "111";
snake_x(43) <= "111";
snake_x(44) <= "111";
snake_y(36) <= "111";
snake_y(37) <= "111";
snake_y(38) <= "111";
snake_y(39) <= "111";
snake_y(40) <= "111";
snake_y(41) <= "111";
snake_y(42) <= "111";
snake_y(43) <= "111";
snake_y(44) <= "111";
--检测胜利
elsif(victory = '1') then
snake_y(7) <= "010";
snake_y(6) <= "100";
snake_y(5) <= "101";
snake_y(4) <= "110";
snake_y(3) <= "110";
snake_y(2) <= "101";
snake_y(1) <= "100";
snake_y(0) <= "010";
snake_x(7) <= "010";
snake_x(6) <= "010";
snake_x(5) <= "010";
snake_x(4) <= "011";
snake_x(3) <= "100";
snake_x(2) <= "101";
snake_x(1) <= "101";
snake_x(0) <= "101";
snake_x(36) <= "111";
snake_x(37) <= "111";
snake_x(38) <= "111";
snake_x(39) <= "111";
snake_x(40) <= "111";
snake_x(41) <= "111";
snake_x(42) <= "111";
snake_x(43) <= "111";
snake_x(44) <= "111";
snake_y(36) <= "111";
snake_y(37) <= "111";
snake_y(38) <= "111";
snake_y(39) <= "111";
snake_y(40) <= "111";
snake_y(41) <= "111";
snake_y(42) <= "111";
snake_y(43) <= "111";
snake_y(44) <= "111";
end if;
5.数码管显示计分部分
P5:process(score)
begin
case score is
when "0000"=>score_1<="1111110"; -- a到g
when "0001"=>score_1<="0110000";
when "0010"=>score_1<="1101101";
when "0011"=>score_1<="1111001";
when "0100"=>score_1<="0110011";
when "0101"=>score_1<="1011011";
when "0110"=>score_1<="0011111";
when "0111"=>score_1<="1110000";
when "1000"=>score_1<="1111111";
when "1001"=>score_1<="1110011";
when others=>score_1<="0000000";
end case;
end process;
☆基于VHDL的贪吃蛇完整程序如下:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity test6 is
port(
clk:in std_logic;--时钟
start_stop:in std_logic;--启停信号
key:in std_logic_vector(3 downto 0);--方向控制
col:out std_logic_vector(7 downto 0);--列坐标
row:out std_logic_vector(7 downto 0);--行坐标
--
score_1:out std_logic_vector(6 downto 0)--数码管
);
end test6;
architecture behave of test6 is
signal dir:std_logic_vector(1 downto 0);--控制蛇移动的方向
type word is array(0 to 44)of std_logic_vector(2 downto 0);
--定义word数据类型
signal count1:integer range 0 to 44;
signal snake_y:word;--定义纵坐标
signal snake_x:word;--定义横坐标
signal count:integer range 0 to 50000000;--分频信号
signal snake_m:std_logic_vector(1 downto 0);--蛇正在移动的方向
signal clk1:std_logic;--一秒时钟
signal clk_yd:std_logic;
signal cont:integer range 0 to 999;
--
signal score:std_logic_vector(3 downto 0) := "0000";--二进制计分
signal die:std_logic;--检测自杀
signal victory:std_logic;--胜利的信号
--生成频率为1HZ的信号
begin
--将蛇刷新的频率变慢一些,防止频率过快
P:process(clk)
begin
if(clk'event and clk = '1') then
cont <= cont + 1;
if(cont = 999) then
clk_yd <= not clk_yd;
cont <= 0;
end if;
end if;
end process;
--1HZ信号
P0:process(clk)
begin
if(clk'event and clk = '1') then
count <= count + 1;
if(count<25000000) then
clk1<='0';
elsif(count>=25000000 and count<50000000) then
clk1<='1';
elsif(count=50000000) then
count<=0;
end if;
end if;
end process;
--控制方向
P1:process(clk,start_stop)
begin
if(clk'event and clk = '0') then
if(key = "0111") then
dir<="00";--上
elsif(key = "1011") then
dir<="01";--下
elsif(key = "1101") then
dir<="10";--左
elsif(key = "1110") then
dir<="11";--右
end if;
end if;
if(start_stop = '0') then
dir<="10";--左
end if;
end process;
--蛇下一步移动的方向
P2:process(clk,start_stop)
begin
if(clk'event and clk = '0') then
if(snake_m = "00") then
if(dir="00") then
snake_m<="00";
elsif(dir="01") then
snake_m<="00";
elsif(dir="10") then
snake_m<="10";
elsif(dir="11") then
snake_m<="11";
end if;
elsif(snake_m = "01") then
if(dir="00") then
snake_m<="01";
elsif(dir="01") then
snake_m<="01";
elsif(dir="10") then
snake_m<="10";
elsif(dir="11") then
snake_m<="11";
end if;
elsif(snake_m = "10") then
if(dir="00") then
snake_m<="00";
elsif(dir="01") then
snake_m<="01";
elsif(dir="10") then
snake_m<="10";
elsif(dir="11") then
snake_m<="10";
end if;
elsif(snake_m = "11") then
if(dir="00") then
snake_m<="00";
elsif(dir="01") then
snake_m<="01";
elsif(dir="10") then
snake_m<="11";
elsif(dir="11") then
snake_m<="11";
end if;
end if;
end if;
if(start_stop = '0') then
snake_m<="10";
end if;
end process;
--在点阵上显示生成
P3:process(clk_yd)
begin
if(clk_yd'event and clk_yd = '1') then
if(count1 > 44) then
count1 <= 0;
else
count1 <= count1 + 1;
end if;
if(count1 <36) then
case snake_x(count1) is
when "000" => row<="01111111";
when "001" => row<="10111111";
when "010" => row<="11011111";
when "011" => row<="11101111";
when "100" => row<="11110111";
when "101" => row<="11111011";
when "110" => row<="11111101";
when "111" => row<="11111110";
when others => row<="11111111";
end case;
case snake_y(count1) is
when "000" => col<="10000000";
when "001" => col<="01000000";
when "010" => col<="00100000";
when "011" => col<="00010000";
when "100" => col<="00001000";
when "101" => col<="00000100";
when "110" => col<="00000010";
when "111" => col<="00000001";
when others => col<="00000000";
end case;
elsif(count1 >= 36) then
case snake_x(count1) is
when "000" => row<="01111111";
when "001" => row<="10111111";
when "010" => row<="11011111";
when "011" => row<="11101111";
when "100" => row<="11110111";
when "101" => row<="11111011";
when "110" => row<="11111101";
when "111" => row<="11111111";
when others => row<="11111111";
end case;
case snake_y(count1) is
when "000" => col<="10000000";
when "001" => col<="01000000";
when "010" => col<="00100000";
when "011" => col<="00010000";
when "100" => col<="00001000";
when "101" => col<="00000100";
when "110" => col<="00000010";
when "111" => col<="00000000";
when others => col<="00000000";
end case;
end if;
end if;
end process;
--蛇身移动模块
P4:process(clk1,start_stop,die,victory)
begin
if(clk1'event and clk1 = '1' and start_stop = '1' and die = '0' and victory = '0') then
--左
if(snake_m = "10") then
snake_x(6) <= snake_x(7);
snake_x(5) <= snake_x(6);
snake_x(4) <= snake_x(5);
snake_x(3) <= snake_x(4);
snake_x(2) <= snake_x(3);
snake_x(1) <= snake_x(2);
snake_y(6) <= snake_y(7);
snake_y(5) <= snake_y(6);
snake_y(4) <= snake_y(5);
snake_y(3) <= snake_y(4);
snake_y(2) <= snake_y(3);
snake_y(1) <= snake_y(2);
snake_x(7) <= snake_x(7) - 1;
--上
elsif(snake_m = "00") then
snake_x(6) <= snake_x(7);
snake_x(5) <= snake_x(6);
snake_x(4) <= snake_x(5);
snake_x(3) <= snake_x(4);
snake_x(2) <= snake_x(3);
snake_x(1) <= snake_x(2);
snake_y(6) <= snake_y(7);
snake_y(5) <= snake_y(6);
snake_y(4) <= snake_y(5);
snake_y(3) <= snake_y(4);
snake_y(2) <= snake_y(3);
snake_y(1) <= snake_y(2);
snake_y(7) <= snake_y(7) - 1;
--右
elsif(snake_m = "11") then
snake_x(6) <= snake_x(7);
snake_x(5) <= snake_x(6);
snake_x(4) <= snake_x(5);
snake_x(3) <= snake_x(4);
snake_x(2) <= snake_x(3);
snake_x(1) <= snake_x(2);
snake_y(6) <= snake_y(7);
snake_y(5) <= snake_y(6);
snake_y(4) <= snake_y(5);
snake_y(3) <= snake_y(4);
snake_y(2) <= snake_y(3);
snake_y(1) <= snake_y(2);
snake_x(7) <= snake_x(7) + 1;
--下
elsif(snake_m = "01") then
snake_x(6) <= snake_x(7);
snake_x(5) <= snake_x(6);
snake_x(4) <= snake_x(5);
snake_x(3) <= snake_x(4);
snake_x(2) <= snake_x(3);
snake_x(1) <= snake_x(2);
snake_y(6) <= snake_y(7);
snake_y(5) <= snake_y(6);
snake_y(4) <= snake_y(5);
snake_y(3) <= snake_y(4);
snake_y(2) <= snake_y(3);
snake_y(1) <= snake_y(2);
snake_y(7) <= snake_y(7) + 1;
end if;
case score is
when "0001" => snake_x(36) <= snake_x(1);
snake_y(36) <= snake_y(1);
when "0010" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
when "0011" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_x(38) <= snake_x(37);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
snake_y(38) <= snake_y(37);
when "0100" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_x(38) <= snake_x(37);
snake_x(39) <= snake_x(38);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
snake_y(38) <= snake_y(37);
snake_y(39) <= snake_y(38);
when "0101" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_x(38) <= snake_x(37);
snake_x(39) <= snake_x(38);
snake_x(40) <= snake_x(39);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
snake_y(38) <= snake_y(37);
snake_y(39) <= snake_y(38);
snake_y(40) <= snake_y(39);
when "0110" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_x(38) <= snake_x(37);
snake_x(39) <= snake_x(38);
snake_x(40) <= snake_x(39);
snake_x(41) <= snake_x(40);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
snake_y(38) <= snake_y(37);
snake_y(39) <= snake_y(38);
snake_y(40) <= snake_y(39);
snake_y(41) <= snake_y(40);
when "0111" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_x(38) <= snake_x(37);
snake_x(39) <= snake_x(38);
snake_x(40) <= snake_x(39);
snake_x(41) <= snake_x(40);
snake_x(42) <= snake_x(41);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
snake_y(38) <= snake_y(37);
snake_y(39) <= snake_y(38);
snake_y(40) <= snake_y(39);
snake_y(41) <= snake_y(40);
snake_y(42) <= snake_y(41);
when "1000" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_x(38) <= snake_x(37);
snake_x(39) <= snake_x(38);
snake_x(40) <= snake_x(39);
snake_x(41) <= snake_x(40);
snake_x(42) <= snake_x(41);
snake_x(43) <= snake_x(42);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
snake_y(38) <= snake_y(37);
snake_y(39) <= snake_y(38);
snake_y(40) <= snake_y(39);
snake_y(41) <= snake_y(40);
snake_y(42) <= snake_y(41);
snake_y(43) <= snake_y(42);
when "1001" => snake_x(36) <= snake_x(1);
snake_x(37) <= snake_x(36);
snake_x(38) <= snake_x(37);
snake_x(39) <= snake_x(38);
snake_x(40) <= snake_x(39);
snake_x(41) <= snake_x(40);
snake_x(42) <= snake_x(41);
snake_x(43) <= snake_x(42);
snake_x(44) <= snake_x(43);
snake_y(36) <= snake_y(1);
snake_y(37) <= snake_y(36);
snake_y(38) <= snake_y(37);
snake_y(39) <= snake_y(38);
snake_y(40) <= snake_y(39);
snake_y(41) <= snake_y(40);
snake_y(42) <= snake_y(41);
snake_y(43) <= snake_y(42);
snake_y(44) <= snake_y(43);
when others => null;
end case;
--蛇吃食计分,生成新的食物
if(snake_y(7) = snake_y(0) and snake_x(7) = snake_x(0)) then
score <= score + 1;
if(score = "1001") then
victory <= '1';
end if;
snake_y(0) <= snake_y(7) - 1;
snake_x(0) <= snake_x(7) + 3;
end if;
--防止食物生成在墙上
for i in 8 to 35 loop
if(snake_y(0) = snake_y(i) and snake_x(0) = snake_x(i)) then
snake_y(0) <= snake_y(0) - 3;
snake_x(0) <= snake_x(0) - 4;
end if;
end loop;
--蛇自杀检测
for i in 1 to 6 loop
if(snake_y(7) = snake_y(i) and snake_x(7) = snake_x(i)) then
die <= '1';
end if;
end loop;
for i in 36 to 44 loop
if(snake_y(7) = snake_y(i) and snake_x(7) = snake_x(i)) then
die <= '1';
end if;
end loop;
--蛇撞墙检测
for i in 8 to 35 loop
if(snake_y(7) = snake_y(i) and snake_x(7) = snake_x(i)) then
die <= '1';
end if;
end loop;
end if;
if(start_stop = '0') then
--蛇身
snake_y(7) <= "011";
snake_y(6) <= "011";
snake_y(5) <= "011";
snake_y(4) <= "011";
snake_y(3) <= "100";
snake_y(2) <= "101";
snake_y(1) <= "101";
--食物
snake_y(0) <= "100";
--蛇身
snake_x(7) <= "011";
snake_x(6) <= "100";
snake_x(5) <= "101";
snake_x(4) <= "110";
snake_x(3) <= "110";
snake_x(2) <= "110";
snake_x(1) <= "101";
--食物
snake_x(0) <= "101";
--墙(纵坐标)
snake_y(8) <= "000";
snake_y(9) <= "000";
snake_y(10) <= "000";
snake_y(11) <= "000";
snake_y(12) <= "000";
snake_y(13) <= "000";
snake_y(14) <= "000";
snake_y(15) <= "000";
snake_y(16) <= "001";
snake_y(17) <= "001";
snake_y(18) <= "010";
snake_y(19) <= "010";
snake_y(20) <= "011";
snake_y(21) <= "011";
snake_y(22) <= "100";
snake_y(23) <= "100";
snake_y(24) <= "101";
snake_y(25) <= "101";
snake_y(26) <= "110";
snake_y(27) <= "110";
snake_y(28) <= "111";
snake_y(29) <= "111";
snake_y(30) <= "111";
snake_y(31) <= "111";
snake_y(32) <= "111";
snake_y(33) <= "111";
snake_y(34) <= "111";
snake_y(35) <= "111";
--墙(横坐标)
snake_x(8) <= "000";
snake_x(9) <= "001";
snake_x(10) <= "010";
snake_x(11) <= "011";
snake_x(12) <= "100";
snake_x(13) <= "101";
snake_x(14) <= "110";
snake_x(15) <= "111";
snake_x(16) <= "000";
snake_x(17) <= "111";
snake_x(18) <= "000";
snake_x(19) <= "111";
snake_x(20) <= "000";
snake_x(21) <= "111";
snake_x(22) <= "000";
snake_x(23) <= "111";
snake_x(24) <= "000";
snake_x(25) <= "111";
snake_x(26) <= "000";
snake_x(27) <= "111";
snake_x(28) <= "000";
snake_x(29) <= "001";
snake_x(30) <= "010";
snake_x(31) <= "011";
snake_x(32) <= "100";
snake_x(33) <= "101";
snake_x(34) <= "110";
snake_x(35) <= "111";
--分数
score <= "0000";
--死亡标志
die <= '0';
--胜利标志
victory <= '0';
--长的身体
snake_x(36) <= "111";
snake_x(37) <= "111";
snake_x(38) <= "111";
snake_x(39) <= "111";
snake_x(40) <= "111";
snake_x(41) <= "111";
snake_x(42) <= "111";
snake_x(43) <= "111";
snake_x(44) <= "111";
snake_y(36) <= "111";
snake_y(37) <= "111";
snake_y(38) <= "111";
snake_y(39) <= "111";
snake_y(40) <= "111";
snake_y(41) <= "111";
snake_y(42) <= "111";
snake_y(43) <= "111";
snake_y(44) <= "111";
end if;
--检测死亡
if(die = '1') then
snake_y(7) <= "010";
snake_y(6) <= "110";
snake_y(5) <= "100";
snake_y(4) <= "101";
snake_y(3) <= "100";
snake_y(2) <= "101";
snake_y(1) <= "010";
snake_y(0) <= "110";
snake_x(7) <= "010";
snake_x(6) <= "010";
snake_x(5) <= "011";
snake_x(4) <= "010";
snake_x(3) <= "100";
snake_x(2) <= "101";
snake_x(1) <= "101";
snake_x(0) <= "101";
snake_x(36) <= "111";
snake_x(37) <= "111";
snake_x(38) <= "111";
snake_x(39) <= "111";
snake_x(40) <= "111";
snake_x(41) <= "111";
snake_x(42) <= "111";
snake_x(43) <= "111";
snake_x(44) <= "111";
snake_y(36) <= "111";
snake_y(37) <= "111";
snake_y(38) <= "111";
snake_y(39) <= "111";
snake_y(40) <= "111";
snake_y(41) <= "111";
snake_y(42) <= "111";
snake_y(43) <= "111";
snake_y(44) <= "111";
--检测胜利
elsif(victory = '1') then
snake_y(7) <= "010";
snake_y(6) <= "100";
snake_y(5) <= "101";
snake_y(4) <= "110";
snake_y(3) <= "110";
snake_y(2) <= "101";
snake_y(1) <= "100";
snake_y(0) <= "010";
snake_x(7) <= "010";
snake_x(6) <= "010";
snake_x(5) <= "010";
snake_x(4) <= "011";
snake_x(3) <= "100";
snake_x(2) <= "101";
snake_x(1) <= "101";
snake_x(0) <= "101";
snake_x(36) <= "111";
snake_x(37) <= "111";
snake_x(38) <= "111";
snake_x(39) <= "111";
snake_x(40) <= "111";
snake_x(41) <= "111";
snake_x(42) <= "111";
snake_x(43) <= "111";
snake_x(44) <= "111";
snake_y(36) <= "111";
snake_y(37) <= "111";
snake_y(38) <= "111";
snake_y(39) <= "111";
snake_y(40) <= "111";
snake_y(41) <= "111";
snake_y(42) <= "111";
snake_y(43) <= "111";
snake_y(44) <= "111";
end if;
end process;
--计分
P5:process(score)
begin
case score is
when "0000"=>score_1<="1111110"; -- a到g
when "0001"=>score_1<="0110000";
when "0010"=>score_1<="1101101";
when "0011"=>score_1<="1111001";
when "0100"=>score_1<="0110011";
when "0101"=>score_1<="1011011";
when "0110"=>score_1<="0011111";
when "0111"=>score_1<="1110000";
when "1000"=>score_1<="1111111";
when "1001"=>score_1<="1110011";
when others=>score_1<="0000000";
end case;
end process;
end behave;
本文还有很多地方待完善,如有问题,也希望各位能够指出