We use cookies. This helps us run our website and give you a better experience. By using our site, you agree to our cookie policy.

Snake Game Command Prompt Code

snake.append(new_head)

import random import time import os

Here's the complete code for the Snake game: snake game command prompt code

# Main game loop while True: draw_board() handle_input() update_game_state() time.sleep(0.1) WIDTH - 1)

# Clear the screen def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear') y) in snake: print('#'

if snake[-1] == food: food = (random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1)) else: snake.pop(0)

# Draw the game board def draw_board(): clear_screen() for y in range(HEIGHT): for x in range(WIDTH): if (x, y) in snake: print('#', end=' ') elif (x, y) == food: print('*', end=' ') else: print(' ', end=' ') print()

Scroll to top