For a script that I added on my classroom's TI-84 Plus CE: Python Edition calculators, I'm proud of this script, but help me improve it, please.
Please note that there is no animation included within it, but is a text-based snake game. I'm planning on adding animation later. ```python # Credits: Aratox # # TO PLAY: # # - Press the "trace" button # - 8 - UP # - 2 - DOWN # - 4 - LEFT # - 6 - RIGHT # # Good luck! import random password = input("Enter the password to play the game: ") correct_password = "0000" if password != correct password: print("Wrong password.") exit() snake = [(1, 1)] food = (5, 5) UP = (-1, 0) DOWN = (1, 0) LEFT = (0, -1) RIGHT = (0, 1) direction = RIGHT def move(snake, direction): head = snake[-1] new_head = (head[0] + direction[0], head[1] + direction[1]) if new_head == food: snake.append(food) generate_food() else: snake.append(new_head) snake.pop(0) def generate_food(): global food food = (random.randint(1, 9), random.randint(1, 9)) while food in snake: food = (random.randint(1, 9), random.randint(1,9)) def check_collision(): head = snake[-1] if head[0] < 1 or head[0] > 9 or head[1] < 1 or head[1] > 9: return True if len(snake) != len(set(snake)): return True return False def print_board(): for i in range(1, 11): for j in range(1, 11): if (i, j) in snake: print("X", end=" ") elif (i, j) == food: print("*", end=" ") else: print("." end=" ") print() while True: print_board() key = input() if key == "8": if direction != DOWN: direction = UP elif key == "2": if direction != UP: direction = DOWN elif key == "4": if direction != RIGHT: direction = LEFT elif key == "6": if direction != LEFT: direction = RIGHT else: break move(snake, direction) if check_collision(): print("Game Over! Press 5 to restart.") restart_key = input() if restart_key == "5": snake = [(1, 1)] generate_food() direction = RIGHT continue else: break ```
I also forgot to mention that typing this on the calculator took me around 2 hours... and this is only 100 lines of code ðŸ«
you've done a great job on this game, especially considering that you typed it all on a calculator. Keep up the good work!
Your code would fail right off the bat. ``` if password != correct password: ```
... What happened with the embed lol 😨
You likely added an extra `
Also, your code would cause the board to be printed multiple times. I'd recommend clearing, or removing x amount of lines that are in relation to the board, and re-printing it in the same spot. Gives it an animation effect, of sorts.
If you're going for actual movement of the board, while an input exists, it won't really work.
If you `import os`, you can do the following two clear the console each time, if you'd prefer. ```python os.system('clear') ```
it hurts my brain
good :)
Join our real-time social learning platform and learn together with your friends!