snake is ok

This commit is contained in:
李昂 2021-10-28 16:34:18 +08:00
parent 0bd1e52d24
commit 335ffb1504

View File

@ -2,7 +2,6 @@ from PikaObj import *
import PikaStdLib
import PikaPiZero
import STM32
pin = STM32.GPIO()
pin.init()
pin.setPin('PA0')
@ -21,80 +20,93 @@ remove('pin')
ll = STM32.lowLevel()
oled = PikaPiZero.OLED()
oled.init()
p0 = PikaPiZero.Point()
p0.x = 7
p0.y = 4
p_next_num = 0
k = 0
while k < 3:
p = p0
snake = PikaPiZero.Point()
snake.x = 7
snake.y = 4
snake_lengh = 0
while snake_lengh < 3:
body = snake
i = 0
while i < p_next_num:
p = p.next
while i < snake_lengh:
body = body.next
i = i + 1
p.next = PikaPiZero.Point()
p.next.x = p.x - 1
p.next.y = p.y
p.next.prev = p
p_next_num = p_next_num + 1
k = k + 1
body.next = PikaPiZero.Point()
body.next.x = body.x - 1
body.next.y = body.y
body.next.prev = body
snake_lengh = snake_lengh + 1
fruit = PikaPiZero.Point()
fruit.x = 13
fruit.y = 2
mem = PikaStdLib.MemChecker()
print('mem used max:')
mem.max()
pos = 0
direction = 0
isUpdate = 1
while True:
if isUpdate:
isUpdate = 0
p = p0
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 = PikaPiZero.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 < p_next_num:
p = p.next
while i < snake_lengh:
body = body.next
i = i + 1
i = 0
while i < p_next_num:
p = p.prev
p.next.x = p.x
p.next.y = p.y
while i < snake_lengh:
body = body.prev
body.next.x = body.x
body.next.y = body.y
i = i + 1
if pos == 0:
p0.x = p0.x + 1
if p0.x > 15:
p0.x = 0
if pos == 1:
p0.x = p0.x - 1
if p0.x < 0:
p0.x = 15
if pos == 2:
p0.y = p0.y - 1
if p0.y < 0:
p0.y = 7
if pos == 3:
p0.y = p0.y + 1
if p0.y > 7:
p0.y = 0
p = p0
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()
while i < p_next_num:
oled.drawPoint(p.x, p.y)
p = p.next
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:
pos = 0
direction = 0
isUpdate = 1
if ll.readPin('PC13') == 0:
pos = 1
direction = 1
isUpdate = 1
if ll.readPin('PA15') == 0:
pos = 2
direction = 2
isUpdate = 1
if ll.readPin('PB6') == 0:
pos = 3
direction = 3
isUpdate = 1