from pyb import *
from max7219 import Matrix8x8
import time
from tm1638 import TM1638
'''点阵'''
spi=SPI(2, SPI.MASTER, baudrate=1000000, polarity=1, phase=0, bits=8, firstbit=SPI.MSB)
cs=Pin("B12", Pin.OUT)
d=Matrix8x8(spi, cs, 4)
d.brightness(5)
#DIN——B15
#CS——B12
#CLK——B13
'''按键'''
KEY1 = Pin("PB3",Pin.IN,Pin.PULL_NONE)
KEY2 = Pin("PB4",Pin.IN,Pin.PULL_NONE)
KEY3 = Pin("PB5",Pin.IN,Pin.PULL_NONE)
KEY4 = Pin("PB6",Pin.IN,Pin.PULL_NONE)
'''p2数码管'''
stb2 = Pin("B9", Pin.OUT)
clk2 = Pin("B8", Pin.OUT)
dio2 = Pin("A15", Pin.OUT)
tm2 = TM1638(stb2, clk2, dio2, brightness=2)
'''p1数码管'''
stb1 = Pin("B10", Pin.OUT)
clk1 = Pin("B1", Pin.OUT)
dio1 = Pin("B0", Pin.OUT)
tm1 = TM1638(stb1, clk1, dio1, brightness=2)
'''按键'''
KEY1 = Pin("PB3",Pin.IN,Pin.PULL_NONE)
KEY2 = Pin("PB4",Pin.IN,Pin.PULL_NONE)
KEY3 = Pin("PB5",Pin.IN,Pin.PULL_NONE)
KEY4 = Pin("PB6",Pin.IN,Pin.PULL_NONE)
#蜂鸣器
tim = pyb.Timer(1, freq=1000)
beeper = pyb.Pin("A8", pyb.Pin.OUT_PP)
ch = tim.channel(1, pyb.Timer.PWM, pin=beeper)
ch.pulse_width_percent(0)
def feng(melody):
tones = {
"1": 262,
"2": 294,
"3": 330,
"4": 349,
"5": 392,
"6": 440,
"7": 494,
"-": 100,
}
for tone in melody:
freq1 = tones[tone]
if freq1 != 100:
tim.freq(freq1)
ch.pulse_width_percent(30)
else:
ch.pulse_width_percent(100) # 空拍时一样不上电
# 停顿一下 (四四拍每秒两个音,每个音节中间稍微停顿一下)
pyb.delay(35)
ch.pulse_width_percent(100) # 设备占空比为0,即不上电
pyb.delay(20)
#右边
#人
x=0
x1 = 0
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
R=0
def man1():
global x1
global x2
global x3
global x4
global x5
global x6
global x7
global R
x1 = x+0
x2 = x+2
x3 = x+1
x4 = x+1
x5 = x+1
x6 = x+1
x7 = x+2+R
d.pixel(x1,0,1)
d.pixel(x2,0,1)
d.pixel(x3,1,1)
d.pixel(x4,2,1)
d.pixel(x5,3,1)
d.pixel(x6,4,1)
d.pixel(x7,2,1)
d.show()
d.pixel(x1,0,0)
d.pixel(x2,0,0)
d.pixel(x3,1,0)
d.pixel(x4,2,0)
d.pixel(x5,3,0)
d.pixel(x6,4,0)
d.pixel(x7,2,0)
d.show()
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
#反转攻击标志位
a1 = 0#a1-4//3
a2 = 0#a2-6//4
a3 = 0#a3-8//5
a4 = 0#a4-10//6
#攻击1
def gj1():
man1()
global x8
global x9
global x10
global x11
global x12
global x13
global a1
global a2
global a3
global a4
x8 = x+3+a1
x9 = x+3+a1
x10 =x+3+a1
x11 =x+4+a2
x12 =x+5+a3
x13 =x+6+a4
d.pixel(x8,1,1)
d.pixel(x9,2,1)
d.pixel(x10,3,1)
d.pixel(x11,2,1)
d.pixel(x12,2,1)
d.pixel(x13,2,1)
d.show()
delay(50)
man1()
d.pixel(x8,1,0)
d.pixel(x9,2,0)
d.pixel(x10,3,0)
d.pixel(x11,2,0)
d.pixel(x12,2,0)
d.pixel(x13,2,0)
x14 =0
x15 =0
x16 =0
#抬手攻击1
def ct1():
man1()
global x14
global x15
global x16
global a1
global a2
global a3
x14 = x+3+a1
x15 = x+4+a2
x16 = x+5+a3
d.pixel(x14,2,1)
d.pixel(x15,3,1)
d.pixel(x16,4,1)
d.show()
delay(50)
man1()
d.pixel(x14,2,0)
d.pixel(x15,3,0)
d.pixel(x16,4,0)
x17 = 0
x18 = 0
x19 = 0
x20 = 0
x21 = 0
#防御1
def fy1():
man1()
global x17
global x18
global x19
global x20
global x21
global a1
global a2
x17 = x+3+a1
x18 = x+4+a2
x19 = x+4+a2
x20 = x+4+a2
x21 = x+3+a1
d.pixel(x17,4,1)
d.pixel(x18,3,1)
d.pixel(x19,2,1)
d.pixel(x20,1,1)
d.pixel(x21,2,1)
d.show()
delay(100)
man1()
d.pixel(x17,4,0)
d.pixel(x18,3,0)
d.pixel(x19,2,0)
d.pixel(x20,1,0)
d.pixel(x21,2,0)
#左边
#人
y = 0
y1 = 0
y2 = 0
y3 = 0
y4 = 0
y5 = 0
y6 = 0
y7 = 0
L=0
def man2():
global y1
global y2
global y3
global y4
global y5
global y6
global y7
global L
y1 = y+31
y2 = y+30
y3 = y+30
y4 = y+30
y5 = y+30
y6 = y+29
y7 = y+29+L
d.pixel(y1,0,1)
d.pixel(y2,1,1)
d.pixel(y3,2,1)
d.pixel(y4,3,1)
d.pixel(y5,4,1)
d.pixel(y6,0,1)
d.pixel(y7,2,1)
d.show()
d.pixel(y1,0,0)
d.pixel(y2,1,0)
d.pixel(y3,2,0)
d.pixel(y4,3,0)
d.pixel(y5,4,0)
d.pixel(y6,0,0)
d.pixel(y7,2,0)
y8 = 0
y9 = 0
y10 = 0
y11 = 0
y12 = 0
y13 = 0
b1 = 0#b1+10//25
b2 = 0#b2+8//26
b3 = 0#b3+6//27
b4 = 0#b4+4//28
#攻击
def gj2():
man2()
global y8
global y9
global y10
global y11
global y12
global y13
global b1
global b2
global b3
global b4
y8 =y+28+b4
y9 =y+28+b4
y10=y+28+b4
y11=y+27+b3
y12=y+26+b2
y13=y+25+b1
d.pixel(y8,1,1)
d.pixel(y9,2,1)
d.pixel(y10,3,1)
d.pixel(y11,2,1)
d.pixel(y12,2,1)
d.pixel(y13,2,1)
d.show()
delay(50)
man2()
d.pixel(y8,1,0)
d.pixel(y9,2,0)
d.pixel(y10,3,0)
d.pixel(y11,2,0)
d.pixel(y12,2,0)
d.pixel(y13,2,0)
y14 =0
y15 =0
y16 =0
#抬手攻击2
def ct2():
man2()
global y14
global y15
global y16
global b2
global b3
global b4
y14 = y+28+b4
y15 = y+27+b3
y16 = y+26+b2
d.pixel(y14,2,1)
d.pixel(y15,3,1)
d.pixel(y16,4,1)
d.show()
delay(50)
man2()
d.pixel(y14,2,0)
d.pixel(y15,3,0)
d.pixel(y16,4,0)
y17 = 0
y18 = 0
y19 = 0
y20 = 0
y21 = 0
#防御
def fy2():
man2()
global y17
global y18
global y19
global y20
global y21
global b3
global b4
y17 =y+28+b4
y18 =y+27+b3
y19 =y+27+b3
y20 =y+27+b3
y21 =y+28+b4
d.pixel(y17,4,1)
d.pixel(y18,3,1)
d.pixel(y19,2,1)
d.pixel(y20,1,1)
d.pixel(y21,2,1)
d.show()
delay(100)
man2()
d.pixel(y17,4,0)
d.pixel(y18,3,0)
d.pixel(y19,2,0)
d.pixel(y20,1,0)
d.pixel(y21,2,0)
#扣血 如果当武器的三个像素点击中躯干的任意一个像素点就可以可以算作击中,如果盾牌可以填补原先的像素点,就不算击中
#血条
z1=100#左边
z2=100#右边
c1="1-4"
c2="4-3"
n=1#用来计数key1的奇偶
while True:
if KEY4.value()==1:
for i in range(8):
tm1.led(i, 1)
tm2.led(i, 1)
time.sleep(0.25)
tm1.led(i, 0)
tm2.led(i, 0)
d.text("NIMn", 0, 0, 0)
z1=100#左边
z2=100#右边
x=0
y=0
man1()
man2()
continue
if KEY2.value()==1:#无声模式
c1="-"
c2="-"
if KEY3.value()==1:#有声模式
c1="1-4"
c2="4-3"
if KEY1.value()==1:
n = n + 1
if KEY1.value()==0:
n = n
if KEY4.value()==0:
if (n%2) == 0:
tm1.leds(0xFF)
tm2.leds(0xFF)
xx1=str(z2)
xx2=str(z1)
tm1.show(xx1)
tm2.show(xx2)
man1()
man2()
continue
else:
if z1>0 and z2>0:
tm1.leds(0xFF)
tm2.leds(0xFF)
xx1=str(z2)
xx2=str(z1)
tm1.show(xx1)
tm2.show(xx2)
button2=tm1.keys()
button1=tm2.keys()
man2()
man1()
if button2 ==1:
if y<0:
y = y+1
if button2 ==2:
if y>-29 :
y = y-1
if button2 ==4:
man2()
man1()
if y4>=x4:
L=0
b1 = 0#b1+10//25
b2 = 0#b2+8//26
b3 = 0#b3+6//27
b4 = 0#b4+4//28
ct2()
delay(20)
gj2()
feng(c1)
for j in range(y13,y11+1):
if button1 == 128:
z1=z1
else:
if j == x4:
z1=z1-1
elif j ==x7:
z1=z1-0.5
else:
z1=z1
else:
L=2
b1 = 10#b1+10//25
b2 = 8#b2+8//26
b3 = 6#b3+6//27
b4 = 4#b4+4//28
ct2()
delay(20)
gj2()
feng(c1)
for t in range(y11,y13+1):
man1()
if button1 == 128:
z1=z1
else:
if t == x4:
z1=z1-1
elif t ==x7:
z1=z1-0.5
else:
z1=z1
if button2 ==8:
man1()
man2()
if y4>=x4:
L=0
b4=0
b3=0
fy2()
feng(c2)
else:
L=2
b3=6#b3+6//27
b4=4#b4+4//28
fy2()
feng(c2)
if button1 ==16:
if x<29:
x = x+1
if button1 ==32 :
if x>0:
x = x-1
if button1 ==64:
man1()
man2()
if y4>=x4:
R=0
a1=0
a2=0
a3=0
a4=0
ct1()
delay(20)
gj1()
feng(c1)
for k in range(x11,x13+1):
if button2 ==8:
z2=z2
else:
if k == y3:
z2=z2-1
elif k == y7:
z2=z2-0.5
else:
z2=z2
else:
R=-2
a1=-4
a2=-6
a3=-8
a4=-10
ct1()
delay(20)
gj1()
man1()
feng(c1)
for i in range(x13,x11+1):
if button2 ==8:
z2=z2
else:
man2()
if i == y3:
z2=z2-1
elif i == y7:
z2=z2-0.5
else:
z2=z2
if button1 ==128:
man1()
man2()
if y4>=x4:
R=0
a1=0
a2=0
fy1()
feng(c2)
else:
R=-2
a1=-4
a2=-6
fy1()
feng(c2)
tm1.show(xx1)
tm2.show(xx2)
else:#对输赢进行判断和显示
if z1>z2:
tm1.show(str(0))
tm1.leds(0)
tm2.leds(0xFF)
d.text("NIMn", 0, 0, 1)
d.show()
delay(500)
else:
tm2.show(str(0))
tm1.leds(0xFF)
tm2.leds(0)
d.text("NIMn", 0, 0, 1)
d.show()
delay(500)
0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复