main_snake_OLED.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from PikaObj import *
  2. import PikaStdLib
  3. import machine
  4. pin = machine.GPIO()
  5. pin.setPin('PA0')
  6. pin.setMode('in')
  7. pin.setPull('down')
  8. pin.enable()
  9. pin.setPin('PA15')
  10. pin.setMode('in')
  11. pin.setPull('up')
  12. pin.enable()
  13. pin.setPin('PC13')
  14. pin.enable()
  15. pin.setPin('PB6')
  16. pin.enable()
  17. remove('pin')
  18. ll = machine.lowLevel()
  19. oled = machine.OLED()
  20. oled.init()
  21. snake = machine.Point()
  22. snake.x = 7
  23. snake.y = 4
  24. snake_lengh = 0
  25. while snake_lengh < 3:
  26. body = snake
  27. i = 0
  28. while i < snake_lengh:
  29. body = body.next
  30. i = i + 1
  31. body.next = machine.Point()
  32. body.next.x = body.x - 1
  33. body.next.y = body.y
  34. body.next.prev = body
  35. snake_lengh = snake_lengh + 1
  36. fruit = machine.Point()
  37. fruit.x = 13
  38. fruit.y = 2
  39. mem = PikaStdLib.MemChecker()
  40. print('mem used max:')
  41. mem.max()
  42. direction = 0
  43. isUpdate = 1
  44. while True:
  45. if isUpdate:
  46. isUpdate = 0
  47. if fruit.x == snake.x:
  48. if fruit.y == snake.y:
  49. body = snake
  50. i = 0
  51. while i < snake_lengh:
  52. body = body.next
  53. i = i + 1
  54. body.next = machine.Point()
  55. body.next.prev = body
  56. snake_lengh = snake_lengh + 1
  57. fruit.x = fruit.x + 3
  58. if fruit.x > 15:
  59. fruit.x = fruit.x - 15
  60. fruit.y = fruit.y + 3
  61. if fruit.y > 7:
  62. fruit.y = fruit.y - 7
  63. body = snake
  64. i = 0
  65. while i < snake_lengh:
  66. body = body.next
  67. i = i + 1
  68. i = 0
  69. while i < snake_lengh:
  70. body = body.prev
  71. body.next.x = body.x
  72. body.next.y = body.y
  73. i = i + 1
  74. if direction == 0:
  75. snake.x = snake.x + 1
  76. if snake.x > 15:
  77. snake.x = 0
  78. if direction == 1:
  79. snake.x = snake.x - 1
  80. if snake.x < 0:
  81. snake.x = 15
  82. if direction == 2:
  83. snake.y = snake.y - 1
  84. if snake.y < 0:
  85. snake.y = 7
  86. if direction == 3:
  87. snake.y = snake.y + 1
  88. if snake.y > 7:
  89. snake.y = 0
  90. body = snake
  91. i = 0
  92. oled.clear()
  93. oled.drawPoint(fruit.x, fruit.y)
  94. while i < snake_lengh:
  95. oled.drawPoint(body.x, body.y)
  96. body = body.next
  97. i = i + 1
  98. oled.refresh()
  99. if ll.readPin('PA0') == 1:
  100. direction = 0
  101. isUpdate = 1
  102. if ll.readPin('PC13') == 0:
  103. direction = 1
  104. isUpdate = 1
  105. if ll.readPin('PA15') == 0:
  106. direction = 2
  107. isUpdate = 1
  108. if ll.readPin('PB6') == 0:
  109. direction = 3
  110. isUpdate = 1