赞
踩
采用app通过蓝牙模块来操作蛇的移动情况
reg[18:0] Snake[0:7]; //蛇 18:10 x 9:1 y 0 :flag 是否是蛇身
reg[17:0] Food; // 食物
//方向
always@(posedge clk or negedge rst)
begin
if(rst == 1'b0)
direct <= 2'b00;
else if(up == 1'b1 && direct != 2'b01)
direct <= 2'b00;
else if(down == 1'b1 && direct != 2'b00)
direct <= 2'b01;
else if(left == 1'b1 && direct != 2'b11)
direct <= 2'b10;
else if(right == 1'b1 && direct != 2'b10)
direct <= 2'b11;
else
direct <= direct;
end
//刷新蛇 18:10 x 9:1 y
else if(speed >= SPEED)
begin
case(direct)
2'b00:Snake[0][9:1] <= Snake[0][9:1] - 'd25;
2'b01:Snake[0][9:1] <= Snake[0][9:1] + 'd25;
2'b10:Snake[0][18:10] <= Snake[0][18:10] - 'd25;
2'b11:Snake[0][18:10] <= Snake[0][18:10] + 'd25;
endcase
for(i = 1;i<= sub_index ;i=i+1)
Snake[i] <= Snake[i-1];
end
//生成食物
always@(posedge clk or negedge rst)
begin
if(rst == 1'b0)
Food <= {9'd40,9'd50};
else if(eated == 1'b1)
Food <= {1'b0,Food[8]^Food[7]^Food[6]^Food[5],Food[7:1],1'b0,Food[16:10],Food[9]^Food[17]};
end
always@(posedge clk)
begin
if(cur_x >= Snake[0][18:10] && cur_x < Snake[0][18:10] + 'd25 && cur_y >= Snake[0][9:1] && cur_y < Snake[0][9:1]+'d25 && cur_y >= Snake[0][9:1] && cur_y < Snake[0][9:1]+'d25 && Snake[0][0] == 1'b1)
lcd_data_out = 24'hABCDEF;
else if(cur_x >= Snake[1][18:10] && cur_x < Snake[1][18:10] + 'd25 && cur_y >= Snake[1][9:1] && cur_y < Snake[1][9:1]+'d25 && cur_y >= Snake[1][9:1] && cur_y < Snake[1][9:1]+'d25 && Snake[1][0] == 1'b1)
lcd_data_out = 24'hFACDEB;
else if(cur_x >= Snake[2][18:10] && cur_x < Snake[2][18:10] + 'd25 && cur_y >= Snake[2][9:1] && cur_y < Snake[2][9:1]+'d25 && cur_y >= Snake[2][9:1] && cur_y < Snake[2][9:1]+'d25 && Snake[2][0] == 1'b1)
lcd_data_out = 24'hFACDEB;
else if(cur_x >= Snake[3][18:10] && cur_x < Snake[3][18:10] + 'd25 && cur_y >= Snake[3][9:1] && cur_y < Snake[3][9:1]+'d25 && cur_y >= Snake[3][9:1] && cur_y < Snake[3][9:1]+'d25 && Snake[3][0] == 1'b1)
lcd_data_out = 24'hFACDEB;
else if(cur_x >= Snake[4][18:10] && cur_x < Snake[4][18:10] + 'd25 && cur_y >= Snake[4][9:1] && cur_y < Snake[4][9:1]+'d25 && cur_y >= Snake[4][9:1] && cur_y < Snake[4][9:1]+'d25 && Snake[4][0] == 1'b1)
lcd_data_out = 24'hFACDEB;
else if(cur_x >= Snake[5][18:10] && cur_x < Snake[5][18:10] + 'd25 && cur_y >= Snake[5][9:1] && cur_y < Snake[5][9:1]+'d25 && cur_y >= Snake[5][9:1] && cur_y < Snake[5][9:1]+'d25 && Snake[5][0] == 1'b1)
lcd_data_out = 24'hFACDEB;
else if(cur_x >= Snake[6][18:10] && cur_x < Snake[6][18:10] + 'd25 && cur_y >= Snake[6][9:1] && cur_y < Snake[6][9:1]+'d25 && cur_y >= Snake[6][9:1] && cur_y < Snake[6][9:1]+'d25 && Snake[6][0] == 1'b1)
lcd_data_out = 24'hFACDEB;
else if(cur_x >= Food[17:9] && cur_x < Food[17:9] + 'd25 && cur_y >= Food[8:0] && cur_y < Food[8:0]+'d25 && cur_y >= Food[8:0] && cur_y < Food[8:0]+'d25)
lcd_data_out = 24'hFACDEB;
else
lcd_data_out = 24'hffffff;
end
效果图如下 下载链接
关注微信公众号 FPGA之旅 回复 FPGA贪吃蛇免费获取
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。