作者:fftust
版本:V0.1
邮箱:
固件版本:>= 44.01.005

一、玩法简介

该贪食蛇游戏是可以穿过屏幕,然后从屏幕另外一测穿出。只有头部和身子接触时,游戏才算结束。

操控: 遥杆控制 or 陀螺仪感应。 开启后拨动遥杆开始游戏。
切换移动模式: 按下B键将在遥杆控制和 陀螺仪感应 模式来回切换
改变移动速度: 按住A键,然后遥杆向左拨动则降低速度,遥杆向右拨动则移动速度加快 videoCacheA3B2BB97-C790-4A9D-AE83-2181C1A81154.mov.mp4 (4.95MB)

二、源代码

  1. import time
  2. import random
  3. from matrix_game import game_base, Sprite, RIGHT_MEET, LEFT_MEET,UP_MEET,DOWN_MEET
  4. import cyberpi
  5. import matrix_game
  6. matrix_game.FACE_ROW = 16
  7. matrix_game.FACE_CLO = 16
  8. # 设置方向
  9. matrix_game.SCREEN_DIR = "UPRIGHT"
  10. # 初始化
  11. game = matrix_game.game_base()
  12. game.set_background([0] * 16)
  13. MOVE_NOT = 0
  14. MOVE_UP = 1
  15. MOVE_DOWN = 2
  16. MOVE_LEFT = 3
  17. MOVE_RIGHT = 4
  18. FACE_CROSS = 16
  19. FACE_LINE = 8
  20. GAME_READY = 0
  21. GAME_PLAYING = 1
  22. GAME_OVER = 2
  23. GAME_PASS = 3
  24. snake_list = [[0, 0], [2, 3], [3, 3]]
  25. game_status = GAME_READY
  26. food_point = [0, 0]
  27. user_score = 0
  28. run_speed = 8
  29. move_control_mode = 0
  30. # 游戏结束判断
  31. ## 当头部和身体其他部分接触时,则游戏结束
  32. def game_over_check():
  33. global snake_list
  34. global game_status
  35. if snake_list[-1] in snake_list[0 : (len(snake_list) - 1)]:
  36. game_status = GAME_OVER
  37. # 类似渲染的概念,按节奏刷新身体。移动接口仅控制snake_list,只有调用该接口时才是真正显示出来。
  38. # 显示时,遍历整个list
  39. def show_snake():
  40. global snake_list
  41. for i in range(len(snake_list)):
  42. if i == 0:
  43. game.clear_background_with_point(snake_list[i])
  44. elif i == len(snake_list) - 1:
  45. game.set_background_with_point(snake_list[i])
  46. # 贪食蛇身体增加一个点
  47. def snake_add_point(p_x, p_y):
  48. snake_list.append([p_x, p_y])
  49. cyberpi.audio.play("score")
  50. # 查找空闲点算法
  51. food_create_order_list = [[1,2,3,4], [2,3,1,4], [4,3,1,2], [3,4,1,2], [3,4,2,1]]
  52. def point_create():
  53. global food_point
  54. global user_score
  55. global game_status
  56. user_score += 1
  57. p_x = random.randint(0, 15)
  58. p_y = random.randint(0, 15)
  59. order = random.randint(0, 3)
  60. for item in food_create_order_list[order]:
  61. out_point = check_region_not_full(p_x, p_y, item)
  62. if out_point == None:
  63. if item == food_create_order_list[order][-1]:
  64. game_status = GAME_PASS
  65. else:
  66. continue
  67. else:
  68. food_point = out_point
  69. game.clear_background_with_point(food_point)
  70. game.set_background_with_point(food_point)
  71. return food_point
  72. food_point = [0, 0]
  73. game.set_background_with_point((0, 0))
  74. return None
  75. # 贪食蛇移动算法。 由于每次只能移动一格,因此仅传入方向即可,由于不能立即向后,所以仅处理三种方向。
  76. # 处理逻辑也很简单,移动一格,首先删除 snake_list 末尾的点,然后所有数据向后移动一位,产生新的数据。
  77. def snake_move(dir):
  78. global snake_list
  79. global food_point
  80. if dir == MOVE_UP:
  81. p_x = snake_list[-1][0]
  82. p_y = snake_list[-1][1]
  83. if p_y != 0:
  84. point_t = [p_x, p_y - 1]
  85. else:
  86. point_t = [p_x, 15]
  87. if point_t == food_point:
  88. snake_add_point(food_point[0], food_point[1])
  89. # else:
  90. for i in range(len(snake_list) - 1):
  91. snake_list[i] = snake_list[i + 1]
  92. p_x = snake_list[-1][0]
  93. p_y = snake_list[-1][1]
  94. if p_y != 0:
  95. snake_list[-1] = [p_x, p_y - 1]
  96. else:
  97. snake_list[-1] = [p_x, 15]
  98. elif dir == MOVE_DOWN:
  99. p_x = snake_list[-1][0]
  100. p_y = snake_list[-1][1]
  101. if p_y != 15:
  102. point_t = [p_x, p_y + 1]
  103. else:
  104. point_t = [p_x, 0]
  105. if point_t == food_point:
  106. snake_add_point(food_point[0], food_point[1])
  107. # else:
  108. for i in range(len(snake_list) - 1):
  109. snake_list[i] = snake_list[i + 1]
  110. p_x = snake_list[-1][0]
  111. p_y = snake_list[-1][1]
  112. if p_y != 15:
  113. snake_list[-1] = [p_x, p_y + 1]
  114. else:
  115. snake_list[-1] = [p_x, 0]
  116. elif dir == MOVE_LEFT:
  117. p_x = snake_list[-1][0]
  118. p_y = snake_list[-1][1]
  119. if p_x != 0:
  120. point_t = [p_x - 1, p_y]
  121. else:
  122. point_t = [15, p_y]
  123. if point_t == food_point:
  124. snake_add_point(food_point[0], food_point[1])
  125. # else:
  126. for i in range(len(snake_list) - 1):
  127. snake_list[i] = snake_list[i + 1]
  128. p_x = snake_list[-1][0]
  129. p_y = snake_list[-1][1]
  130. if p_x != 0:
  131. snake_list[-1] = [p_x - 1, p_y]
  132. else:
  133. snake_list[-1] = [15, p_y]
  134. elif dir == MOVE_RIGHT:
  135. p_x = snake_list[-1][0]
  136. p_y = snake_list[-1][1]
  137. if p_x != 15:
  138. point_t = [p_x + 1, p_y]
  139. else:
  140. point_t = [0, p_y]
  141. if point_t == food_point:
  142. snake_add_point(food_point[0], food_point[1])
  143. # else:
  144. for i in range(len(snake_list) - 1):
  145. snake_list[i] = snake_list[i + 1]
  146. p_x = snake_list[-1][0]
  147. p_y = snake_list[-1][1]
  148. if p_x != 15:
  149. snake_list[-1] = [p_x + 1, p_y]
  150. else:
  151. snake_list[-1] = [0, p_y]
  152. else:
  153. return
  154. if point_t == food_point:
  155. print("create result", point_create())
  156. game_over_check()
  157. def check_region_not_full(p_x, p_y, region): # region 1: up_left 2: down_left 3: right_up 4: right_down
  158. global snake_list
  159. x = p_x
  160. y = p_y
  161. if region == 1:
  162. while y >= 0 and y <= 15:
  163. if not ([x, y] in snake_list):
  164. return [x, y]
  165. else:
  166. if x > 0:
  167. x = x - 1
  168. else:
  169. y = y -1
  170. x = p_x
  171. elif region == 2:
  172. while y >= 0 and y <= 15:
  173. if not ([x, y] in snake_list):
  174. return [x, y]
  175. else:
  176. if x > 0:
  177. x = x - 1
  178. else:
  179. y = y + 1
  180. x = p_x
  181. elif region == 3:
  182. while y >= 0 and y <= 15:
  183. if not ([x, y] in snake_list):
  184. return [x, y]
  185. else:
  186. if x < 15:
  187. x = x + 1
  188. else:
  189. y = y -1
  190. x = p_x
  191. elif region == 4:
  192. while y >= 0 and y <= 15:
  193. if not ([x, y] in snake_list):
  194. return [x, y]
  195. else:
  196. if x < 15:
  197. x = x + 1
  198. else:
  199. y = y + 1
  200. x = p_x
  201. return None
  202. def speed_control():
  203. global run_speed
  204. def move_control():
  205. global game_status, move_dir, move_dir_last, food_point
  206. if game_status == GAME_READY:
  207. game_status = GAME_PLAYING
  208. elif game_status == GAME_PLAYING:
  209. if cyberpi.controller.is_press("left"):
  210. if move_dir_last != MOVE_DOWN:
  211. move_dir = MOVE_UP
  212. elif cyberpi.controller.is_press("right"):
  213. if move_dir_last != MOVE_UP:
  214. move_dir = MOVE_DOWN
  215. elif cyberpi.controller.is_press("up"):
  216. if move_dir_last != MOVE_RIGHT:
  217. move_dir = MOVE_LEFT
  218. elif cyberpi.controller.is_press("down"):
  219. if move_dir_last != MOVE_LEFT:
  220. move_dir = MOVE_RIGHT
  221. if count % run_speed == 0:
  222. snake_move(move_dir)
  223. move_dir_last = move_dir
  224. def config_control():
  225. global game_status, move_dir, move_dir_last, food_point, move_control_mode, run_speed
  226. if game_status == GAME_PLAYING:
  227. if cyberpi.controller.is_press("a"):
  228. if cyberpi.controller.is_press("left"):
  229. run_speed += 1
  230. if run_speed > 15:
  231. run_speed = 15
  232. while cyberpi.controller.is_press("left"):
  233. pass
  234. elif cyberpi.controller.is_press("right"):
  235. run_speed -= 1
  236. if run_speed < 2:
  237. run_speed = 2
  238. while cyberpi.controller.is_press("right"):
  239. pass
  240. if cyberpi.controller.is_press("b"):
  241. while cyberpi.controller.is_press("b"):
  242. pass
  243. move_control_mode += 1
  244. move_control_mode %= 2
  245. def move_control2():
  246. global game_status, move_dir, move_dir_last, food_point, move_control_mode
  247. if game_status == GAME_READY:
  248. game_status = GAME_PLAYING
  249. elif game_status == GAME_PLAYING:
  250. if cyberpi.is_tiltleft():
  251. if move_dir_last != MOVE_DOWN:
  252. move_dir = MOVE_UP
  253. elif cyberpi.is_tiltright():
  254. if move_dir_last != MOVE_UP:
  255. move_dir = MOVE_DOWN
  256. elif cyberpi.is_tiltforward():
  257. if move_dir_last != MOVE_RIGHT:
  258. move_dir = MOVE_LEFT
  259. elif cyberpi.is_tiltback():
  260. if move_dir_last != MOVE_LEFT:
  261. move_dir = MOVE_RIGHT
  262. if count % run_speed == 0:
  263. snake_move(move_dir)
  264. move_dir_last = move_dir
  265. move_dir = MOVE_NOT
  266. move_dir_last = MOVE_NOT
  267. count = 0
  268. point_create()
  269. show_snake()
  270. while True:
  271. speed_control()
  272. config_control()
  273. if move_control_mode == 0:
  274. move_control()
  275. else:
  276. move_control2()
  277. show_snake()
  278. if game_status == GAME_OVER:
  279. cyberpi.console.println(len(snake_list) - 3)
  280. if cyberpi.controller.is_press("middle"):
  281. snake_list = [[0, 0], [2, 3], [3, 3]]
  282. food_point = [0, 0]
  283. show_snake()
  284. point_create()
  285. game_status = GAME_PLAYING
  286. elif game_status == GAME_PASS:
  287. cyberpi.console.println("Good Job")
  288. if cyberpi.controller.is_press("middle"):
  289. snake_list = [[0, 0], [2, 3], [3, 3]]
  290. food_point = [0, 0]
  291. point_create()
  292. show_snake()
  293. game_status = GAME_PLAYING
  294. game.screen_refresh()
  295. time.sleep(0.01)
  296. count += 1