Skip to content

Commit

Permalink
Scene added + HMI.
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Jul 4, 2021
1 parent 3b0513f commit c8c1233
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
11 changes: 5 additions & 6 deletions src/Apple.py
Original file line number Diff line number Diff line change
@@ -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')
20 changes: 11 additions & 9 deletions src/Snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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}")

Expand All @@ -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}",
Expand Down
42 changes: 38 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand All @@ -34,4 +65,7 @@ def main(stdscr):


if __name__ == "__main__":
curses.wrapper(main)
try:
curses.wrapper(main)
except KeyboardInterrupt:
pass

0 comments on commit c8c1233

Please sign in to comment.