-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicro-snake.py
127 lines (93 loc) · 3.01 KB
/
micro-snake.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#Snake clone for Micropython
#Problems with debouncing rotary encoder
from machine import Pin, I2C
import ssd1306
import time
from rotary_quad_encoder import RotaryQuadEncoder
import urandom
snake=[[10,10],[15,10],[20,10],[25,10],[30,10]]
directions=['e','n','w','s']
snake_dir='w'
apple=[urandom.randrange(18,120),urandom.randrange(18,55)]
score=0
# ESP32 Pin assignment
i2c = I2C(-1, scl=Pin(22), sda=Pin(21))
# ESP8266 Pin assignment
#i2c = I2C(-1, scl=Pin(5), sda=Pin(4))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
r = RotaryQuadEncoder(pin1=12, pin2=13, half_steps=False, pins_pull_up=False,
track_count=True, reverse=False, range_mode=RotaryQuadEncoder.RANGE_BOUNDED, min=0, max=1)
push_b=Pin(5, Pin.IN)
def move_snake(snake_dir):
global snake
global apple
global score
if snake_dir=='e':
head=snake[0][0]-6
print(head)
snake.insert(0,[head,snake[0][1]])
time.sleep_ms(50)
elif snake_dir=='w':
head=snake[0][0]+6
snake.insert(0,[head,snake[0][1]])
time.sleep_ms(50)
elif snake_dir=='n':
head=snake[0][1]-6
snake.insert(0,[snake[0][0],head])
time.sleep_ms(50)
elif snake_dir=='s':
head=snake[0][1]+6
snake.insert(0,[snake[0][0],head])
time.sleep_ms(50)
snake.pop()
if (snake[0][0] > 128) or (snake[0][0] < 0) or (snake[0][1] > 64) or (snake[0][1] < 16):
print('bump!')
snake[0]=[64,32]
snake_dir='w'
if (apple[0] < snake[0][0] + 5) and (apple[0] + 3 > snake[0][0]) and (apple[1] < snake[0][1] + 5) and (apple[1] + 3 > snake[0][1]):
print('yummy!')
oled.invert(1)
time.sleep(0.01)
oled.invert(0)
time.sleep(0.01)
score+=1
#extend body after apple
snake.insert(0, [snake[0][0],snake[0][1]])
#new apple
apple=[urandom.randrange(18,120),urandom.randrange(18,55)]
print('--------------')
#print(snake)
oled.fill(0)
oled.rect(0,16,128,48,1)
oled.text('Apples %i' % score, 0,0)
oled.rect(apple[0],apple[1],3,3,1)
for segment in snake:
oled.fill_rect(segment[0],segment[1],5,5,1)
oled.show()
def rotate(pin):
global r
global snake_dir
global directions
result = r.process()
if result != None:
print(result)
idx = directions.index(snake_dir)
if result==0:
idx+=1
if idx>3:
idx=0
elif result==1:
idx-=1
if idx<0:
idx=3
snake_dir=directions[idx]
print(snake_dir)
r.pin1.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=rotate)
r.pin2.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=rotate)
#push_b.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=push)
oled.fill(0)
while True:
move_snake(snake_dir)
time.sleep_ms(200)