Files
Chris Proctor a9385f8296 Refactoring lab
2026-06-26 13:27:11 -04:00

37 lines
973 B
Python

from random import randint, choice
from .snake import SnakeHead
from .apple import Apple
from retro.game import Game
def create_game():
"""Return a fresh, initialized Snake game."""
head = SnakeHead()
apple = Apple()
game = Game(
[head, apple],
{'score': 0, 'reward': 0, 'energy': 150, 'apple_dx': 0.0, 'apple_dy': 0.0},
board_size=(32, 16),
framerate=12,
)
bw, bh = game.board_size
head.position = (randint(1, bw - 2), randint(1, bh - 2))
direction, character = choice([
(SnakeHead.RIGHT, '>'),
(SnakeHead.UP, '^'),
(SnakeHead.LEFT, '<'),
(SnakeHead.DOWN, 'v'),
])
head.direction = direction
head.character = character
apple.relocate(game)
hx, hy = head.position
ax, ay = apple.position
game.state['apple_dx'] = (ax - hx) / bw
game.state['apple_dy'] = (ay - hy) / bh
return game
if __name__ == '__main__':
create_game().play()