Move snake forward one segment.
()
| 28 | |
| 29 | |
| 30 | def move(): |
| 31 | """Move snake forward one segment.""" |
| 32 | head = snake[-1].copy() |
| 33 | head.move(aim) |
| 34 | |
| 35 | if not inside(head) or head in snake: |
| 36 | square(head.x, head.y, 9, 'red') |
| 37 | update() |
| 38 | return |
| 39 | |
| 40 | snake.append(head) |
| 41 | |
| 42 | if head == food: |
| 43 | print('Snake:', len(snake)) |
| 44 | food.x = randrange(-15, 15) * 10 |
| 45 | food.y = randrange(-15, 15) * 10 |
| 46 | else: |
| 47 | snake.pop(0) |
| 48 | |
| 49 | clear() |
| 50 | |
| 51 | for body in snake: |
| 52 | square(body.x, body.y, 9, 'black') |
| 53 | |
| 54 | square(food.x, food.y, 9, 'green') |
| 55 | update() |
| 56 | ontimer(move, 100) |
| 57 | |
| 58 | |
| 59 | setup(420, 420, 370, 0) |