In this project, you can use the joystick and keys of CyberPi to control the snake.

    1. import pygame
    2. from sys import exit
    3. import random
    4. import time
    5. import cyberpi
    6. class Point():
    7. def __init__(self, row, clo):
    8. self.row = row
    9. self.clo = clo
    10. def copy(self):
    11. return Point(row=self.row, clo=self.clo)
    12. # Initialize the settings
    13. pygame.init()
    14. width = 800
    15. hight = 400
    16. ROW = 30
    17. CLO = 50
    18. direct = 'left'
    19. window = pygame.display.set_mode((width, hight))
    20. pygame.display.set_caption('Greedy Snake')
    21. # Set the head of the snake in the middle
    22. head = Point(row=int(ROW / 2), clo=int(CLO / 2))
    23. # Initialize the number of cells in the body of the snake
    24. snake = [
    25. Point(row=head.row, clo=head.clo + 1),
    26. Point(row=head.row, clo=head.clo + 2),
    27. Point(row=head.row, clo=head.clo + 3)
    28. ]
    29. # Generate food outside of the body of the snake
    30. def gen_food():
    31. while 1:
    32. position = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))
    33. is_coll = False
    34. if head.row == position.row and head.clo == position.clo:
    35. is_coll = True
    36. for body in snake:
    37. if body.row == position.row and body.clo == position.clo:
    38. is_coll = True
    39. break
    40. if not is_coll:
    41. break
    42. return position
    43. # Define coordinates
    44. # Define the color of the snake head
    45. head_color = (0, 158, 128)
    46. # Coordinates of food
    47. snakeFood = gen_food()
    48. # Color of food
    49. snakeFood_color = (255, 255, 0)
    50. snake_color = (200, 0, 18)
    51. # Define a function to execute multiple drawing steps
    52. def rect(point, color):
    53. # Set the left and top positions for drawing
    54. left = point.clo * width / CLO
    55. top = point.row * hight / ROW
    56. # Color the squares
    57. pygame.draw.rect(window, color, (left, top, width / CLO, hight / ROW))
    58. quit = True
    59. # Set the frame rate
    60. clock = pygame.time.Clock()
    61. while quit:
    62. # Lock the frame rate
    63. clock.tick(30)
    64. # Control through the joystick of CyberPi
    65. if cyberpi.controller.is_press("up"):
    66. if direct == 'left' or direct == 'right':
    67. direct = 'top'
    68. if cyberpi.controller.is_press("down"):
    69. if direct == 'left' or direct == 'right':
    70. direct = 'bottom'
    71. if cyberpi.controller.is_press("left"):
    72. if direct == 'top' or direct == 'bottom':
    73. direct = 'left'
    74. if cyberpi.controller.is_press("right"):
    75. if direct == 'top' or direct == 'bottom':
    76. direct = 'right'
    77. # Control through the keys
    78. for event in pygame.event.get():
    79. if event.type == pygame.QUIT:
    80. quit = False
    81. elif event.type == pygame.KEYDOWN:
    82. if event.key == 273 or event.key == 119:
    83. if direct == 'left' or direct == 'right':
    84. direct = 'top'
    85. if event.key == 274 or event.key == 115:
    86. if direct == 'left' or direct == 'right':
    87. direct = 'bottom'
    88. if event.key == 276 or event.key == 97:
    89. if direct == 'top' or direct == 'bottom':
    90. direct = 'left'
    91. if event.key == 275 or event.key == 100:
    92. if direct == 'top' or direct == 'bottom':
    93. direct = 'right'
    94. # Eat food
    95. eat = (head.row == snakeFood.row and head.clo == snakeFood.clo)
    96. # Process the body of the snake
    97. # 1.Insert the original snake head into the head of the snake
    98. # 2.Delete the last snake
    99. if eat:
    100. snakeFood = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))
    101. snake.insert(0, head.copy())
    102. if not eat:
    103. snake.pop()
    104. # Move the snake
    105. if direct == 'left':
    106. head.clo -= 1
    107. if direct == 'right':
    108. head.clo += 1
    109. if direct == 'top':
    110. head.row -= 1
    111. if direct == 'bottom':
    112. head.row += 1
    113. dead = False
    114. if head.clo < 0 or head.row < 0 or head.clo >= CLO or head.row >= ROW:
    115. dead = True
    116. for body in snake:
    117. if head.clo == body.clo and head.row == body.row:
    118. dead = True
    119. break
    120. if dead:
    121. print('Game Over')
    122. pygame.quit()
    123. exit()
    124. quit = False
    125. # Draw the background
    126. pygame.draw.rect(window, (20, 10, 10), (0, 0, width, hight))
    127. # Snake head
    128. rect(head, head_color)
    129. # Draw food
    130. rect(snakeFood, snakeFood_color)
    131. # Draw the body of the snake
    132. for body in snake:
    133. rect(body, snake_color)
    134. # Stop controlling through CyberPi
    135. pygame.display.flip()
    136. time.sleep(0.05)
    137. pygame.quit()
    138. exit()