MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / move

Method move

projects/Snake Game/src/snake.py:21–41  ·  view source on GitHub ↗

Moves the snake in the given direction. Args: direction (Direction): The direction to move the snake. Returns: Point: The new snake head position.

(self, direction)

Source from the content-addressed store, hash-verified

19 self.direction = Direction.RIGHT
20
21 def move(self, direction):
22 """Moves the snake in the given direction.
23
24 Args:
25 direction (Direction): The direction to move the snake.
26
27 Returns:
28 Point: The new snake head position.
29 """
30 x, y = self.head
31 if direction == Direction.RIGHT:
32 x += self.block_size
33 elif direction == Direction.LEFT:
34 x -= self.block_size
35 elif direction == Direction.DOWN:
36 y += self.block_size
37 elif direction == Direction.UP:
38 y -= self.block_size
39 self.head = Point(x, y)
40 self.blocks.insert(0, self.head)
41 return self.head
42
43 def self_collision(self):
44 """Checks if the snake collides with itself.

Callers 2

play_stepMethod · 0.45
test_moveMethod · 0.45

Calls

no outgoing calls

Tested by 1

test_moveMethod · 0.36