目錄
簡介
效果展示
源代碼
main.py?
ssd1306.py
實現思路
血量值
分數
恐龍
障礙物
得分與血量值的計算
簡介
使用合宙esp32c3模塊,基于micropython平臺開發的一款oled小游戲,恐龍快跑,所有代碼已經給出,將兩個py文件放進esp32c3里即可運行,使用的是硬件i2c,這個ssd1306.py文件是我優化過的,許多用法可查看源碼即可推敲,只支持128*64的I2C oled一定要用我提供的ssd1306驅動。
效果展示
esp32 micropython oled恐龍快跑
源代碼
main.py?
from ssd1306 import SSD1306_I2C
from machine import Pin,I2C,ADC
import time
import randomi2c=I2C(0,scl=Pin(5),sda=Pin(4))
oled=SSD1306_I2C(i2c)ps2x=ADC(Pin(2))
ps2x.atten(ADC.ATTN_11DB)
#生命值的繪制
live_list=[0x30,0x4C,0x42,0x21,0x21,0x42,0x4C,0x30]
#繪制恐龍
dinosaur=[0x03,0x01,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x7F,0xFF,0x8F,0xAF,0x8D,0xF9,0x78,
0xC0,0xE0,0xF0,0x78,0x7F,0xFD,0xF0,0xFC,0xFF,0xF9,0xF0,0xE0,0xC0,0x00,0x80,0x00]
#初始化屏幕
oled.fill(0)
oled.p8(live_list,0,0)
oled.p8(live_list,9,0)
oled.p8(live_list,18,0)
oled.text('0',120,0)
oled.p16(dinosaur,0,48)
oled.rect(117,53,10,10,True)
oled.show()
#設定游戲參數,一次跳躍用12幀數據處理
jump = 0
jump_num = [48,34,23,15,10,8]#根據恐龍的跳躍位置和自由落體運動規律得到的每幀繪制恐龍的位置
fall = False#降落標志
score = 0#分數
x = 0
live=3#生命值為3
#障礙物隨機坐標
box1 = random.randint(40,120)
box2 = random.randint(40,120) + box1
speed = 3#初始游戲速度while True:#如果還有生命值if live:#當搖桿推動且恐龍在地平線上才觸發跳動if ps2x.read()>2500 and jump == 0:jump = 1#一直升高直到最高點if jump != 0:jump += 1 if not fall else -1#降落到地面if jump == 0:fall = Falsescore += 1if jump == 5:fall = True#跳到最高點下落x += speedspeed = 3 + score//10#速度隨積分增加#刷新屏幕oled.fill(0)#顯示血量值for i in range(live):oled.p8(live_list,i*9,0)#更新分數oled.text(str(score),120,0)#畫小恐龍的外形oled.p16(dinosaur,0,jump_num[jump])#畫障礙物oled.rect(box1-x,55,4,8,True)oled.rect(box2-x,55,4,8,True)#判斷當前障礙物過了屏幕,就把它變最后一個if box1+4-x <= 0:box1 = box2 box2 = box1 + random.randint(40,120)#判斷是否碰到障礙物if 15 > box1-x > 12 and 48-jump_num[jump] < 14:live-=1score-=1time.sleep_ms(20)elif 0<=box1-x<4 and 48-jump_num[jump] < 14:live-=1#生命值為0,恐龍掛掉了else:oled.text('GAME_OVER',29,30)oled.show()
ssd1306.py
import framebuf
# 寄存器定義
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xa4)
SET_NORM_INV = const(0xa6)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_DISP = const(0xae)
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)class SSD1306:def __init__(self,external_vcc):self.width = 128self.height = 64self.external_vcc = external_vccself.pages = 8self.init_display()def init_display(self):for cmd in (SET_DISP | 0x00, #熄屏SET_MEM_ADDR, 0x00, #水平尋址SET_DISP_START_LINE | 0x00,#顯示起始行地址SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0SET_MUX_RATIO, 63,SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0SET_DISP_OFFSET, 0x00,SET_COM_PIN_CFG, 0x12,# timing and driving schemeSET_DISP_CLK_DIV, 0x80,SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,SET_VCOM_DESEL, 0x30, # 0.83*Vcc# displaySET_CONTRAST, 0xff, # maximumSET_ENTIRE_ON, # output follows RAM contentsSET_NORM_INV, # not inverted# charge pumpSET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,0x2e, # 禁止滾動0xae | 0x01): #開屏self.write_cmd(cmd)self.fill(0)self.show()def v_scroll(self, d=1): self.write_cmd(0x2e) # 關閉滾動if d:self.write_cmd(0x26) # 向左self.write_cmd(0x00)self.write_cmd(0x07) # 起始頁self.write_cmd(0x00) # 滾動幀率self.write_cmd(0x00) # 結束頁else:self.write_cmd(0x27) # 向左self.write_cmd(0x00)self.write_cmd(0x00) # 起始頁self.write_cmd(0x00) # 滾動幀率self.write_cmd(0x07) # 結束頁self.write_cmd(0x00)self.write_cmd(0xff)self.write_cmd(0x2f) # 開啟滾動def poweroff(self):self.write_cmd(const(0xae) | 0x00)#熄屏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 show(self):self.write_cmd(SET_COL_ADDR)self.write_cmd(0)self.write_cmd(127)self.write_cmd(SET_PAGE_ADDR)self.write_cmd(0)self.write_cmd(63)self.write_framebuf()def fill(self, c):self.framebuf.fill(c)def pixel(self, x, y, c):self.framebuf.pixel(x, y, c)def text(self, string, x, y, c=1):self.framebuf.text(string, x, y, c)def hline(self,x,y,w,c=1):self.framebuf.hline(x,y,w,c)def vline(self,x,y,h,c=1):self.framebuf.vline(x,y,h,c)def line(self,x1,y1,x2,y2,c=1):self.framebuf.line(x1,y1,x2,y2,c)def rect(self,x,y,w,h,c=1,f=False):self.framebuf.rect(x,y,w,h,c,f)def ellipse(self,x,y,xr,yr,c,f=False,m=15):self.framebuf.ellipse(x,y,xr,yr,c,f,m)def cube(self,x,y,l):self.rect(x,y,l,l)self.rect(x+int(0.5*l),int(y-0.5*l),l,l)self.line(x,y,int(x+0.5*l),int(y-0.5*l),1)self.line(x+l,y,int(x+1.5*l),int(y-0.5*l),1)self.line(x,y+l,int(x+0.5*l),int(y+0.5*l),1)self.line(x+l,y+l,int(x+1.5*l),int(y+0.5*l),1)def p8(self,page,x,y):for e in range(8):byte=bin(page[e]).replace('0b','')while len(byte)<8:byte='0'+bytefor i in range(8):if byte[i]=='1':self.pixel(x+e,y+i,int(byte[i]))def p16(self,page,x,y):for e in range(32):byte=bin(page[e]).replace('0b','')while len(byte)<8:byte='0'+bytefor i in range(8):if byte[i] and e<16:self.pixel(x+e,y+i,int(byte[i]))elif byte[i] and e>=16:self.pixel(x-16+e,y+8+i,int(byte[i]))def p32(self,page,x,y):for e in range(128):byte=bin(page[e]).replace('0b','')while len(byte)<8:byte='0'+bytefor i in range(8):if byte[i] and e<32:self.pixel(x+e,y+i,int(byte[i]))elif byte[i] and 32<=e<64:self.pixel(x+e-32,y+8+i,int(byte[i]))elif byte[i] and 64<=e<96:self.pixel(x+e-64,y+16+i,int(byte[i]))elif byte[i] and 96<=e<128:self.pixel(x+e-96,y+24+i,int(byte[i]))class SSD1306_I2C(SSD1306):def __init__(self,i2c, addr=0x3c, external_vcc=False):self.i2c = i2cself.addr = addrself.temp = bytearray(2)# buffer需要8 * 128的顯示字節加1字節命令self.buffer = bytearray(8 * 128 + 1)self.buffer[0] = 0x40 # Co=0, D/C=1self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], 128, 64)super().__init__(external_vcc)def write_cmd(self, cmd):self.temp[0] = 0x80 # Co=1, D/C#=0self.temp[1] = cmdself.i2c.writeto(self.addr, self.temp)def write_framebuf(self):self.i2c.writeto(self.addr, self.buffer)
實現思路
在python框架下開發游戲,思路很重要
血量值
用了8*8的矩形取模,是一個小愛心,代表血量值,在oled的左上角展示
分數
設置了游戲獎勵分數,位于oled右上角
恐龍
擁有跳躍功能,符合自由落體運動規律,采用16*16取模方式構建,在oled左下角。恐龍跳起來分為6幀,落地也分為6幀,就是恐龍從地面跳到最高點時,刷新6次,用6個圖像展示動態過程。根據自由落體運動規律,6幀平均每幀時間相同,由于恐龍只在y軸上運動,幀與幀之間的距離為1:3:5:7:9,用一個列表來記錄這個規律,計算每一幀恐龍y坐標的位置,從最高點向下運動也是一樣的。
障礙物
用4*8的矩形表示,設置兩個并排的矩形,分別用隨機數將其位置初始化,由于矩形只在x軸上移動,只需不停的移動矩形,改變矩形的x坐標即可,由于在恐龍的處理設置了12幀,不妨將障礙物一起嵌入這12幀中,在每一幀減小矩形的x坐標,每一幀減小的數量關系到矩形的移動速度。為了設置難度,把矩形移動速度與積分掛鉤,積分越多,移動速度越大,難度越大,游戲越有意思,當然也可以用隨機數處理,忽快忽慢,也很有意思。定義了兩個矩形,(也可以根據個人感覺多定義幾個),分別編號1,2,接近恐龍的為1號,可以實時計算矩形的坐標來檢查矩形是否出界。分別用隨機數表示兩個矩形出現的位置,并把2號矩形加在1號矩形的后面,以隨機數為它們之間的距離,由于不斷減小矩形的坐標,并刷新,視覺上就看到了向左滾動的矩形,當第一個矩形出界后,就把2號矩形轉為1號,同時新定義1個2號矩形,以一個隨機數為距離加在1號矩形的后面,以此類推,就形成了一個迭代效果,源源不斷的矩形滾動。
得分與血量值的計算
我把得分加在落地的瞬間,只要落地就加1分,產生了一種很奇妙的效果,玩家為了多得分,就要多跳,又不能碰到障礙物,無形中加了難度和趣味。
由于恐龍只在y軸上移動,矩形只在x軸移動,它們交叉的區域就是碰撞區域,可以計算損失血量,但是程序是在一個循環里的,esp32c3運行速度很快,如果把0-15的區域都設為碰撞區,會導致一次碰撞損失很多血量,我想要一次碰撞損失1滴血,就把碰撞區域縮小到兩邊,對應跳起和降落碰到障礙物兩種情況。同時加了一定的延時使游戲感更好。
對于碰撞的判斷,就相當于車禍雙方,雙方有交叉區域才能判定發生碰撞,當障礙物處于交叉區域,恐龍的跳躍高度沒有跳出障礙區時,就判斷發生碰撞,血量值減1。
當血量值為0時,游戲結束。
如果你有更好的想法,大家一起交流