from machine import Pin, I2C, RTC, Timer
import ssd1306
import time
import machine
import random
from machine import ADC, Pin
#圖形
#character01 剪刀
#0x00, 0x00, 0x06, 0x30, 0x06, 0x70, 0x06, 0x70, 0x06, 0x70, 0x07, 0x70, 0x0f, 0x70, 0x0f, 0xf0, 0x37, 0x18, 0x3b, 0x7c, 0x3f, 0x7c, 0x1c, 0x78, 0x08, 0x18, 0x0c, 0x10, 0x07, 0xe0, 0x00, 0x00
#character02 石頭
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0xb0, 0x1f, 0xf8, 0x3f, 0xf8, 0x2d, 0xec, 0x2d, 0xdc, 0x2d, 0xbc, 0x2f, 0x7c, 0x2f, 0x78, 0x17, 0xf8, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00
#character03 布
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x06, 0xcc, 0x36, 0xcc, 0x36, 0xd8, 0x33, 0xd8, 0x1b, 0xf0, 0x3f, 0xf6, 0x3f, 0x8e, 0x3f, 0x7e, 0x1f, 0x7c, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00
#character_icon01 剪刀
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x03, 0xc0, 0x04, 0x60, 0x04, 0xe0, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#character_icon02 石頭
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x0f, 0xf0, 0x0a, 0xb0, 0x0a, 0xb0, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#character_icon03 布
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0xa0, 0x0a, 0xa0, 0x0a, 0xa0, 0x0f, 0xe8, 0x0f, 0xf0, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#character_vs
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x41, 0x80, 0x21, 0x38, 0x22, 0x66, 0x26, 0x42, 0x14, 0x30, 0x1c, 0x18, 0x1c, 0x88, 0x08, 0x88, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00
#character_vs2
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x80, 0x41, 0x00, 0x21, 0x3c, 0x22, 0x44, 0x32, 0x60, 0x16, 0x30, 0x14, 0x08, 0x0c, 0x08, 0x08, 0x88, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00
#character_life 生命值
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x30, 0x12, 0x78, 0x17, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x0f, 0xf0, 0x0f, 0xf0, 0x07, 0xe0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#character_stick 搖桿0,右,上,左
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x40, 0x05, 0xa0, 0x05, 0xa0, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x40, 0x04, 0xe0, 0x04, 0xe0, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x03, 0xc0, 0x05, 0xa0, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x40, 0x07, 0x20, 0x07, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#character_effect1 霸氣
#character_effect2 霸氣2
#character_A
#character_B
#character_pause 暫停
# 函數指定
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
vert = machine.ADC(12)
horz = machine.ADC(14)
vert.atten(ADC.ATTN_11DB)
horz.atten(ADC.ATTN_11DB)
button_A = machine.Pin(19, machine.Pin.IN, machine.Pin.PULL_UP)
button_B = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_UP)
button_stateA = 1
button_stateB = 1
button_press_timeA = 0
button_press_timeB = 0
long_press_threshold = 2000
current_timeA = time.ticks_ms()
current_timeB = time.ticks_ms()
#0=press 1=unpress
previous_answers = []
previous_count = 1
# 定义中断回调函数
def button_callback1(pin):
global button_stateA, button_press_timeA, current_timeA
current_timeA = time.ticks_ms()
button_stateA = pin.value()
if button_stateA == 0:
button_press_timeA = current_timeA
def button_callback2(pin):
global button_stateB, button_press_timeB, current_timeB
current_timeB = time.ticks_ms()
button_stateB = pin.value()
if button_stateB == 0:
button_press_timeB = current_timeB
button_A.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=button_callback1)
button_B.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=button_callback2)
def draw_character(data, x, y):
for row in range(16): # 遍歷每一行,因為我們的字型是16x16的點陣圖
for byte_index in range(2): # 因為我們是以8位的byte來存儲,一行需要2個byte(16位元)
# 根據行數和位元組索引來計算我們在資料中的位置
byte_val = data[row * 2 + byte_index]
# 計算X軸的起始位置。我們知道每個位元組有8個點,所以乘以8
x_0 = x + byte_index * 8
# 計算Y軸的位置,因為我們在遍歷每一行時,y座標會增加
y_0 = y + row
for bit in range(8): # 遍歷位元組中的每一位
# 這裡我們使用一個遮罩來檢查byte_val中的特定位元是否為1。
# 1 << (7 - bit) 會產生如下的遮罩: 10000000, 01000000, ... , 00000001
# 然後我們用位元與運算(&)來檢查byte_val的這一位是否被設為1。
if byte_val & (1 << (7 - bit)):
oled.pixel(x_0 + bit, y_0, 1) # 如果是1,則在OLED上繪製一個點
print('1', end='') # 也在終端上印出1
else:
print(' ', end='') # 如果不是,則在終端上印出空格
print() # 在終端上換行,以對應到下一行的繪製
def random_choice_lim(gestures):
global previous_answers
esp32_choice = None
if change_frequency == 0:
esp32_choice = random.choice(gestures)
elif change_frequency <= 10 :
if previous_answers and previous_count >= 1 and lv <= 10 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 10 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 15 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 9 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 20 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 8 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 25 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 7 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 30 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 6 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 35 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 5 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 40 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 4 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 45 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 3 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 50 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 2 :
previous_count = 1
esp32_choice = random.choice(gestures)
if previous_answers and previous_count >= 1 and lv <= 60 :
esp32_choice = random.choice(previous_answers)
previous_count += 1
if previous_count >= 1 :
previous_count = 1
esp32_choice = random.choice(gestures)
else :
esp32_choice = random.choice(gestures)
# 将当前回合的选择添加到答案列表
previous_answers.append(esp32_choice)
return esp32_choice
#剪刀石頭布
character01 = [
0x00, 0x00, 0x06, 0x30, 0x06, 0x70, 0x06, 0x70, 0x06, 0x70, 0x07, 0x70, 0x0f, 0x70, 0x0f,0xf0, 0x37, 0x18, 0x3b, 0x7c, 0x3f, 0x7c, 0x1c, 0x78, 0x08, 0x18, 0x0c, 0x10, 0x07, 0xe0, 0x00, 0x00]
character02 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0xb0, 0x1f, 0xf8, 0x3f, 0xf8, 0x2d, 0xec, 0x2d, 0xdc, 0x2d, 0xbc, 0x2f, 0x7c, 0x2f, 0x78, 0x17, 0xf8, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00]
character03 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x06, 0xcc, 0x36, 0xcc, 0x36, 0xd8, 0x33, 0xd8, 0x1b, 0xf0, 0x3f, 0xf6, 0x3f, 0x8e, 0x3f, 0x7e, 0x1f, 0x7c, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00]
#剪刀石頭布icon
character_icon01 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x03, 0xc0, 0x04, 0x60, 0x04, 0xe0, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_icon02 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x0f, 0xf0, 0x0a, 0xb0, 0x0a, 0xb0, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_icon03 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0xa0, 0x0a, 0xa0, 0x0a, 0xa0, 0x0f, 0xe8, 0x0f, 0xf0, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
#vs特效
character_vs = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x41, 0x80, 0x21, 0x38, 0x22, 0x66, 0x26, 0x42, 0x14, 0x30, 0x1c, 0x18, 0x1c, 0x88, 0x08, 0x88, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00]
character_vs2 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x80, 0x41, 0x00, 0x21, 0x3c, 0x22, 0x44, 0x32, 0x60, 0x16, 0x30, 0x14, 0x08, 0x0c, 0x08, 0x08, 0x88, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00]
#搖桿圖示
character_stick0 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x40, 0x05, 0xa0, 0x05, 0xa0, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_stick01 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x40, 0x04, 0xe0, 0x04, 0xe0, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_stick02 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x03, 0xc0, 0x05, 0xa0, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_stick03 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x40, 0x07, 0x20, 0x07, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
#生命值
character_life01 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x30, 0x12, 0x78, 0x17, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x0f, 0xf0, 0x0f, 0xf0, 0x07, 0xe0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_life02 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x30, 0x12, 0x78, 0x17, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x0f, 0xf0, 0x0f, 0xf0, 0x07, 0xe0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_life03 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x30, 0x12, 0x78, 0x17, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x0f, 0xf0, 0x0f, 0xf0, 0x07, 0xe0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
character_Q = [
0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x07, 0xe0, 0x0e, 0x70, 0x08, 0x30, 0x08, 0x30, 0x04, 0x60, 0x00, 0xc0, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00]
#oled.fill(0)
lv = 0
life = 3
win_count = 0
#邏輯空間
icon01 = character_icon01
icon02 = character_icon02
icon03 = character_icon03
# 定义手势和结果
gestures = ["SEISOR","ROCK", "PAPER"]
results = ["TIE", "WIN", "LOSE"]
player_choice = None
com_choice = (character_Q)
p1_choice = (character_Q)
# 初始化方向變數
direction = None
game_running = False
# 初始化游戏参数
dif = 0 # 初始难度
max_dif = 10 # 最大难度
change_frequency = 10 # 初始换拳频率,每5轮换拳一次
round_counter = 0 # 轮数计数器
round_count = 0
while True:
press_durationA = current_timeA - button_press_timeA
press_durationB = current_timeB - button_press_timeB
# 等待玩家按下按钮
if button_stateB == 1:
oled.fill(0)
oled.text('START' ,0, 0)
oled.text('press B', 38, 28)
oled.show()
# 等待按钮按下
elif button_stateB == 0 and press_durationB <= long_press_threshold :
# 清空屏幕
oled.fill(0)
oled.show()
break
while True:
# 讀取搖桿的X和Y值
x_value = vert.read()
y_value = horz.read()
current_timeA = time.ticks_ms()
current_timeB = time.ticks_ms()
press_durationA = current_timeA - button_press_timeA
press_durationB = current_timeB - button_press_timeB
# 判斷方向
if x_value > 3000:
direction = (character_stick01)
icon_x = 1
icon_y = 0
icon01_x = 1
icon01_y = 0
icon02_x = 0
icon02_y = 0
icon03_x = 0
icon03_y = 0
choice = gestures[0]
p1_character = (character01)
elif y_value < 200:
direction = (character_stick02)
icon_x = 0
icon_y = -1
icon02_x = 0
icon02_y = -1
icon01_x = 0
icon01_y = 0
icon03_x = 0
icon03_y = 0
choice = gestures[1]
p1_character = (character02)
elif x_value < 200:
direction = (character_stick03)
icon_x = -1
icon_y = 0
icon03_x = -1
icon03_y = 0
icon02_x = 0
icon02_y = 0
icon01_x = 0
icon01_y = 0
choice = gestures[2]
p1_character = (character03)
else:
direction = (character_stick0)
icon_x = 0
icon_y = 0
icon01_x = 0
icon01_y = 0
icon03_x = 0
icon03_y = 0
icon02_x = 0
icon02_y = 0
choice = None
p1_character = (character_Q)
#player
draw_character(direction, 91+(icon_x), 50+(icon_y), )
draw_character(com_choice, 24, 20)
draw_character(p1_choice, 90, 20)
draw_character(icon01, 103-(icon01_x), 50-(icon01_y))
draw_character(icon02, 91-(icon02_x), 38-(icon02_y))
draw_character(icon03, 78-(icon03_x), 50-(icon03_y),)
#icon
oled.text('LIFE', 2, 2)
draw_character(character_life01, 35, -3)
draw_character(character_life02, 47, -3)
draw_character(character_life03, 59, -3)
oled.text('LV', 85, 2, )
oled.text(str(lv), 102, 2,)
oled.text('rnd', 2, 55)
oled.text(str(round_count), 28, 55)
#effect
draw_character(character_vs, 57, 20)
draw_character(character_vs2, 57, 20)
oled.show()
print(vert.read())
print(horz.read())
#??time.sleep(0.01)
oled.fill(0)
if not game_running:
if button_stateA == 1 or choice is None:
com_choice = character_Q
elif button_stateA == 0 and press_durationA <= long_press_threshold and choice is not None:
player_choice = choice
p1_choice = p1_character
game_running = True
if game_running:
if random_choice_lim(gestures) == gestures[0] :
com_choice = (character01)
elif random_choice_lim(gestures) == gestures[1] :
com_choice = (character02)
elif random_choice_lim(gestures) == gestures[2] :
com_choice = (character03)
# 打印玩家和ESP32的选择
print(player_choice) # 你可以根据按钮的不同状态进行选择
print("esp32"+ random_choice_lim(gestures))
# 判断胜负
if player_choice == random_choice_lim(gestures):
result = 0 # 平局
elif (player_choice == gestures[0] and random_choice_lim(gestures) == gestures[2]) or \
(player_choice == gestures[1] and random_choice_lim(gestures) == gestures[0]) or \
(player_choice == gestures[2] and random_choice_lim(gestures) == gestures[1]):
result = 1 # 玩家赢
win_count += 1 #勝場數
lv += 1
round_counter += 1
else:
result = 2 # ESP32赢
life -= 1 #少一命
# 打印结果
print(results[result])
round_count += 1
# 根据计数器来更新难度和换拳频率
if round_counter >= change_frequency:
round_counter = 0
if dif < max_dif :
dif += 1
change_frequency = 10 - dif # 换拳频率随难度增加
if win_count >= 3 :
life += 1
game_running = False
#顯示測試
# draw_character(character01, 90, 20, )
# draw_character(character02, 24, 20,)
#draw_character(character03, 24, 20,)
# draw_character(icon01, 68, 50)
# draw_character(icon02, 56, 38)
# draw_character(icon03, 43, 50,)
# draw_character(character_vs, 57, 20)
#draw_character(character_vs2, 57, 20)
#draw_character(character_stick0, 56, 50)
# draw_character(character_stick01, 57, 50)
#draw_character(character_stick02, 56, 49)
#draw_character(character_stick03, 55, 50)
# oled.text('LIFE', 2, 2)
# draw_character(character_life01, 35, -3)
# draw_character(character_life02, 47, -3)
# draw_character(character_life03, 59, -3)
# oled.text('LV', 85, 2, )
# oled.text(lv, 102, 2,)
# Refresh the OLED to see the character
oled.show()