Drawing the snake – Řešení [3]
import pyglet
TILE_SIZE = 64
class GameState:
def initialize(self):
self.snake = [(1, 2), (2, 2), (3, 2), (3, 3), (3, 4), (3, 5), (4, 5)]
self.food = [(2, 0), (5, 1), (1, 4)]
def draw(self):
for x, y in self.snake:
snake_tiles['end-end'].blit(x * TILE_SIZE, y * TILE_SIZE,
width=TILE_SIZE, height=TILE_SIZE)
for x, y in self.food:
apple_image.blit(x * TILE_SIZE, y * TILE_SIZE,
width=TILE_SIZE, height=TILE_SIZE)
state = GameState()
state.initialize()
apple_image = pyglet.image.load('apple.png')
green_image = pyglet.image.load('green.png')
snake_tiles = {}
for start in ['bottom', 'end', 'left', 'right', 'top']:
for end in ['bottom', 'end', 'left', 'right', 'top', 'dead', 'tongue']:
key = start + '-' + end
image = pyglet.image.load('snake-tiles/' + key + '.png')
snake_tiles[key] = image
window = pyglet.window.Window()
@window.event
def on_draw():
window.clear()
# Lepší vykreslování (pro nás zatím kouzelné zaříkadlo)
pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
state.draw()
pyglet.app.run()
Toto je stránka lekce z kurzu, který probíhá nebo proběhl naživo s instruktorem.