logo
down
shadow

Snake game using FPGA in VHDL


Snake game using FPGA in VHDL

By : user2951603
Date : November 18 2020, 11:13 AM
I wish did fix the issue. First of all, I recommend you split your display and your position update functionality into separate processes to make things clearer.
You seem to already have mostly everything you need for your display process, except that, as Paebbels mentions in his comment, this should be a synchronous (i.e. clocked) process. I assume your clock is named clk.
code :
PROCESS(clk)
BEGIN
    IF rising_edge(clk) THEN
        --display time 
        IF(disp_ena = '1') THEN 
            -- print the border                
            IF (column=19 or column=1901 or row=19 or row=1181) THEN
                red <= (OTHERS => '1'); 
                green   <= (OTHERS => '0');
                blue <= (OTHERS => '0');
            ELSE
                red <= (OTHERS => '0');
                green   <= (OTHERS => '0');
                blue <= (OTHERS => '0');
            END IF;

            IF (DRAW = '1') THEN   
                red <= (OTHERS => '1');
                green   <= (OTHERS => '1');
                blue <= (OTHERS => '1');
            END IF;
        END IF;
    END IF;
END PROCESS;
type direction_t is (DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT);
signal direction : direction_t;
process(clk) begin
    if rising_edge(clk) then
        if rst = '1' then
            direction <= DIR_UP;
        else
            if up = '0' then
                direction <= DIR_UP;
            elsif down = '0' then
                direction <= DIR_DOWN;
            elsif left = '0' then
                direction <= DIR_LEFT;
            elsif right = '0' then
                direction <= DIR_RIGHT;
            end if;
        end if;
    end if;
end process;
signal update_ce : std_logic;

-- (...)

process(clk) begin
    if rising_edge(clk) then
        -- adapt the value to your taste
        if disp_ena = '1' and column = 1901 and row = 1181 then
            update_ce <= '1';
        else
            update_ce <= '0';
        end if;
    end if;
end process;
process(clk)
begin
    if rising_edge(clk) then
        if rst = '1' then
            -- adapt starting position to your taste
            SQ_X1<= 100;
            SQ_Y1<= 100;
        elsif update_ce = '1' then
            case direction is
            when DIR_UP =>
                if SQ_X1 - 1 > 20 then 
                    SQ_X1 <= SQ_X1 - 1;
                end if;

            when DIR_DOWN =>
                if SQ_X1 + 1 < 1180 then 
                    SQ_X1 <= SQ_X1 + 1;
                end if; 

            when DIR_LEFT =>
                if SQ_Y1 - 1 > 20 then 
                    SQ_Y1 <= SQ_Y1 - 1;
                end if;

            when others =>
            -- DIR_RIGHT
                if SQ_Y1 - 1 > 20 then 
                    SQ_Y1 <= SQ_Y1 - 1;
                end if;                    
            end case;
        end if;
    end if;
end process;


Share : facebook icon twitter icon
snake game on jquery/javascript when pressing key, snake moving faster

snake game on jquery/javascript when pressing key, snake moving faster


By : Mont'ster
Date : March 29 2020, 07:55 AM
like below fixes the issue Your setInterval is called inside your eventhandler. Move setinterval outside, along with a shared dir variable. Then you will have no need for clearinterval.
code :
var dir = 'bottom';
 setInterval(move = function() {
    var snake = $('.snake');
    var food = $('.food');
    if(dir == 'top') {
        snake.css({"top": $(".snake").position().top + 5 + "px"});
    }
    if(dir == 'bottom') {
        snake.css({"top": $(".snake").position().top - 5 + "px"});
    }
    if(dir == 'left') {
        snake.css({"left": $(".snake").position().left + 5 + "px"});
    }
    if(dir == 'right') {
        snake.css({"left": $(".snake").position().left - 5 + "px"});
    }
}, 150); 
$(document).keydown(function(event){
    if(event.which == 40) {
        dir = 'top';
    } else if(event.which == 39) {
        dir = 'left';           
    } else if(event.which == 37) {
        dir = 'right';        
    } else if(event.which == 38) {
        dir = 'bottom';    
    }; 
});
Creating a snake game, when you press two directions quickly snake eats itself [Java]

Creating a snake game, when you press two directions quickly snake eats itself [Java]


By : Vadik Tokarchuk
Date : March 29 2020, 07:55 AM
Does that help Based on your description, your code is changing the boolean variables before the move occurs. A simple solution would be to store all moves in a Queue, and process them by removing them, that way you ensure you won't overwrite a move.
In other words, every time you record a key event that would change the booleans, store some sort of signal (an int, a String, an enum, etc.) in a Queue, and in your move method, simply remove the signal from the front of the Queue and process it like you process the boolean variables. If you were to use an enum for UP DOWN RIGHT LEFT it would be fairly readable and you could use a switch-case to process each movement.
code :
switch (movement) {
    case UP: /* up code */ break;
    case LEFT: /* left code */ break;
    case RIGHT: /* right code */ break;
    case DOWN: /* down code */ break;
}
private enum Movement { UP, DOWN, RIGHT, LEFT }
Queue<Movement> movementQueue = new ArrayDeque<Movement>();
movementQueue.offer(UP); // or DOWN or RIGHT or LEFT, whichever you want.
Movement movement = movementQueue.poll();
Snake game using FPGA (ALTERA)

Snake game using FPGA (ALTERA)


