update main_snake.py

This commit is contained in:
pikastech 2021-12-28 16:49:04 +08:00
parent 409d2bebf5
commit cac73f3f6e

View File

@ -64,34 +64,33 @@ while True:
if isUpdate:
# isUpdate = 0
# check eat fruit
if f.x == s.x:
if f.y == s.y:
# have eat fruit
isEat = 1
f.x = f.x + 30
if f.x > x_max:
f.x = f.x - x_max
f.y = f.y + 30
if f.y > y_max:
f.y = f.y - y_max
lcd.fill(f.x, f.y, w, h, 'green')
if f.x == s.x and f.y == s.y:
# have eat fruit
isEat = 1
f.x = f.x + 30
if f.x > x_max:
f.x = f.x - x_max
f.y = f.y + 30
if f.y > y_max:
f.y = f.y - y_max
lcd.fill(f.x, f.y, w, h, 'green')
# move snake by the direction
if d == 0:
x_new = s.x + 10
y_new = s.y
if x_new > x_max:
x_new = 0
if d == 1:
elif d == 1:
x_new = s.x
y_new = s.y - 10
if y_new < 0:
y_new = y_max
if d == 2:
elif d == 2:
x_new = s.x
y_new = s.y + 10
if y_new > y_max:
y_new = 0
if d == 3:
elif d == 3:
x_new = s.x - 10
y_new = s.y
if x_new < 0:
@ -111,29 +110,28 @@ while True:
print(len)
print('mem used max:')
mem.max()
if not isEat:
# drow the snake and fruit
# clear last body
lcd.fill(s.prev.x, s.prev.y, w, h, 'white')
# new body
s.prev.x = x_new
s.prev.y = y_new
# head is last body
s = s.prev
lcd.fill(s.x, s.y, w, h, 'blue')
b = s
i = 0
# drow the snake and fruit
# clear last body
lcd.fill(s.prev.x, s.prev.y, w, h, 'white')
# new body
s.prev.x = x_new
s.prev.y = y_new
# head is last body
s = s.prev
lcd.fill(s.x, s.y, w, h, 'blue')
b = s
i = 0
# scan key
key_val = key.get()
if key_val == 0:
d = 0
isUpdate = 1
if key_val == 1:
elif key_val == 1:
d = 1
isUpdate = 1
if key_val == 2:
elif key_val == 2:
d = 2
isUpdate = 1
if key_val == 3:
elif key_val == 3:
d = 3
isUpdate = 1