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