39_gluttonousSnake.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/usr/bin/env python
  2. #version : 2024.03.07
  3. #language : en
  4. #hardware : PI
  5. from maix import display
  6. from maix import image
  7. from maix import camera
  8. import os
  9. import time
  10. import random
  11. import sys
  12. sys.path.append("/root/")
  13. from CocoPi import QMI8658
  14. from CocoPi import BUTTON
  15. from maix import mjpg
  16. from maix import utils
  17. import base64
  18. import time
  19. cameraSize = True
  20. def CAMERATYPE():
  21. global cameraSize
  22. try:
  23. if os.path.exists("/etc/cameraSize.cfg"):
  24. cameraSize = True
  25. else:
  26. cameraSize = False
  27. except:
  28. cameraSize = False
  29. CAMERATYPE()
  30. def v831_display_show_canvas(displayShow):
  31. global _canvas_y,_canvas_x,ScreenOrientation,cameraSize
  32. CANVASSHOWIMGAGE = ""
  33. if ScreenOrientation:
  34. displayShowCanvas = image.new(size = (240, 320))
  35. displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
  36. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  37. displayShowVER = displayShowCanvas.crop(0,0,240,320)
  38. displayShowVER = displayShowVER.rotate(-90, adjust=1)
  39. display.show(displayShowVER)
  40. else:
  41. displayShowCanvas = image.new(size = (320, 240))
  42. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  43. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  44. display.show(displayShowCanvas)
  45. ScreenOrientation = False
  46. _canvas_x = 0
  47. _canvas_y = 0
  48. key_C = BUTTON(13)
  49. image.load_freetype("/root/preset/fonts/botPixel.ttf")
  50. if cameraSize==True:
  51. camera.camera.config(size=(320,240))
  52. else:
  53. camera.camera.config(size=(240,320))
  54. x = random.randrange(100, 141, 10)
  55. y = random.randrange(100, 141, 10)
  56. x1 = random.randrange(100, 141, 10)
  57. y1 = random.randrange(100, 141, 10)
  58. a = [{"x":x - 30, "y":y}, {"x":x - 20, "y":y}, {"x":x - 10, "y":y}, {"x":x, "y":y}]
  59. canvas = image.new(size = (320, 240))
  60. #-------------------------------
  61. #初始化色彩配置
  62. splash_theme_color = (255,255,255)
  63. splash_front_color = (60,60,60)
  64. fontcolor=255
  65. fontsize=1.4
  66. #开机显示
  67. splash = image.new(size=(320, 240),mode = "RGB")
  68. splash.draw_rectangle(0,0,320,240,color=splash_theme_color,thickness=-1)
  69. splash.draw_string(60, 48, "Gluttonous", 1.5, color=(255, 200, 0))
  70. splash.draw_string(120, 88, "Snake", 1.5, color=(255, 128, 64))
  71. display.show(splash)
  72. time.sleep(1)
  73. #初始化设置
  74. qmi8658=QMI8658()
  75. #校准
  76. qmi8658.calibrate()
  77. #标定初始方位
  78. initData={}
  79. initData["AcX"]=qmi8658.get_accel(10)["AcX"]
  80. initData["AcY"]=qmi8658.get_accel(10)["AcY"]
  81. initData["AcZ"]=qmi8658.get_accel(10)["AcZ"]
  82. initData["GyX"]=qmi8658.get_accel(10)["GyX"]
  83. initData["GyY"]=qmi8658.get_accel(10)["GyY"]
  84. initData["GyZ"]=qmi8658.get_accel(10)["GyZ"]
  85. angle_x = 0
  86. angle_y = 0
  87. score_count = 0
  88. direction = "RIGHT"
  89. isFinsh = False
  90. b = {"x":250, "y":250}
  91. c = {"x":250, "y":250}
  92. while True:
  93. angle_x = round(qmi8658.getPitchYawRollGxGyGz(initData)[0],2)
  94. angle_y = round(qmi8658.getPitchYawRollGxGyGz(initData)[1],2)
  95. canvas.clear()
  96. if x == x1 and y == y1:
  97. x1 = random.randrange(10, 311, 10)
  98. y1 = random.randrange(10, 231, 10)
  99. score_count = score_count + 1
  100. for j in a:
  101. if j.get("x") == x1 and j.get("y") == y1:
  102. x1 = x1 + 10
  103. y1 = y1 + 10
  104. else:
  105. if angle_y > 30 and direction != "DOWN":
  106. direction = "UP"
  107. y = y - 10
  108. b = {"x":x, "y":y}
  109. if x == x1 and y == y1:
  110. x1 = random.randrange(10, 311, 10)
  111. y1 = random.randrange(10, 231, 10)
  112. score_count = score_count + 1
  113. a.append({"x":x, "y":y})
  114. for j in a:
  115. if j.get("x") == x1 and j.get("y") == y1:
  116. x1 = x1 + 10
  117. y1 = y1 + 10
  118. elif y == -10:
  119. isFinsh = True
  120. else:
  121. c = a[0]
  122. a.pop(0)
  123. for k in a:
  124. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  125. isFinsh = True
  126. a.append({"x":x, "y":y})
  127. elif angle_y < -30 and direction != "UP":
  128. direction = "DOWN"
  129. y = y + 10
  130. b = {"x":x, "y":y}
  131. if x == x1 and y == y1:
  132. x1 = random.randrange(10, 311, 10)
  133. y1 = random.randrange(10, 231, 10)
  134. score_count = score_count + 1
  135. a.append({"x":x, "y":y})
  136. for j in a:
  137. if j.get("x") == x1 and j.get("y") == y1:
  138. x1 = x1 + 10
  139. y1 = y1 + 10
  140. elif y == 240:
  141. isFinsh = True
  142. else:
  143. c = a[0]
  144. a.pop(0)
  145. for k in a:
  146. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  147. isFinsh = True
  148. a.append({"x":x, "y":y})
  149. elif angle_x < -30 and direction != "RIGHT":
  150. direction = "LEFT"
  151. x = x - 10
  152. b = {"x":x, "y":y}
  153. if x == x1 and y == y1:
  154. x1 = random.randrange(10, 311, 10)
  155. y1 = random.randrange(10, 231, 10)
  156. score_count = score_count + 1
  157. a.append({"x":x, "y":y})
  158. for j in a:
  159. if j.get("x") == x1 and j.get("y") == y1:
  160. x1 = x1 + 10
  161. y1 = y1 + 10
  162. elif x == -10:
  163. isFinsh = True
  164. else:
  165. c = a[0]
  166. a.pop(0)
  167. for k in a:
  168. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  169. isFinsh = True
  170. a.append({"x":x, "y":y})
  171. elif angle_x > 30 and direction != "LEFT":
  172. direction = "RIGHT"
  173. x = x + 10
  174. b = {"x":x, "y":y}
  175. if x == x1 and y == y1:
  176. x1 = random.randrange(10, 311, 10)
  177. y1 = random.randrange(10, 231, 10)
  178. score_count = score_count + 1
  179. a.append({"x":x, "y":y})
  180. for j in a:
  181. if j.get("x") == x1 and j.get("y") == y1:
  182. x1 = x1 + 10
  183. y1 = y1 + 10
  184. elif x == 320:
  185. isFinsh = True
  186. else:
  187. c = a[0]
  188. a.pop(0)
  189. for k in a:
  190. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  191. isFinsh = True
  192. a.append({"x":x, "y":y})
  193. else:
  194. if direction == "UP":
  195. y = y - 10
  196. b = {"x":x, "y":y}
  197. if y == -10:
  198. isFinsh = True
  199. else:
  200. c = a[0]
  201. a.pop(0)
  202. for k in a:
  203. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  204. isFinsh = True
  205. a.append({"x":x, "y":y})
  206. elif direction == "DOWN":
  207. y = y + 10
  208. b = {"x":x, "y":y}
  209. if y == 240:
  210. isFinsh = True
  211. else:
  212. c = a[0]
  213. a.pop(0)
  214. for k in a:
  215. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  216. isFinsh = True
  217. a.append({"x":x, "y":y})
  218. elif direction == "LEFT":
  219. x = x - 10
  220. b = {"x":x, "y":y}
  221. if x == -10:
  222. isFinsh = True
  223. else:
  224. c = a[0]
  225. a.pop(0)
  226. for k in a:
  227. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  228. isFinsh = True
  229. a.append({"x":x, "y":y})
  230. else:
  231. x = x + 10
  232. b = {"x":x, "y":y}
  233. if x == 320:
  234. isFinsh = True
  235. else:
  236. c = a[0]
  237. a.pop(0)
  238. for k in a:
  239. if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
  240. isFinsh = True
  241. a.append({"x":x, "y":y})
  242. if isFinsh == True:
  243. canvas.clear()
  244. canvas.draw_string(60,40, "Gameover!", scale = 1.5, color = (255,255,255) , thickness = 1)
  245. canvas.draw_string(85,90, "Score:", scale = 1.5, color = (255,0,0) , thickness = 1)
  246. canvas.draw_string(210,90, (str(score_count)), scale = 1.5, color = (255,0,0) , thickness = 1)
  247. canvas.draw_string(30,150, "Press", scale = 1, color = (255,255,255) , thickness = 1)
  248. canvas.draw_string(105,150, "C", scale = 1, color = (255,255,255) , thickness = 1)
  249. canvas.draw_string(125,150, "TO", scale = 1, color = (255,255,255) , thickness = 1)
  250. canvas.draw_string(165,150, "Restart!", scale = 1, color = (255,255,255) , thickness = 1)
  251. v831_display_show_canvas(canvas)
  252. if (key_C.is_pressed()):
  253. while key_C.is_pressed():
  254. time.sleep(0.001)
  255. angle_x = 0
  256. angle_y = 0
  257. score_count = 0
  258. direction = "RIGHT"
  259. isFinsh = False
  260. x = random.randrange(100, 141, 10)
  261. y = random.randrange(100, 141, 10)
  262. x1 = random.randrange(100, 141, 10)
  263. y1 = random.randrange(100, 141, 10)
  264. a = [{"x":x - 30, "y":y}, {"x":x - 20, "y":y}, {"x":x - 10, "y":y}, {"x":x, "y":y}]
  265. b = {"x":250, "y":250}
  266. c = {"x":250, "y":250}
  267. else:
  268. canvas.draw_rectangle(c.get("x"),c.get("y"),10, 10, color=(0,0,0), thickness=-1)
  269. canvas.draw_rectangle(x1,y1, x1+10,y1+ 10, color=(255,255,255), thickness=-1)
  270. for i in a:
  271. x = i.get("x")
  272. y = i.get("y")
  273. canvas.draw_rectangle(x,y, x+10,y+ 10, color=(255,255,255), thickness=-1)
  274. v831_display_show_canvas(canvas)
  275. if isFinsh == False:
  276. time.sleep(0.2)
  277. else:
  278. pass