def test_movement_length_three(): field = had.State() field.snake_coords = [(0, 0)] field.snake_coords = [(5, 5), (5, 6), (5, 7)] field.snake_direction = 0, 1 field.move() assert field.snake_coords == [(5, 6), (5, 7), (5, 8)] field.snake_direction = 0, 1 field.move() assert field.snake_coords == [(5, 7), (5, 8), (5, 9)] field.snake_direction = -1, 0 field.move() assert field.snake_coords == [(5, 8), (5, 9), (4, 9)] field.snake_direction = -1, 0 field.move() assert field.snake_coords == [(5, 9), (4, 9), (3, 9)] field.snake_direction = 0, -1 field.move() assert field.snake_coords == [(4, 9), (3, 9), (3, 8)] field.snake_direction = 0, -1 field.move() assert field.snake_coords == [(3, 9), (3, 8), (3, 7)] field.snake_direction = 1, 0 field.move() assert field.snake_coords == [(3, 8), (3, 7), (4, 7)] field.snake_direction = 1, 0 field.move() assert field.snake_coords == [(3, 7), (4, 7), (5, 7)] assert field.snake_alive
def test_crash_self(direction): field = had.State() field.snake_coords = [(0, 0)] field.snake_coords = [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (1, 2), (0, 2), (0, 1), (1, 1)] field.snake_direction = direction field.move() assert not field.snake_alive
def test_crash_wall(direction): field = had.State() field.snake_coords = [(0, 0)] field.width = 1 field.height = 1 field.snake_direction = direction field.move() assert not field.snake_alive
def test_repr_size(): expected = textwrap.dedent(""" . . . . . . . . . . . . . . > . . . . . . . . . X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . """).strip() state = had.State() state.width = 11 state.height = 5 state.snake_coords = [(2, 2), (3, 3)] assert str(state) == expected
def test_repr_starting_field(): expected = textwrap.dedent(""" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . X > . . . . . . . . """).strip() state = had.State() assert str(state) == expected
def test_repr_snake(): expected = textwrap.dedent(""" . . . . . . . . . . . . . . > . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . X X X . . . . . . . . . . . . . . . . . . . . . . . . . """).strip() state = had.State() state.snake_coords = [(2, 2), (3, 2), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8)] assert str(state) == expected
def test_movement_length_one(): field = had.State() field.snake_coords = [(0, 0)] field.snake_direction = 0, 1 field.move() assert field.snake_coords == [(0, 1)] field.snake_direction = 0, -1 field.move() assert field.snake_coords == [(0, 0)] field.snake_direction = 1, 0 field.move() assert field.snake_coords == [(1, 0)] field.snake_direction = -1, 0 field.move() assert field.snake_coords == [(0, 0)] assert field.snake_alive
def test_eat_food(direction): field = had.State() field.snake_coords = [(0, 0)] field.width = 5 field.height = 5 field.snake_coords = [(2, 2)] field.food = [(2, 1), (2, 3), (1, 2), (3, 2)] field.snake_direction = direction print(field, '\n') field.move() print(field, '\n') assert len(field.food) == 4 assert len(set(field.food) - {(2, 1), (1, 2), (2, 3), (3, 2)}) == 1 field.food = [(2, 1), (2, 3), (1, 2), (3, 2)] field.food.remove((2+direction[0], 2+direction[1])) print(field, '\n') assert len(field.snake_coords) == 2 assert field.snake_coords[0] == (2, 2) field.move() print(field, '\n') assert len(field.snake_coords) == 2 assert (2, 2) not in field.snake_coords assert len(field.food) == 3 assert len(set(field.food)) == 3 assert set(field.food).isdisjoint(field.snake_coords) assert field.snake_alive field.move() print(field, '\n') field.move() print(field, '\n') field.move() print(field, '\n') assert field.snake_coords[-1] == (2, 2) assert field.snake_alive
import had from pathlib import Path TILE_SIZE = 64 MARGIN = 8 red_image = pyglet.image.load('apple.png') snake_tiles = {} for path in Path('snake-tiles').glob('*.png'): snake_tiles[path.stem] = pyglet.image.load(path) window = pyglet.window.Window(width=800, height=600) state = had.State() state.add_food() state.width = window.width // TILE_SIZE state.height = window.height // TILE_SIZE queued_directions = [] def draw(): window.clear() pyglet.gl.glEnable(pyglet.gl.GL_BLEND) pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA) tile_width = window.width // state.width tile_height = window.height // state.height for a, b, c in zip(