Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
MicroPython实现ESP32+SSD1306的小恐龙游戏
本人计算机专业,第一次接触单片机,有做的不好的地方望大佬指点!!
准备工作就是烧入micropython固件在micropython.org/download/下载,通过Thonny烧录然后写代码Tonny3.3.13(点击下载)。
废话少说就开始写程序了。
一、SSD1306驱动
网上很多的教程和文件,这里选了个简单的,能够实现I2C和SPI的接口,但是只能显示英文。
SSD1306.py
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces from micropython import const import framebuf # register definitions SET_CONTRAST = const(0x81) SET_ENTIRE_ON = const(0xA4) SET_NORM_INV = const(0xA6) SET_DISP = const(0xAE) SET_MEM_ADDR = const(0x20) SET_COL_ADDR = const(0x21) SET_PAGE_ADDR = const(0x22) SET_DISP_START_LINE = const(0x40) SET_SEG_REMAP = const(0xA0) SET_MUX_RATIO = const(0xA8) SET_COM_OUT_DIR = const(0xC0) SET_DISP_OFFSET = const(0xD3) SET_COM_PIN_CFG = const(0xDA) SET_DISP_CLK_DIV = const(0xD5) SET_PRECHARGE = const(0xD9) SET_VCOM_DESEL = const(0xDB) SET_CHARGE_PUMP = const(0x8D) # Subclassing FrameBuffer provides support for graphics primitives # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html class SSD1306(framebuf.FrameBuffer): def __init__(self, width, height, external_vcc): self.width = width self.height = height self.external_vcc = external_vcc self.pages = self.height // 8 self.buffer = bytearray(self.pages * self.width) super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) self.init_display() def init_display(self): for cmd in ( SET_DISP, # display off # address setting SET_MEM_ADDR, 0x00, # horizontal # resolution and layout SET_DISP_START_LINE, # start at line 0 SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 SET_MUX_RATIO, self.height - 1, SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 SET_DISP_OFFSET, 0x00, SET_COM_PIN_CFG, 0x02 if self.width > 2 * self.height else 0x12, # timing and driving scheme SET_DISP_CLK_DIV, 0x80, SET_PRECHARGE, 0x22 if self.external_vcc else 0xF1, SET_VCOM_DESEL, 0x30, # 0.83*Vcc # display SET_CONTRAST, 0xFF, # maximum SET_ENTIRE_ON, # output follows RAM contents SET_NORM_INV, # not inverted # charge pump SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14, SET_DISP | 0x01, # display on ): # on self.write_cmd(cmd) self.fill(0) self.show() def poweroff(self): self.write_cmd(SET_DISP) def poweron(self): self.write_cmd(SET_DISP | 0x01) def contrast(self, contrast): self.write_cmd(SET_CONTRAST) self.write_cmd(contrast) def invert(self, invert): self.write_cmd(SET_NORM_INV | (invert & 1)) def rotate(self, rotate): self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3)) self.write_cmd(SET_SEG_REMAP | (rotate & 1)) def show(self): x0 = 0 x1 = self.width - 1 if self.width == 64: # displays with width of 64 pixels are shifted by 32 x0 += 32 x1 += 32 self.write_cmd(SET_COL_ADDR) self.write_cmd(x0) self.write_cmd(x1) self.write_cmd(SET_PAGE_ADDR) self.write_cmd(0) self.write_cmd(self.pages - 1) self.write_data(self.buffer) class SSD1306_I2C(SSD1306): def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): self.i2c = i2c self.addr = addr self.temp = bytearray(2) self.write_list = [b"\x40", None] # Co=0, D/C#=1 super().__init__(width, height, external_vcc) def write_cmd(self, cmd): self.temp[0] = 0x80 # Co=1, D/C#=0 self.temp[1] = cmd self.i2c.writeto(self.addr, self.temp) def write_data(self, buf): self.write_list[1] = buf self.i2c.writevto(self.addr, self.write_list) class SSD1306_SPI(SSD1306): def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): self.rate = 10 * 1024 * 1024 dc.init(dc.OUT, value=0) res.init(res.OUT, value=0) cs.init(cs.OUT, value=1) self.spi = spi self.dc = dc self.res = res self.cs = cs import time self.res(1) time.sleep_ms(1) self.res(0) time.sleep_ms(10) self.res(1) super().__init__(width, height, external_vcc) def write_cmd(self, cmd): self.spi.init(baudrate=self.rate, polarity=0, phase=0) self.cs(1) self.dc(0) self.cs(0) self.spi.write(bytearray([cmd])) self.cs(1) def write_data(self, buf): self.spi.init(baudrate=self.rate, polarity=0, phase=0) self.cs(1) self.dc(1) self.cs(0) self.spi.write(buf) self.cs(1)
创建SSD1306.py文件把代码复制进去就行了
二、主要实现代码
main.py
import machine import random from machine import Pin,SPI from SSD1306 import SSD1306_SPI import time oled = SSD1306_SPI(128, 64,SPI(2, 80000000), Pin(22), Pin(19), Pin(5))#驱动屏幕 but = Pin(13,Pin.IN)#定义按键 get_time = time.time()#显示用到的游戏时间 game_time = time.time()#用于 jump = 0#刷新率为40,跳起时每秒加1,等效于计时器作用(写的时候还不会计时器) jump_num = [0,8,15,21,25,29,32,34,35]#跳起高度,类似于二次方程,模拟重力加速度 down = False#用于判断jump自加还是自减 score = 0#分数 gameover = False#判断游戏结束 far = 0#距离,很多用处 ''' 障碍物的想法是每个障碍物之间最少距离40,宽度8,三个总长超过 屏幕宽度128,这样就不会同时出现四个,所以只创建三个,当第1 个障碍物跑完屏幕的同时将此时的第2个障碍物定义为第1个,第3个 定义为第2个,然后再创建第3个。就做到实际上就三个障碍物轮番跑 ''' box1 = random.randint(40,120) box2 = random.randint(40,120) + box1 box3 = random.randint(40,120) + box2 speed = 2#速度(障碍物每帧移动的像素点) while True: if not gameover: #触发跳起动作 if but.value() and jump == 0: jump = 1 if jump != 0: jump += 1 if not down else -1 if jump == 0: down = False score += 1 if jump == 8: down = True#跳到最高点下落 far += speed#移动的距离按速度累加(实际是障碍物移动的距离) score = far//30#本来分数==时间然后感觉太蠢了就随便写个东西,直接用far太容易把字符挤出了 speed = 2 + score//100#每过100分速度加1 oled.fill(0)#我用的SSD1306太捞了没有clear函数T^T oled.line(0,63,128,63,1)#最底下那条线 #画分数和时间 oled.text('SCORE:'+str(score),0,1) get_time = 'TIME:'+str(int(time.time()-game_time)) oled.text(get_time,140-len(get_time)*10,1) #画小恐龙的外形 for i in range(6): oled.line(15-i,51-jump_num[jump],15-i,58-jump_num[jump],1) oled.line(17-i,51-jump_num[jump],17-i,54-jump_num[jump],1) oled.line(11-i,55-jump_num[jump],11,58-jump_num[jump],1) oled.line(14,55-jump_num[jump],14,60-jump_num[jump],1) oled.line(10,55-jump_num[jump],10,60-jump_num[jump],1) oled.line(14,52-jump_num[jump],14,53-jump_num[jump],0) oled.line(13,52-jump_num[jump],13,53-jump_num[jump],0) #画障碍物 for i in range(8): oled.line(i+box1-far,55,i+box1-far,61,1) for i in range(8): oled.line(i+box2-far,55,i+box2-far,61,1) for i in range(8): oled.line(i+box3-far,55,i+box3-far,61,1) #判断当前障碍物过了屏幕,就把它变最后一个 if box1+8-far <= 0: box1 = box2 box2 = box3 box3 = box2 + random.randint(40,120) #判断是否碰到障碍物 if 14 > box1-far > 2 and 60-jump_num[jump] > 55: gameover = True else: oled.text('GAME_OVER',128//2-35,30) #再次按下按钮初始化并重新开始 if but.value(): score = 0 game_time = time.time() gameover = False far = 0 box1 = random.randint(50,120) box2 = random.randint(50,120) + box1 box3 = random.randint(50,120) + box2 speed = 2 continue time.sleep(1/40) oled.show()
代码不难,比较难想的点就是障碍物的生成,实际上只生成三个障碍物,每两个距离随机,最少40,也就是屏幕上不可能同时出现4个,当第一个消失的时候实际上此时第二个变为了第一个,第三个变为第二个,同时第一个变成第三个,如图
三、操作演示
本文引用文档:
SSD1306驱动:ESP32开发之旅——ssd1306 OLED屏的使用 by 未来程序猿小吕
感谢大佬的传授
来源:https://blog.csdn.net/zcy2333/article/details/125848327
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0090539455413818 seconds