5320 views|31 replies

96

Posts

0

Resources
The OP
 

Snake for esp32 (time to show real skills) [Copy link]

 
 
Rendering:
  1. import random import machine from machine import Pin,I2C,ADC import ssd1306 from time import sleep import framebuf i2c = I2C(scl=Pin(18), sda=Pin(19), freq=900000) oled = ssd1306.SSD1306_I2C(128,64, i2c) oled.poweron() oled.init_display() oled.fill(0) oled.show() w=8 h=8 st=8 base_x=20 base_y=20 snoke=[[10,10],[20,10],[30,10]] by="right" oled.fill(0) oled.fill_rect(snoke[0][0],snoke[0][1],w,h,1) oled.fill_rect(snoke[1][0],snoke[1][1 ],w,h,1) oled.fill_rect(snoke[2][0],snoke[2][1],w,h,1) oled.show() def suiji(): global base_x global base_y base_x = random.randint(10,100) base_y = random.randint(10,50) def base(): oled.fill_rect(base_x,base_y,w,h,1) def check() : if (abs(base_x-snoke[0][0]))<=2 and (abs(base_y-snoke[0][1]))<=2: if by=="up": new=[snoke[-1][0],snoke[-1][1]+st] snoke.append(new) suiji() if by=="down": new=[snoke[-1][0 ],snoke[-1][1]-st] snoke.append(new) suiji() if by=="left": new=[snoke[-1][0]+st,snoke[-1][1]] snoke.append(new) suiji() if by=="right": new=[snoke[-1][0 ]-st,snoke[-1][1]] snoke.append(new) suiji() def right(): global snoke oled.fill(0) base() new=[snoke[0][0]+st,snoke[0][1]] snoke.insert(0,new) snoke.pop() for x in range(len(snoke)): oled.fill_rect(snoke[x][0],snoke[x][1],w,h,1) oled.show() def up(): global snoke oled.fill(0) base() new=[ snoke[0][0],snoke[0][1]-st] snoke.insert(0,new) snoke.pop() for x in range(len(snoke)): oled.fill_rect(snoke[x][0],snoke[x][1],w,h,1) oled.show() def down(): global snoke oled.fill(0) base() new=[ snoke[0][0],snoke[0][1]+st] snoke.insert(0,new) snoke.pop() for x in range(len(snoke)): oled.fill_rect(snoke[x][0],snoke[x][1],w,h,1) oled.show() def left(): global snoke oled.fill(0) base() new=[snoke[0][0]-st,snoke[0][1]] snoke.insert(0,new) snoke.pop() for x in range(len(snoke)): oled.fill_rect(snoke[x][0],snoke[x][1],w,h,1) oled.show() a1=ADC(Pin(35)) a2=ADC(Pin(34)) def test(): global by x=a1.read() y=a2.read() if x==0: by="left" elif x==4095: by="right" elif y==0: by="up" elif y==4095: by="down" while 1: check() test() if by=="right": right() elif by=="left": left() elif by=="up": up() elif by=="down": down() sleep(0.5) print('snoke:%s,%s' %(snoke[0][0],snoke[0][1])) print('base: %s,%s' %(base_x,base_y))
复制代码
This content is created by EEWORLD forum user youxinweizhi, if needed Reprinting or using for commercial purposes requires the author's consent and citing the source.


Latest reply

Strong   Details Published on 2019-10-21 07:58

赞赏

1

查看全部赞赏

 
 

103

Posts

3

Resources
2
 
Take a look
 
 
 

1368

Posts

6

Resources
3
 
Quickly watch
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 
 
 

1

Posts

2

Resources
4
 
Take a look
 
 
 

17

Posts

0

Resources
5
 
Curious curious
 
 
 

1

Posts

0

Resources
6
 
learn. . . . .
 
 
 

13

Posts

0

Resources
7
 
Observation
 
 
 

2

Posts

0

Resources
8
 
Guess
 
 
 

2

Posts

0

Resources
9
 
 
 
 

1

Posts

0

Resources
10
 
Very powerful                                    
 
 
 

2

Posts

0

Resources
11
 
Check out the great works
 
 
 

616

Posts

0

Resources
12
 
, isn't the source code hidden?
 
 
 

2

Posts

0

Resources
13
 
You can also play like this!
 
 
 

29

Posts

0

Resources
14
 
Watch
 
 
 

85

Posts

0

Resources
15
 

curious

 
 
 

16

Posts

0

Resources
16
 
Look at the code and learn
 
 
 

63

Posts

0

Resources
17
 
Very similar to
 
 
 

1

Posts

0

Resources
18
 

Awesome, this technique is 666

 
 
 

291

Posts

0

Resources
19
 

Can you share the code so I can learn from it?

 
 
 

6

Posts

0

Resources
20
 

Is it real technology?

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list