In [2]:
#点字成诗游戏完整代码
import random
def rules():
print('初始分数十分,根据给出的十二个字每猜出一句诗即可得十分,满一百即为获胜。')
def gameover():
print('退出游戏')
def team():
print('乱世一人舞倾情之作。。。')
def win():
print('恭喜你,赢得了胜利!')
def lose():
print('怎么办,一不小心输了。。。')
def get_ch_table(line):
ch_table = []
for ch in line:
if ch not in ch_table:
ch_table.append(ch)
return ch_table
def poem_robot(file_name):
with open(file_name) as fh:
text = fh.read()
poems = text.split() #这里的文件是poems.txt,只有换行符,没有逗号
poem = random.choice(poems)
chs = get_ch_table(text.replace(r'\n',''))
guess_ch_table = [ch for ch in poem]
while len(guess_ch_table) < 12 :
ch = random.choice(chs)
if ch not in guess_ch_table:
guess_ch_table.append(ch)
random.shuffle(guess_ch_table)
for i in range(12): #控制乱序的字的输出格式
print(guess_ch_table[i],end=' ')
print()
return poem #自己出题,返回正确答案???
def gamebegin():
filename = r'D:\python\poems.txt'
score = 10
while score >0:
real_poem = poem_robot(filename)
answer_poem = input('请输入猜测诗句,回车结束,直接回车表示退出游戏')
if answer_poem == real_poem:
print('答对了,加十分')
score += 10
print('你当前的分数是:',score)
print()
if score == 100:
win()
print()
return
elif answer_poem == '': #????
print('退出游戏')
print('你最后的分数是:',score)
break
else:
score -= 10
print('答错了,减十分')
print('成语其实是:',real_poem)
print('你当前的分数是:',score)
print()
else:
lose()
print()
return
def menu():
print('''=======游戏菜单=======
1.游戏玩法说明;
2.开始游戏;
3.退出游戏;
4.制作团队;
=======游戏菜单=======''')
def main():
while 1:
menu()
choice = int(input('请输入您的选择:')) #输入的不是数字的时候,仍然有问题。。。
if choice == 1:
rules()
elif choice == 2:
gamebegin()
elif choice == 3:
gameover()
break
elif choice == 4:
team()
else:
print('不好意思,输入错误,请重新选择')
#if __name__ == '__main()__':
# main()
main()