Had – Logika hry – Řešení [0]

from pathlib import Path

import pyglet

from had import State

TILE_SIZE = 64
TILES_DIRECTORY = Path('snake-tiles')

red_image = pyglet.image.load('apple.png')
snake_tiles = {}
for path in TILES_DIRECTORY.glob('*.png'):
    snake_tiles[path.stem] = pyglet.image.load(path)

window = pyglet.window.Window()

state = State()

@window.event
def on_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)
    for x, y in state.snake:
        source = 'tail'     # (Tady případně je nějaké
        dest = 'head'       #  složitější vybírání políčka)
        snake_tiles[source + '-' + dest].blit(
            x * TILE_SIZE, y * TILE_SIZE, width=TILE_SIZE, height=TILE_SIZE)
    for x, y in state.food:
        red_image.blit(
            x * TILE_SIZE, y * TILE_SIZE, width=TILE_SIZE, height=TILE_SIZE)

def move(dt):
    state.move()

pyglet.clock.schedule_interval(move, 1/6)

pyglet.app.run()