0% found this document useful (0 votes)
172 views

Code

The document describes a snake game module with the following functionality: 1) It controls the movement, speed, and length of the snake based on the game level. The length increases when the snake eats food. 2) It generates a new random location for food when the snake's head reaches the food. 3) It checks for collisions with the snake's body or boundaries to determine if the game is over. 4) Additional modules handle the snake movement based on button inputs, display the game world graphics, and generate dynamic text.

Uploaded by

Junaid Ghouri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

Code

The document describes a snake game module with the following functionality: 1) It controls the movement, speed, and length of the snake based on the game level. The length increases when the snake eats food. 2) It generates a new random location for food when the snake's head reaches the food. 3) It checks for collisions with the snake's body or boundaries to determine if the game is over. 4) Additional modules handle the snake movement based on button inputs, display the game world graphics, and generate dynamic text.

Uploaded by

Junaid Ghouri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

module bot_icon (

input [7:0] pixel_col,


input [7:0]pixel_row,
input [7:0] LocX_reg,
input [7:0] LocY_reg,
input [7:0] Food_locX,
input [7:0] Food_locY,
input clk,
input reset,
input icon_tick,
input ctrl_off,
output reg [2:0] icon,
output reg [7:0]s_len = 8'd10,
output reg game_over = 0,
output reg food_gen = 0
);
if (ctrl_off ==0 &&(Food_locX > 8'h89 || Food_locY > 8'h6D || Food_locX
< 8'h11 ||
Food_locY < 8'h0B))
if(ctrl_off == 0 && body_stack[0]=={Food_locX,Food_locY}) begin //Check
if the head
location of the snake is eual tothe location of the food
food_gen = 1; //If the above condition is met the food_gen is set high
to
generate the new location for the food
/*The levels of the snake game are based on the updating length of the
snake !*/
if( s_len == 8'd8 ) /*Contion to jump from lvl - 1 to lvl - 2*/
s_len = s_len + 8'd5;
else if(s_len == 14) /*Contion to jump from lvl - 2 to lvl - 3*/
s_len = s_len + 8'd10;
else if( s_len == 25) /*Contion to jump from lvl - 3 to You Won !*/
s_len = s_len + 8'd15;
else
s_len = s_len + 8'd1; /*When the snake is in a particular level its body
increments by 1 when it eats food */
end
else begin
food_gen = 0;
end
body_stack[0]=={Food_locX,Food_locY}
for(j=100;j>=1;j=j-1) begin
if( (body_stack[0] == body_stack[j]) &&(j <= s_len))begin
game_over = 1;
end
else begin
j=j;
end
end
if ( (s_len >= 8'd40)||( body_stack[0][15:8] == 8'h91 )||
( body_stack[0][15:8]
== 8'h10 )|| ( body_stack[0][7:0] == 8'h6F )||( body_stack[0][7:0] ==
8'h0A ) )
game_over = 1;
end
Snake Movement
module snake_movement(
input clk,
input reset,
input game_over,
input db_btn_west,
input db_btn_east,
input db_btn_north,
input db_btn_south,
output [7:0] LocSX,
output [7:0] LocSY,
input [7:0] s_len,
output reg icon_tick = 0,
output reg ctrl_off=0
);
always @(posedge icon_tick) begin
if(s_len >= 8'd6 && s_len <= 8'd8)begin
snake_speed = snake_cnt1;
end
else if(s_len >= 8'd13 && s_len <= 8'd14) begin
snake_speed = snake_cnt2;
end
else begin
snake_speed = snake_cnt3;
end
……………………………………………………………..
……………………………………………………………..
End
if ( user_inputs == 4'b0010 && current_heading != 4'b0001 &&
current_heading !=
4'b0010)begin
current_heading = 4'b0010;
end
else if ( user_inputs == 4'b0001 && current_heading != 4'b0001 &&
current_heading
!= 4'b0010)begin
current_heading = 4'b0001;
end
else if ( user_inputs == 4'b0100 && current_heading != 4'b0100 &&
current_heading
!= 4'b1000)begin
current_heading = 4'b0100;
end
else if ( user_inputs == 4'b1000 && current_heading != 4'b0100 &&
current_heading
!= 4'b1000)begin
current_heading = 4'b1000;
end
if(reset) begin
LocSX_reg = 8'h40;
LocSY_reg = 8'h40;
end
else begin
case(current_heading)
4'b0001: LocSX_reg = LocSX_reg - 1;
4'b0010: LocSX_reg = LocSX_reg + 1;
4'b0100: LocSY_reg = LocSY_reg - 1;
4'b1000: LocSY_reg = LocSY_reg + 1;
endcase
end
Handling meta-stability:
The game over flag is set high initially while resetting the game.This has been handled as
follows
always @ (posedge game_over or posedge reset) begin
if (reset) begin
ctrl_off = 0;
k = 0;
end
else begin
k = k + 1;
if(game_over && (k > 1))
ctrl_off = 1;
end
end
Snake World
module snake_world(
input [7:0] pixel_col,
input [7:0] pixel_row,
input rand_en,
input ctrl_off,
input [7:0] s_len,
output reg [7:0] LocX = 8'h43,
output reg [7:0] LocY = 8'h43,
output reg [1:0] food_icon,
output reg [1:0] snake_world_pixels
);
Dynamic Text generation:
if (pixel_col > 8'h30 && pixel_col < 8'h3D && pixel_row > 8'h23 &&
pixel_row <
8'h27) begin
snake_world_pixels = 2'b10;
end
else if (pixel_col > 8'h30 && pixel_col < 8'h34 && pixel_row > 8'h26 &&
pixel_row < 8'h2D) begin
snake_world_pixels = 2'b10;
end
else if (pixel_col > 8'h30 && pixel_col < 8'h3D && pixel_row > 8'h2C &&
pixel_row < 8'h30) begin
snake_world_pixels = 2'b10;
end
else if (pixel_col > 8'h39 && pixel_col < 8'h3D && pixel_row > 8'h29 &&
pixel_ow < 8'h2D) begin
snake_world_pixels = 2'b10;
end
Random generation of locations for food
always@(*) begin
locX_reg = ((x + 4271)*4273 - 9973*3)*57;
locY_reg = ((y + 3343)*3347 - 9857*3)*55;
x = locX_reg;
y = locY_reg;
end
http://wenku.baidu.com/view/92c0
56222f60ddccda38a097.html

You might also like