You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

'''
深圳市普中科技有限公司PRECHIN 普中)
技术支持www.prechin.net
PRECHIN
普中
实验名称:按键控制实验
接线说明OLED(IIC)液晶模块-->ESP32 IO
VCC --> 5V
GND --> GND
SCL --> 18
SDA --> 23
按键模块-->ESP32 IO
K1 --> 14
实验现象程序下载成功后按下K1操作小恐龙的起跳
'''
# 导入
from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
import time, random
# 初始化IIC-OLED显示屏
i2c = SoftI2C(sda=Pin(23),scl=Pin(18))
oled = SSD1306_I2C(128,64,i2c,addr=0x3c)
# 创建按键对象
K1 = Pin(14,Pin.IN,Pin.PULL_UP)
# 创建游戏时间
get_time = time.time()
game_time = time.time()
# 刷新率为40跳起时每秒加1
jump = 0
jump_num = [0,8,15,21,25,29,32,34,35] # 跳起高度
down = False # 用于判断jump自加还是自减
# 分数
score = 0
# 生命值
life = 3
# 判断游戏结束
game = True
# 距离
far = 0
# 障碍物方块距离随机生成30到50像素之间
box1 = random.randint(30,40)
box2 = random.randint(30,40) + box1
box3 = random.randint(30,40) + box2
box4 = random.randint(30,40) + box3
speed = 2 # 速度(障碍物每帧移动的像素点)
# 循环
while True:
if game:
# 触发跳起动作
if K1.value()==0 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 # 移动的距离按速度累加(实际是障碍物移动的距离)
oled.fill(0) # 清空屏幕显示
oled.line(0,63,128,63,1) # 底线(屏幕最下边游戏线条)
# 显示生命值、分数和时间
oled.text('LF:'+str(life),0,1)
oled.text('SC:'+str(score),45,1)
get_time = 'S:'+str(int(time.time()-game_time))
oled.text(get_time,95,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)
for i in range(8):
oled.line(i+box4-far,55,i+box4-far,61,1)
#判断当前障碍物过了屏幕,就把它变最后一个
if box1+8-far <= 0:
box1 = box2
box2 = box3
box3 = box4
box4 = box3 + random.randint(30,40)
#判断是否碰到障碍物
if 14 > box1-far > 2 and 60-jump_num[jump] > 55:
life -= 1
if life:
jump = 0
far = 0
box1 = random.randint(30, 40)
box2 = random.randint(30, 40) + box1
box3 = random.randint(30, 40) + box2
box4 = random.randint(30, 40) + box3
else:
game = False
score = 0
else:
oled.fill_rect(0,1,45,10,0)
oled.text('LF:'+str(life),0,1)
oled.text('SC:'+str(score),45,1)
oled.text('GAME_OVER',128//2-40,30)
# 游戏结束之后,再次按下按键等待两秒钟再重新开始游戏
if K1.value()==0:
time.sleep(2)
game_time = time.time()
game = True
far = 0
life = 3
box1 = random.randint(30,40)
box2 = random.randint(30,40) + box1
box3 = random.randint(30,40) + box2
box4 = random.randint(30,40) + box3
speed = 2
continue
time.sleep(1/40)
oled.show()