此项目利用童芯派的方向键对 pygame 中的贪吃蛇进行控制。

    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. # 初始化
    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('贪吃蛇游戏')
    21. # 蛇头坐标定在中间
    22. head = Point(row=int(ROW / 2), clo=int(CLO / 2))
    23. # 初始化蛇身的元素数量
    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. # 生成食物并且不让食物生成在蛇的身体里面
    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. # 定义坐标
    44. # 蛇头颜色可以自定义
    45. head_color = (0, 158, 128)
    46. # 食物坐标
    47. snakeFood = gen_food()
    48. # 食物颜色
    49. snakeFood_color = (255, 255, 0)
    50. snake_color = (200, 0, 18)
    51. # 需要执行很多步画图操作 所以定义一个函数
    52. def rect(point, color):
    53. # 定位 画图需要left和top
    54. left = point.clo * width / CLO
    55. top = point.row * hight / ROW
    56. # 将方块涂色
    57. pygame.draw.rect(window, color, (left, top, width / CLO, hight / ROW))
    58. quit = True
    59. # 设置帧频率
    60. clock = pygame.time.Clock()
    61. while quit:
    62. # 处理帧频 锁帧
    63. clock.tick(30)
    64. # 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. # 键盘控制
    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. # 吃东西
    95. eat = (head.row == snakeFood.row and head.clo == snakeFood.clo)
    96. # 处理蛇的身子
    97. # 1.把原来的头插入到snake的头上
    98. # 2.把最后一个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. # 移动一下
    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. # 背景画图
    126. pygame.draw.rect(window, (20, 10, 10), (0, 0, width, hight))
    127. # 蛇头
    128. rect(head, head_color)
    129. # 绘制食物
    130. rect(snakeFood, snakeFood_color)
    131. # 绘制蛇的身子
    132. for body in snake:
    133. rect(body, snake_color)
    134. # 交还控制权
    135. pygame.display.flip()
    136. time.sleep(0.05)
    137. pygame.quit()
    138. exit()