I am new to FPGA programming and I am using HDL Verilog (Using Vivado)
to generate a game similar to geometry dash.
I've written a code such that whilst the button is being pressed the
block jumps up and down in the same x position. Once the button is
released, the floor changes to 32 pixels above the initial floor and
this stays as the new floor position.
However, this was not what was trying to achieve. My code makes it so
the floor position is dictated by whether the button is being pressed or
not. I was trying to make it such that when the button is pressed the
block keeps jumping and if a certain condition is met (i.e. The floor
position changes) then the block starts jumping from the new floor
position and if the button is released the block rests on the new floor
position (exactly like the platforming in geometry dash).
I can see that I need to make the floor position and button press
independent but I have no idea how to do it. Any guidance is appreciated
My code referring to the jumping logic:
1 | module jumping#(parameter
|
2 | FLR_POS = 11'd696,
|
3 | BLK_HGT = 11'd32,
|
4 | INL_BLK_X = 11'd300,
|
5 | INL_BLK_Y = FLR_POS - BLK_HGT,
|
6 | SPD = 11'd32,
|
7 | ACCEL = 11'd2)(
|
8 |
|
9 | //input pix_clk,
|
10 | input game_clk,
|
11 | input [4:0] btn,
|
12 | output [10:0] blkpos_x,
|
13 | output [10:0] blkpos_y
|
14 | );
|
15 |
|
16 | reg [10:0] floor = 11'd696;
|
17 | reg [10:0] blkpos_x_r = 11'd300;
|
18 | reg [10:0] blkpos_y_r = 11'd664;
|
19 | reg jump_state;
|
20 | reg [10:0] spd_y = 11'd32;
|
21 | reg [10:0] acc_y = 11'd2;
|
22 |
|
23 |
|
24 | always@(posedge game_clk) begin
|
25 | if(btn[1]) begin
|
26 | jump_state <= 1;
|
27 | end
|
28 | else if ((btn[1]==0) && (blkpos_y_r + BLK_HGT >= (floor - 11'd32))) begin
|
29 | jump_state <= 0;
|
30 | end
|
31 | end
|
32 |
|
33 | always@(posedge game_clk) begin
|
34 |
|
35 | if(btn[0]) begin
|
36 | blkpos_x_r <= 11'd503;
|
37 | blkpos_y_r <= FLR_POS - BLK_HGT;
|
38 | end
|
39 |
|
40 | else if(jump_state == 1) begin
|
41 | blkpos_y_r <= blkpos_y_r - spd_y;
|
42 | spd_y <= spd_y - acc_y;
|
43 |
|
44 | if (spd_y == -SPD - acc_y) begin
|
45 | spd_y <= SPD;
|
46 | blkpos_y_r <= blkpos_y_r;
|
47 | end
|
48 |
|
49 | if (blkpos_y_r + BLK_HGT >= (FLR_POS - 11'd32)) begin
|
50 | floor <= floor - 11'd32;
|
51 | end
|
52 | end
|
53 |
|
54 | else if(jump_state == 0) begin
|
55 | blkpos_y_r <= blkpos_y_r;
|
56 | spd_y = SPD;
|
57 | acc_y = ACCEL;
|
58 | end
|
59 | end
|
60 |
|
61 | assign blkpos_x = blkpos_x_r;
|
62 | assign blkpos_y = blkpos_y_r;
|
63 |
|
64 | endmodule
|