Created
January 19, 2025 03:06
-
-
Save jake-87/2d3931a8ea883f08826f5db1f4e0fad8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
width = 20 | |
height = 20 | |
score = 0 | |
import os | |
import sys | |
import tty | |
import termios | |
import time | |
import select | |
import readline | |
def getch(): | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
if select.select([sys.stdin,],[],[],0.15)[0]: | |
ch = sys.stdin.read(1) | |
else: | |
ch = None | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
def myexit(*x): | |
print("crashed!") | |
print("your score was: ", score) | |
raise RuntimeError | |
class Snake(): | |
def __init__(self): | |
self.pos = [(10,10)] | |
def move(self, grow, pos): | |
if pos == 's': | |
x, y = self.pos[0] | |
if y == height - 1: | |
newy = 0 | |
else: | |
newy = y + 1 | |
if not(grow): | |
del self.pos[-1] | |
if self.in_at_all((x, newy)): | |
myexit(0) | |
self.pos.insert(0, (x, newy)) | |
elif pos == 'w': | |
x, y = self.pos[0] | |
if y == 0: | |
newy = height - 1 | |
else: | |
newy = y - 1 | |
if not(grow): | |
del self.pos[-1] | |
if self.in_at_all((x, newy)): | |
myexit(0) | |
self.pos.insert(0, (x, newy)) | |
elif pos == 'a': | |
x, y = self.pos[0] | |
if x == 1: | |
newx = width - 2 | |
else: | |
newx = x - 1 | |
if not(grow): | |
del self.pos[-1] | |
if self.in_at_all((newx, y)): | |
myexit(0) | |
self.pos.insert(0, (newx, y)) | |
elif pos == 'd': | |
x, y = self.pos[0] | |
if x == width - 2: | |
newx = 1 | |
else: | |
newx = x + 1 | |
if not(grow): | |
del self.pos[-1] | |
if self.in_at_all((newx, y)): | |
myexit(0) | |
self.pos.insert(0, (newx, y)) | |
else: | |
raise RuntimeError | |
def in_snek(self, coord): | |
return coord in self.pos | |
def in_head(self, coord): | |
return self.pos[0] == coord | |
def in_at_all(self, coord): | |
return self.in_snek(coord) or self.in_head(coord) | |
def print_board(snake, apple, board): | |
s = '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' | |
s += 'W' * 2 * width | |
s += '\n' | |
for y in range(len(board)): | |
for x in range(len(board[y])): | |
if snake.in_head((x,y)): | |
s += 'S' | |
elif snake.in_snek((x,y)): | |
s += '=' | |
elif (x,y) == apple: | |
s += '' | |
else: | |
if x == 0: | |
s += 'W' | |
elif x == width - 1: | |
s += ' W' | |
else: | |
s += ' ' | |
s += ' ' | |
s += '\n' | |
s += 'W' * 2 * width | |
s += '\nq to quit\n' | |
global score | |
s += 'score is ' + str(score) + '\n' | |
print(s) | |
import random | |
def gen_apple(snake): | |
x = 0 | |
y = 0 | |
while True: | |
x = random.randint(2,width - 2) | |
y = random.randint(2,height - 2) | |
if not (snake.in_at_all((x,y))): | |
return (x,y) | |
while True: | |
board = [[0 for i in range(width)] for i in range(height)] | |
s = Snake() | |
s.move(True,'d') | |
s.move(True,'d') | |
s.move(True,'d') | |
s.move(True,'d') | |
apple = gen_apple(s) | |
grow = False | |
print_board(s, apple, board) | |
c = 'd' | |
while True: | |
while True: | |
k = getch() | |
if k == 'w' and c == 's': | |
pass | |
elif k == 's' and c == 'w': | |
pass | |
elif k == 'a' and c == 'd': | |
pass | |
elif k == 'd' and c == 'a': | |
pass | |
else: | |
oldc = c | |
c = k if k != None else c | |
if c == 'w' or c == 'a' or c == 's' or c == 'd': | |
break | |
elif c == 'q': | |
print("your score was: ", score) | |
break | |
else: | |
c = oldc | |
try: | |
s.move(grow, c) | |
except RuntimeError: | |
break | |
if s.in_at_all(apple): | |
grow = True | |
score += 1 | |
apple = gen_apple(s) | |
else: | |
grow = False | |
print_board(s, apple, board) | |
print("thanks for playing! play again?") | |
if input('[y/n]: ') != 'y': | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment