Refactoring lab
This commit is contained in:
36
games/snake/__init__.py
Normal file
36
games/snake/__init__.py
Normal file
@@ -0,0 +1,36 @@
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user