By : Lary.Walker
Date : March 29 2020, 07:55 AM
I wish this helpful for you Let's say you have a LED matrix like this:
To answer not your question, but your comment about " if u can at least show me how to make the blinking LED i will be grateful :)", we can do as this:
code :
module blink (input wire clk,  /* assuming a 50MHz clock in your trainer */
              output wire anode,  /* to be connected to RC7 */
              output wire cathode);  /* to be connected to RB7 */

  reg [24:0] freqdiv = 25'h0000000;
  always @(posedge clk)
    freqdiv <= freqdiv + 1;
  assign cathode = 1'b0;
  assign anode = freqdiv[24];
endmodule
module runningdot (input wire clk,  /* assuming a 50MHz clock in your trainer */
       output wire [7:0] anodes,  /* to be connected to RC0-7 */
       output wire [7:0] cathodes);  /* to be connected to RB0-7 */

  reg [23:0] freqdiv = 24'h0000000;
  always @(posedge clk)
    freqdiv <= freqdiv + 1;
  wire clkled = freqdiv[23];

  reg [7:0] r_anodes = 8'b10000000;
  reg [7:0] r_cathodes = 8'b01111111;
  assign anodes = r_anodes;
  assign cathodes = r_cathodes;

  always @(posedge clkled) begin
    r_anodes <= {r_anodes[0], r_anodes[7:1]};  /* shifts LED left to right */
    if (r_anodes == 8'b00000001)  /* when the last LED in a row is selected... */
      r_cathodes <= {r_cathodes[0], r_cathodes[7:1]}; /* ...go to the next row */
  end
endmodule
Snake game using FPGA (NEXYS2)

Snake game using FPGA (NEXYS2)


By : Desire Aheza
Date : March 29 2020, 07:55 AM
seems to work fine Beyond designing and wiring up the hardware (connectors, LED matrices and such) and the actual design of the game (the rules and what inputs cause which outputs), you start by breaking down the design constraints into blocks which you then write as VHDL. For example, you might have a display output component that takes pixel change events and writes the screen state to the display (or maybe it takes the whole screen state, and only handles the interface logic to the display). You might have a game logic component, and a controller interface component.
Once you decide what each block should do, you have to decide how it will do that with data flow and control logic, which you write in VHDL (you need to know the basic syntax and functionality of VHDL first!).
pygame snake game, snake doesn't move when rapidly changing direction

pygame snake game, snake doesn't move when rapidly changing direction


By : reza98
Date : March 29 2020, 07:55 AM
help you fix your problem First of all, you should try to use a single main loop.
While you're rendering the start or end screen, you can't interact with the window because no event loop runs. It's very annoying that you can't move or close the window during that time. Maybe take a look here for an example of how to handle this.
code :
MOVE_SNAKE = pygame.USEREVENT
pygame.time.set_timer(MOVE_SNAKE, 300)

# main logic of the game
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:  # if user press any button
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                next_direction = 'RIGHT'
            elif event.key == pygame.K_LEFT or event.key == ord('a'):
                next_direction = 'LEFT'
            elif event.key == pygame.K_UP or event.key == ord('w'):
                next_direction = 'UP'
            elif event.key == pygame.K_DOWN or event.key == ord('s'):
                next_direction = 'DOWN'
            elif event.key == pygame.K_ESCAPE:  # if user choose to quit the game
                pygame.event.post(pygame.event.Event(pygame.QUIT))
            elif event.key == pygame.K_SPACE:
                draw_game_pause()
        elif event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOVE_SNAKE:
            # move snake head
            if direction == 'RIGHT':
                snake_head[0] += step
            elif direction == 'LEFT':
                snake_head[0] -= step
            elif direction == 'DOWN':
                snake_head[1] += step
            elif direction == 'UP':
                snake_head[1] -= step

            # move snake body mechanism
            #   1. insert a new block at the beginning of the body
            #   2. check if snake has reached a food
            #       2.1 if yes, then keep the modified body
            #       2.2 if not, then delete the end of the body
            snake_body.insert(0, list(snake_head))
            if snake_head[0] == food_pos[0] and snake_head[1] == food_pos[1]:
                food_spawn = False
                score += 1
            else:
                snake_body.pop()

    # validation of direction
    if next_direction == 'RIGHT' and direction != 'LEFT':
        direction = 'RIGHT'
    elif next_direction == 'LEFT' and direction != 'RIGHT':
        direction = 'LEFT'
    elif next_direction == 'DOWN' and direction != 'UP':
        direction = 'DOWN'
    elif next_direction == 'UP' and direction != 'DOWN':
        direction = 'UP'



    while food_spawn == False:
        food_pos = [random.randrange(2, screen_width / step - 1) * step,
                    random.randrange(2, screen_height / step - 1) * step]
        if get_food(food_pos, snake_body) == True:
            food_spawn = False
        else:
            food_spawn = True

    # fill game background
    game_screen.fill(white)

    # draw snake body
    for pos in snake_body:
        pygame.draw.rect(game_screen, green, pygame.Rect(pos[0], pos[1], step, step))

    # draw food
    pygame.draw.rect(game_screen, brown, pygame.Rect(food_pos[0], food_pos[1], step, step))

    # check if snake hits the border
    if (snake_head[0] > screen_width - step) or (snake_head[0] < 0) or \
            (snake_head[1] > screen_height - step) or (snake_head[1] < 0):
        draw_game_over()

    # check if snake hits itself
    for block in snake_body[1:]:
        if snake_head[0] == block[0] and snake_head[1] == block[1]:
            draw_game_over()

    new_level = score//5 + 1
    if new_level != level:
        pygame.time.set_timer(MOVE_SNAKE, 300 / new_level)
        if new_level <= 3:
            level = new_level

    show_score(game_over=False)
    pygame.display.flip()

    fps_controller.tick(60)
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org