📄 ball.vhd
字号:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity ball is
Port ( clk : in std_logic;
reset,start : in std_logic;
hcnt : in integer;
vcnt : in integer;
ball_x : buffer integer;
ball_y : buffer integer;
ball_rgb: out std_logic_vector(2 downto 0);
board_x : buffer integer );
end ball;
architecture Behavioral of ball is
----------------------------------------------------------------------
CONSTANT BOARD_WIDTH : INTEGER :=20;
CONSTANT BOARD_LEN : INTEGER :=50;--THIE IS THE 1/2 LENGTH OF BOARD
CONSTANT BALL_R : INTEGER :=5;
CONSTANT BOARD_X0:INTEGER :=320;
CONSTANT BOARD_Y0:INTEGER :=400;
CONSTANT BRICK_WIDTH : INTEGER :=40;
CONSTANT BRICK_LEN : INTEGER :=50;
CONSTANT BRICK_AX: INTEGER :=130;
CONSTANT BRICK_BX: INTEGER :=320;
CONSTANT BRICK_CX: INTEGER :=510;
CONSTANT BRICK_Y : INTEGER :=100;
----------------------------------------------------------------------
signal movclk : std_logic;
signal addx,addy : integer;
begin
drawball: process (clk)
begin
if (clk'event and clk='1') then
if ((hcnt>=(ball_x-5)) and (hcnt<=(ball_x+5)) and (vcnt>=(ball_y-2)) and (vcnt<=(ball_y+2))) or
((hcnt>=(ball_x-2)) and (hcnt<=(ball_x+2)) and (vcnt>=(ball_y-5)) and (vcnt<=(ball_y+5))) or
((hcnt>=(ball_x-3)) and (hcnt<=(ball_x+3)) and (vcnt>=(ball_y-4)) and (vcnt<=(ball_y+4))) or
((hcnt>=(ball_x-4)) and (hcnt<=(ball_x+4)) and (vcnt>=(ball_y-3)) and (vcnt<=(ball_y+3))) then
ball_rgb <= "100";
else
ball_rgb <= "000";
end if;
end if;
end process;
ballmove: process (movclk,reset)
begin
if reset='0' then
ball_x <= BOARD_X0;
ball_y <= BOARD_Y0 - BOARD_WIDTH - BALL_R;
elsif (movclk'event and movclk='1') then
if start='1' and ball_y<=480 then ---avoid overbrim
ball_x <= ball_x+addx;
ball_y <= ball_y+addy;
else
ball_x <=ball_x;
ball_y <=ball_y;
end if;
end if;
end process;
movable: process (movclk,reset)
begin
if reset='0' then
addx<=1;
addy<=-1; ---x,y increase 1, 45degree
elsif (movclk'event and movclk='1') then
if addy>0 then
if (( (BRICK_Y-BRICK_WIDTH-ball_y )<=BALL_R ) and ( ball_x>=(BRICK_AX - BRICK_LEN) ) and ( ball_x<=(BRICK_AX+BRICK_LEN) )) or
(( (BRICK_Y-BRICK_WIDTH-ball_y) <=BALL_R ) and ( ball_x>=(BRICK_BX - BRICK_LEN) ) and ( ball_x<=(BRICK_BX+BRICK_LEN) )) or
(( (BRICK_Y-BRICK_WIDTH-ball_y) <=BALL_R ) and ( ball_x>=(BRICK_CX - BRICK_LEN) ) and ( ball_x<=(BRICK_CX+BRICK_LEN) ))
then
addy<=-1;
elsif (BOARD_Y0+BOARD_WIDTH-ball_y <=BALL_R) and (ball_x>=board_x- BOARD_LEN/5) and (ball_x<=board_x + BOARD_LEN/5) then
addy<=-2;
addx<=0; -----90 degree rebound
elsif (BOARD_Y0+BOARD_WIDTH-ball_y <=BALL_R) and (ball_x>=board_x- 3*BOARD_LEN/5) and (ball_x<board_x - BOARD_LEN/5) then
addx<=-1;
addy<=-1; ------45 degree rebound to left
elsif (BOARD_Y0+BOARD_WIDTH-ball_y <=BALL_R) and (ball_x>=board_x- BOARD_LEN) and (ball_x<board_x -3*BOARD_LEN/5) then
addx<=-2;
addy<=-1; ------30 degree rebound to left
elsif (BOARD_Y0+BOARD_WIDTH-ball_y <=BALL_R) and (ball_x>board_x+ BOARD_LEN/5) and (ball_x<board_x +3*BOARD_LEN/5) then
addx<=1;
addy<=-1; ------45 degree rebound to right
elsif (BOARD_Y0+BOARD_WIDTH-ball_y <=BALL_R) and (ball_x>=board_x- BOARD_LEN) and (ball_x<board_x -3*BOARD_LEN/5) then
addx<=2;
addy<=-1; ------30 degree rebound to right
end if;
elsif addy<0 then
if ((ball_y-BRICK_Y-BRICK_WIDTH <=BALL_R) and (ball_x>=BRICK_AX - BRICK_LEN) and (ball_x<=BRICK_AX+BRICK_LEN)) or
((ball_y-BRICK_Y-BRICK_WIDTH <=BALL_R) and (ball_x>=BRICK_BX - BRICK_LEN) and (ball_x<=BRICK_BX+BRICK_LEN)) or
((ball_y-BRICK_Y-BRICK_WIDTH<=BALL_R) and (ball_x>=BRICK_CX - BRICK_LEN) and (ball_x<=BRICK_CX+BRICK_LEN)) or
ball_y<=BALL_R
then
addy<=1;
end if;
end if;
if addx>0 then
if (( (BRICK_AX-BRICK_WIDTH-ball_x )<=BALL_R ) and ( ball_y>=(BRICK_Y - BRICK_LEN- BALL_R) ) and ( ball_y<=(BRICK_Y+BRICK_LEN+ BALL_R) )) or
(( (BRICK_BX-BRICK_WIDTH-ball_x) <=BALL_R ) and ( ball_y>=(BRICK_Y - BRICK_LEN- BALL_R) ) and ( ball_y<=(BRICK_Y+BRICK_LEN+ BALL_R) )) or
(( (BRICK_CX-BRICK_WIDTH-ball_x) <=BALL_R ) and ( ball_y>=(BRICK_Y - BRICK_LEN- BALL_R) ) and ( ball_y<=(BRICK_Y+BRICK_LEN+ BALL_R) )) or
ball_x<=640-BALL_R
then
addx<=-1;
end if;
elsif addx<0 then
if ((ball_x-BRICK_AX-BRICK_LEN <=BALL_R) and (ball_y>=BRICK_Y - BRICK_WIDTH - BALL_R) and (ball_y<=BRICK_Y+BRICK_WIDTH + BALL_R)) or
((ball_x-BRICK_BX-BRICK_LEN <=BALL_R) and (ball_y>=BRICK_Y - BRICK_WIDTH - BALL_R) and (ball_y<=BRICK_Y+BRICK_WIDTH + BALL_R)) or
((ball_x-BRICK_CX-BRICK_LEN <=BALL_R) and (ball_y>=BRICK_Y - BRICK_WIDTH - BALL_R) and (ball_y<=BRICK_Y+BRICK_WIDTH + BALL_R)) or
ball_x<=BALL_R
then
addx<=1;
end if;
end if;
end if;
end process;
end Behavioral;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -