diff --git a/mato8.asm b/mato8.asm index 1953b0f..28b5cc7 100644 --- a/mato8.asm +++ b/mato8.asm @@ -16,9 +16,11 @@ define direction_down 6 define direction_left 12 define direction_right 18 -define game_over_reg v9 -define fruit_x_reg va -define fruit_y_reg vb +define game_over_reg v7 +define fruit_x_reg v8 +define fruit_y_reg v9 +define tail_x_reg va +define tail_y_reg vb define head_direction_reg vc define head_x_reg vd define head_y_reg ve @@ -30,6 +32,9 @@ start: rnd head_x_reg, 63 rnd head_y_reg, 31 + ld tail_x_reg, head_x_reg + ld tail_y_reg, head_y_reg + ld head_direction_reg, direction_right ld i, single_pixel @@ -131,25 +136,95 @@ turn_right: ret move_snake: + ; Save the direction the snake is moving at head's location + ld v0, head_x_reg + ld v1, head_y_reg + call direction_table_index ld v0, head_direction_reg - call unpack_direction + ld [i], v0 - ld v2, head_x_reg + ; Move the snake's head + call unpack_direction add head_x_reg, v0 ld v0, 63 and head_x_reg, v0 - - ld v3, head_y_reg add head_y_reg, v1 ld v0, 31 and head_y_reg, v0 + ; Draw head location and erase tail location ld i, single_pixel drw head_x_reg, head_y_reg, 1 se vf, 0 call collision - drw v2, v3, 1 + drw tail_x_reg, tail_y_reg, 1 + ; Load the direction the snake was moving at tail's location + ld v0, tail_x_reg + ld v1, tail_y_reg + call direction_table_index + ld v0, [i] + + ; Move the snake's tail + call unpack_direction + add tail_x_reg, v0 + ld v0, 63 + and tail_x_reg, v0 + add tail_y_reg, v1 + ld v0, 31 + and tail_y_reg, v0 + + ret + +direction_table_index: + ; v0 is the X coördinate, from 0 to 63, 00xxxxxx + ; v1 is the Y coördinate, from 0 to 31, 000yyyyy + shl v0, v0 + shl v0, v0 + ; v0 is the X coördinate shifted left by two, xxxxxx00 + ld v2, %11100000 + and v2, v0 + or v1, v2 + ; v1 is combination of high 3 bits of X coördinate and the Y coördinate, xxxyyyyy + ld v2, %00011100 + and v0, v2 + ; v0 is the low 3 bits of the X coördinate shifted left by two, 000xxx00 + + jp v0, direction_table_index_table + direction_table_index_table: + ; 0 + ld i, #400 + jp direction_table_index_end + + ; 1 + ld i, #500 + jp direction_table_index_end + + ; 2 + ld i, #600 + jp direction_table_index_end + + ; 3 + ld i, #700 + jp direction_table_index_end + + ; 4 + ld i, #800 + jp direction_table_index_end + + ; 5 + ld i, #900 + jp direction_table_index_end + + ; 6 + ld i, #a00 + jp direction_table_index_end + + ; 7 + ld i, #b00 + +direction_table_index_end: + add i, v1 ret unpack_direction: