module ball_pos(b_clk,b_rst,ball_posx,ball_posy,p1_pos,p2_pos,p1_size,p2_size);
input b_clk,b_rst;
input [9:0]p1_pos,p2_pos;
input [7:0]p1_size,p2_size;
output [10:0]ball_posx;
output [9:0]ball_posy;
reg [10:0]ball_posx;
reg [9:0]ball_posy;
reg [1:0]direction;//0:x,1:y//0:+,1:-

always@(posedge b_clk,negedge b_rst)
begin
    if(!b_rst)
    begin
        ball_posx<=11'd305;
        ball_posy<=10'd100;
        direction<=2'd0;
    end
    else
    begin
        //bounce
        if(ball_posy<=3)
            direction[1]<=0;
        if((ball_posy+`Ball_size)>=600)
            direction[1]<=1;
        if(ball_posx==62&&(ball_posy+`Ball_size)>=p1_pos&&ball_posy<(p1_pos+p1_size))
            direction[0]<=0;
        if((ball_posx+`Ball_size)==962&&(ball_posy+`Ball_size)>=p2_pos&&ball_posy<(p2_pos+p2_size))//player 2 defend
            direction[0]<=1;
        //ball move
        ball_posx<=ball_posx+((direction[0])? -1:1);
        ball_posy<=ball_posy+((direction[1])? -1:1);
    end
end
endmodule