Ball Control
From Ece
// ball_control.v // Created by Justin Davis, January 2005 // Modified by Patrick Duckworth, April 2007 // Target board: Pegasus by Digilent, Inc. // // Controls the location of the ball // Output the X and Y coordinates where the ball should be drawn // Determines when it has hit a wall or paddle and act accordingly //
module ball_control (clk, paddleY, ballX, ballY,lives,score); input clk; input [9:0] paddleY; output [9:0] ballX, ballY; output [7:0] lives; output [7:0] score;
reg x,y;
reg [16:0] counter; reg [10:0] rand;
parameter WALLWIDTH = 20;
parameter TOTALROWS = 479;
parameter TOTALCOLS = 639;
parameter PADDLEWIDTH = 20;
parameter PADDLELENGTH = 80;
parameter BALL = 20;
reg ballX; reg ballY; reg lives; reg score;
initial begin ballX = 10'b0100000000;
ballY = 10'b0100000000; x=0;
y=1; lives = 8'b00000100; score = 8'b00000000; end
always@(posedge clk)
begin
if (lives >= 1)
begin
counter = counter+1;
end
else
begin
counter = 16'b0000000000000001;
end
// Pseudo randomization of bomb placement
if (rand >= 11'b01001000000)
begin
rand = 11'b00000000000;
end
if (rand == 11'b00000000000)
begin
rand = 11'b00000100000;
end
rand = rand+1;
// Bomb control if (counter == 16'b0000000000000000) begin // If bomb hits paddle if (((ballY+BALL) > (TOTALROWS-PADDLEWIDTH)) && ((ballX+BALL) >= paddleY) && (ballX <= (paddleY+PADDLELENGTH))) begin x = 1; y = 0; score = score+1; end
// If bomb misses paddle else if ((ballY+BALL) > (TOTALROWS-PADDLEWIDTH) || ballX <=0 || ballX >= (TOTALCOLS-BALL)) begin // if paddle misses bomb subtract a life if ((ballY+BALL) > (TOTALROWS-PADDLEWIDTH)) begin lives = lives - 1; end
// set the next bomb placement ballX=rand; x=0; y=1; ballY = 10'b0000010101; end
// bomb movement if (y==1) begin ballY = ballY+1; end else begin ballY = ballY-1; end if (x==1) begin if (ballX >= paddleY+((PADDLELENGTH-BALL)/2)) begin ballX = ballX+2; end else begin ballX = ballX-2; end end end end endmodule




