From c8c12335448de476bdfc05dc664e80bb684391c2 Mon Sep 17 00:00:00 2001 From: Antoine Stevan Date: Sun, 4 Jul 2021 13:40:37 +0200 Subject: [PATCH] Scene added + HMI. --- src/Apple.py | 11 +++++------ src/Snake.py | 20 +++++++++++--------- src/main.py | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/Apple.py b/src/Apple.py index 8dfbece..daa4a09 100644 --- a/src/Apple.py +++ b/src/Apple.py @@ -1,14 +1,13 @@ -import curses - import numpy as np class Apple: - def __init__(self): - self.pos = (np.random.randint(curses.LINES), np.random.randint(curses.COLS)) + def __init__(self, y, x, h, w): + self.pos = None + self.spawn(y, x, h, w) - def spawn(self): - self.pos = (np.random.randint(curses.LINES), np.random.randint(curses.COLS)) + def spawn(self, y, x, h, w): + self.pos = (np.random.randint(y+1, y + h - 1), np.random.randint(x+1, x + w - 1)) def show(self, stdscr): stdscr.addstr(*self.pos, 'O') diff --git a/src/Snake.py b/src/Snake.py index 9cd2d2c..e790a0b 100644 --- a/src/Snake.py +++ b/src/Snake.py @@ -16,10 +16,10 @@ class Snake: - def __init__(self): - head = (curses.LINES // 2, curses.COLS // 2) + def __init__(self, y, x, h, w): + head = (y + (y + h) // 2, x + (x + w) // 2) self.body = [head] - tmp = [tuple(map(sum, zip(head, (0, i)))) for i in range(1, 20)] + tmp = [tuple(map(sum, zip(head, (0, i)))) for i in range(1, 2)] self.body += tmp self.tokens = ['@'] + ['-'] * len(tmp) @@ -29,6 +29,8 @@ def __init__(self): self.trail = [] self.score = 0 + self.scene = y, x, h, w + def change_direction(self, dir): if (dir in [curses.KEY_UP, curses.KEY_DOWN, curses.KEY_LEFT, curses.KEY_RIGHT] and not (self.dir == curses.KEY_UP and dir == curses.KEY_DOWN) @@ -79,23 +81,23 @@ def is_eating(self, apple): eating_apple = self.body[0] == apple.pos if eating_apple: self.score += 1 - apple.spawn() + apple.spawn(*self.scene) return eating_apple def head(self): return self.body[0] - def inside(self): - return (0 <= self.body[0][0] < curses.LINES) and (0 <= self.body[0][1] < curses.COLS) + def inside(self, y, x, h, w): + return (y < self.body[0][0] < y + h - 1) and (x < self.body[0][1] < x + w - 1) def self_intersect(self): return sum([self.head() == part for part in self.body[1:]]) - def update(self, eating_apple): + def update(self, eating_apple, ): if eating_apple: self.trail += [self.body[-1]] * 10 - if not self.inside(): + if not self.inside(*self.scene): return 1 # raise Warning(f"hit walls -> {self.score}") @@ -109,7 +111,7 @@ def show(self, stdscr): for i, part in enumerate(self.body[1:]): stdscr.addch(*part, self.tokens[i + 1]) - if self.inside(): + if self.inside(*self.scene): stdscr.addstr(*self.body[0], '@') msgs = [f"trail: {self.trail}", diff --git a/src/main.py b/src/main.py index 4e4f6b3..4411e36 100644 --- a/src/main.py +++ b/src/main.py @@ -11,16 +11,47 @@ def main(stdscr): curses.curs_set(0) stdscr.nodelay(1) - snake = Snake() - apple = Apple() + scene = 0, 0, curses.LINES - 1, curses.COLS - 1 + snake = Snake(*scene) + apple = Apple(*scene) fps = 15 + H = 20 + W = 20 + + _wall = 10 + curses.init_color(_wall, 1000, 1000, 1000) + curses.init_pair(_wall, _wall, _wall) + play = True while True: stdscr.erase() - snake.change_direction(stdscr.getch()) + for y, x in [(scene[0], scene[1] + x) for x in range(scene[3])] + \ + [(scene[0] + scene[2] - 1, scene[1] + x) for x in range(scene[3])] + \ + [(scene[0] + y, scene[1]) for y in range(1, scene[2])] + \ + [(scene[0] + y, scene[1] + scene[3] - 1) for y in range(1, scene[2])]: + stdscr.addch(y, x, ' ', curses.color_pair(_wall)) + + if curses.is_term_resized(curses.LINES, curses.COLS): + pass + + c = stdscr.getch() + + if c in [27, ord('q')]: + break + + if c == ord('r'): + if not play: + snake = Snake(*scene) + apple = Apple(*scene) + play = True + + if c == 10: + curses.resizeterm(curses.LINES + 5, curses.COLS) + + snake.change_direction(c) if play: snake.move() if snake.update(snake.is_eating(apple)): @@ -34,4 +65,7 @@ def main(stdscr): if __name__ == "__main__": - curses.wrapper(main) + try: + curses.wrapper(main) + except KeyboardInterrupt: + pass