|
楼主 |
发表于 2020-7-6 16:28
|
显示全部楼层
17、石头剪刀布(阿勇)
- #MicroPython动手做(35)——小游戏
- #石头剪刀布
- from mpython import *
- import time
- import random
- from machine import Timer
- def on_button_a_down(_):
- global y, x, k, j
- time.sleep_ms(10)
- if button_a.value() == 1: return
- x = random.randint(1, 3)
- xianshi(x, 10)
- def on_button_b_down(_):
- global y, x, k, j
- time.sleep_ms(10)
- if button_b.value() == 1: return
- y = random.randint(1, 3)
- xianshi(y, 90)
- _status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
- def on_touchpad_P_pressed():pass
- def on_touchpad_P_unpressed():pass
- def on_touchpad_Y_pressed():pass
- def on_touchpad_Y_unpressed():pass
- def on_touchpad_T_pressed():pass
- def on_touchpad_T_unpressed():pass
- def on_touchpad_H_pressed():pass
- def on_touchpad_H_unpressed():pass
- def on_touchpad_O_pressed():pass
- def on_touchpad_O_unpressed():pass
- def on_touchpad_N_pressed():pass
- def on_touchpad_N_unpressed():pass
- tim12 = Timer(12)
- def timer12_tick(_):
- global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
- try:
- touchPad_P.read();pass
- except:
- return
- if touchPad_P.read() < 400:
- if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
- elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
- if touchPad_Y.read() < 400:
- if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
- elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
- if touchPad_T.read() < 400:
- if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
- elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
- if touchPad_H.read() < 400:
- if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
- elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
- if touchPad_O.read() < 400:
- if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
- elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
- if touchPad_N.read() < 400:
- if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
- elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()
- tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)
- def on_touchpad_P_pressed():
- global y, x, k, j
- x = 0
- y = 0
- oled.fill(0)
- oled.DispChar("玩家1按A键玩家2按B键", 0, 0, 1)
- oled.show()
- def xianshi(j, k):
- global y, x
- if j == 1:
- oled.blit(image_picture.load('face/rock_s.pbm', 0), k, 17)
- if j == 2:
- oled.blit(image_picture.load('face/scissors_s.pbm', 0), k, 17)
- if j == 3:
- oled.blit(image_picture.load('face/paper_s.pbm', 0), k, 17)
- oled.show()
- def panduan():
- global y, x, k, j
- if x == y:
- oled.DispChar(" 打平", 50, 50, 1)
- else:
- if x - y == -1 or x - y == 2:
- oled.DispChar("玩家1赢", 50, 50, 1)
- else:
- oled.DispChar("玩家2赢", 50, 50, 1)
- oled.show()
- random.seed(time.ticks_cpu())
- button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
- button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
- image_picture = Image()
- oled.fill(0)
- oled.DispChar(" 石头剪刀布小游戏", 0, 0, 1)
- oled.DispChar("玩家1按A键玩家2按B键", 0, 16, 1)
- oled.DispChar(" 系统自动判断谁输谁赢", 0, 32, 1)
- oled.DispChar(" 按P键开始玩", 0, 48, 1)
- oled.show()
- x = 0
- y = 0
- while True:
- if x != 0 and y != 0:
- panduan()
复制代码
|
|