Ask your own question, for FREE!
Computer Science 114 Online
Aratox:

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.

Aratox:

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 ```

Aratox:

I also forgot to mention that typing this on the calculator took me around 2 hours... and this is only 100 lines of code 🫠

toga:

you've done a great job on this game, especially considering that you typed it all on a calculator. Keep up the good work!

Aratox:

@toga wrote:
you've done a great job on this game, especially considering that you typed it all on a calculator. Keep up the good work!
Thank you!

toga:

@aratox wrote:
@toga wrote:
you've done a great job on this game, especially considering that you typed it all on a calculator. Keep up the good work!
Thank you!
no problem

Extrinix:

Your code would fail right off the bat. ``` if password != correct password: ```

Aratox:

@extrinix wrote:
Your code would fail right off the bat. ``` if password != correct password: ```
💀 Sorry, I typed the code out above by hand, but the code on the calculator has an underscore in between "correct" and "password", so the corrected code there should say: ``` if password != correct_password: ``` Thanks for catching that, by the way.

Aratox:

... What happened with the embed lol 😨

Extrinix:

You likely added an extra `

Extrinix:

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.

Extrinix:

If you're going for actual movement of the board, while an input exists, it won't really work.

Aratox:

@extrinix wrote:
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.
Yes, I know that the board would be printed multiple times, I've already run the script several times on the calculators - also, I'll try what you recommended.

Extrinix:

If you `import os`, you can do the following two clear the console each time, if you'd prefer. ```python os.system('clear') ```

Aratox:

@extrinix wrote:
If you `import os`, you can do the following two clear the console each time, if you'd prefer. ``` os.system('clear') ```
Keep in mind, this is a TI-84 Plus CE: Python Edition calculator, so it has no OS to speak of. I've tried inputting the code already, but got the following error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "SNAKE.py", line 13, in <module> ImportError: no module named 'os'

whyjustwhy:

it hurts my brain

hannahj:

good :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!