Skip to content

Commit

Permalink
Turn snake towards sides with arrow keys
Browse files Browse the repository at this point in the history
(but not backwards)
  • Loading branch information
WebF0x committed Mar 22, 2020
1 parent 258ee21 commit 0a92140
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
14 changes: 14 additions & 0 deletions game.py
Expand Up @@ -22,3 +22,17 @@ def update(self):
self.snake.x -= 1
elif self.snake.orientation == 'right':
self.snake.x += 1

def request_orientation(self, requested_orientation):
current_orientation = self.snake.orientation

if current_orientation == 'up' and requested_orientation == 'down':
return
if current_orientation == 'down' and requested_orientation == 'up':
return
if current_orientation == 'left' and requested_orientation == 'right':
return
if current_orientation == 'right' and requested_orientation == 'left':
return

self.snake.set_orientation(requested_orientation)
14 changes: 14 additions & 0 deletions main.py
Expand Up @@ -38,6 +38,20 @@ def on_draw(self):
}
draw_snake_head(snake_head_canvas, self.game.snake.orientation)

def on_key_press(self, symbol, modifiers):
requested_orientation = None
if symbol == arcade.key.DOWN:
requested_orientation = 'down'
elif symbol == arcade.key.UP:
requested_orientation = 'up'
elif symbol == arcade.key.LEFT:
requested_orientation = 'left'
elif symbol == arcade.key.RIGHT:
requested_orientation = 'right'

if requested_orientation:
self.game.request_orientation(requested_orientation)


def main():
window = GameWindow()
Expand Down
56 changes: 56 additions & 0 deletions test_game.py
Expand Up @@ -44,3 +44,59 @@ def test_when_update_while_facing_right_then_snake_moves_forward(self):
game.update()
assert 6 == game.snake.x
assert 5 == game.snake.y

def test_when_requesting_orientation_to_the_sides_then_snake_turns(self):
game = Game(10)
game.initialize_snake()

game.snake.set_orientation('down')
game.request_orientation('left')
assert 'left' == game.snake.orientation

game.snake.set_orientation('down')
game.request_orientation('right')
assert 'right' == game.snake.orientation

game.snake.set_orientation('up')
game.request_orientation('left')
assert 'left' == game.snake.orientation

game.snake.set_orientation('up')
game.request_orientation('right')
assert 'right' == game.snake.orientation

game.snake.set_orientation('left')
game.request_orientation('up')
assert 'up' == game.snake.orientation

game.snake.set_orientation('left')
game.request_orientation('down')
assert 'down' == game.snake.orientation

game.snake.set_orientation('right')
game.request_orientation('up')
assert 'up' == game.snake.orientation

game.snake.set_orientation('right')
game.request_orientation('down')
assert 'down' == game.snake.orientation

def test_when_requesting_orientation_backwards_then_snake_doesnt_turn(self):
game = Game(10)
game.initialize_snake()

game.snake.set_orientation('down')
game.request_orientation('up')
assert 'down' == game.snake.orientation

game.snake.set_orientation('up')
game.request_orientation('down')
assert 'up' == game.snake.orientation

game.snake.set_orientation('right')
game.request_orientation('left')
assert 'right' == game.snake.orientation

game.snake.set_orientation('left')
game.request_orientation('right')
assert 'left' == game.snake.orientation

0 comments on commit 0a92140

Please sign in to comment.