Add basic game structure and cli

This commit is contained in:
Felix Bargfeldt 2019-04-07 18:00:09 +02:00
parent cc2f0f012b
commit 15b26df206
No known key found for this signature in database
GPG key ID: 99184F5FDC589A67
5 changed files with 110 additions and 0 deletions

21
console_client.py Normal file
View file

@ -0,0 +1,21 @@
from os import get_terminal_size
from game import Game
from getkey import getkey
from snake import Snake
wid, hei = get_terminal_size()
game: Game = Game(wid, hei, Snake("Snake", (0, 255, 0)))
key: str = getkey()
while key not in "\x03\x04\x1aqe":
if key in ["\033[A", "w", "k"]: # UP
print("up")
elif key in ["\033[B", "s", "j"]: # DOWN
print("down")
elif key in ["\033[C", "d", "l"]: # RIGHT
print("right")
elif key in ["\033[D", "a", "h"]: # LEFT
print("left")
key: str = getkey()

1
directions.py Normal file
View file

@ -0,0 +1 @@
NORTH, EAST, SOUTH, WEST = range(4)

16
game.py Normal file
View file

@ -0,0 +1,16 @@
from snake import Snake
class Game:
def __init__(self, width: int, height: int, snake: Snake):
self.width: int = width
self.height: int = height
self.snake: Snake = snake
self.register_snake()
def register_snake(self):
assert self.width > 4 and self.height > 2, "Board is too small."
self.snake.register(self, [(2 + i, 2) for i in range(3)])
def tick(self):
self.snake.tick()

23
getkey.py Executable file
View file

@ -0,0 +1,23 @@
import sys
import termios
import tty
def getkey() -> str:
def gk() -> str:
fd: int = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch: str = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
out: str = gk()
if out == "\033":
o: str = gk()
if o == "[":
return out + o + gk()
return out + o
return out

49
snake.py Normal file
View file

@ -0,0 +1,49 @@
from typing import Tuple, List
from directions import *
from game import Game
class Snake:
def __init__(self, name: str, colour: Tuple[int, int, int]):
self.name: str = name
self.colour: Tuple[int, int, int] = colour
self.board: Game = None
self.body: List[Tuple[int, int]] = None
self.direction: int = EAST
self.can_change_direction: bool = True
self.dead: bool = True
def register(self, game: Game, body: List[Tuple[int, int]]):
self.board: Game = game
self.body: List[Tuple[int, int]] = body
self.dead: bool = False
def change_direction(self, new_direction: int):
if self.can_change_direction and (new_direction + self.direction) % 2:
self.direction: int = new_direction
self.can_change_direction: bool = False
def tick(self):
if self.dead:
return
headx, heady = self.body[-1]
if self.direction == NORTH:
heady -= 1
elif self.direction == EAST:
headx += 1
elif self.direction == SOUTH:
heady += 1
elif self.direction == WEST:
headx -= 1
if not (0 <= headx < self.board.width and 0 <= heady < self.board.height):
self.dead: bool = True
return
self.body.pop()
if (headx, heady) in self.body:
self.dead: bool = True
return
self.body.append((headx, heady))
self.can_change_direction: bool = True