Python:猜拳游戏项目(excel形式保存信息)

2022-07-27,,,,

项目功能
1.系统生成随机的石头剪刀布,玩家输入石头剪刀布
2.因为玩家可能会输入shitou st这样的输入,需要格式化成合理输入
3.进行石头剪刀布的游戏,输出游戏结果,假设每次可以玩5局
4.将游戏结果写入到excel中,包括系统出拳,玩家出拳,游戏结果,目前得分
5.游戏有欢迎信息(欢迎来到游戏)和用户交互(游戏剩余次数)
6.如果游戏的得分达到0分,游戏也会结束
7.在开始游戏的时候要求用户输入玩家姓名,会创建于玩家姓名同名的sheet页
8.如果玩家已经存在,则输出欢迎回来,您目前的积分为:xx分
9.如果玩家不存在,则输出欢迎来到游戏,您目前有5个积分
10.当是老玩家,游戏积分为0分,则提示用户充值,充值1元2积分

代码如下

import openpyxl
import random
class excel:
    def __init__(self,filename,sheetname):#读某个单元格的值
        self.file = openpyxl.load_workbook(filename)
        self.sheet = self.file[sheetname]
        self.name=filename

    def write(self, sheet, data,num):#将数据以列表形式写入
        sheet = self.file[sheet]
        for i in range(1, len(data) + 1):
            sheet.cell(num,i).value = data[i-1]
        self.file.save(self.name)

def formatx(indata):
    if indata=='shitou' or indata=='shi tou' or indata=='st':
        indata = '石头'
    elif indata=='bu'   or indata=='b u':
        indata = '布'
    elif indata=='jiandao' or indata=='jd':
        indata='剪刀'
    elif indata=='石头' or indata=='布' or indata=='剪刀':
        pass
    return indata

def getscore(name):
    wb = openpyxl.load_workbook('first.xlsx')
    sheet = wb[name]
    maxrow = sheet.max_row
    maxcol = sheet.max_column
    score = sheet.cell(maxrow, maxcol).value
    if score=='积分':
        score = 5
        print("欢迎来到游戏")
    else:print("欢迎回来游戏")
    return score

def login(name):
    wb = openpyxl.load_workbook('first.xlsx')
    names = wb.sheetnames
    if name not in names:
        wb.create_sheet(name)
        sheet = wb[name]
        sheet.cell(1,1).value='电脑'
        sheet.cell(1, 2).value = '玩家'
        sheet.cell(1, 3).value = '结果'
        sheet.cell(1, 4).value = '积分'
        wb.save('first.xlsx')

if __name__=="__main__":
    name = input('请输入您的名字:')
    login(name)
    score = getscore(name)
    print("积分{}".format(score))
    if score<=0:
        print('请充值:')
        money = int(input('请输入充值金额'))
        score += money*2
    opt = excel('first.xlsx', name)
    for num in range(1,6):
        compute = random.choice(['石头','剪刀','布'])
        player = input('请输入猜拳的内容:')
        player=formatx(player)
        if player==compute:
            result = [compute,player,'平局',score]
            print('电脑出拳:{},玩家出拳:{},游戏结果:{}'.format(compute,player,result[2],score))
            opt.write(name, result,num+1)
        elif (player=='石头' and compute=='剪刀') or (player=='剪刀' and compute=='布') or player=='布' and compute=='石头':
            score+=1
            result = [compute, player, '玩家胜利',score]
            print('电脑出拳:{},玩家出拳:{},游戏结果:{}'.format(compute, player, result[2],score))
            opt.write(name, result,num+1)
        else:
            score-=1
            result = [compute, player, '玩家失败',score]
            print('电脑出拳:{},玩家出拳:{},游戏结果:{}'.format(compute, player, result[2],score))
            opt.write(name, result,num+1)
        if score<=0:
            break
        print('游戏剩余次数:{}'.format(5-num))
    print("游戏结束")

本文地址:https://blog.csdn.net/weixin_51322568/article/details/110286140

《Python:猜拳游戏项目(excel形式保存信息).doc》

下载本文的Word格式文档,以方便收藏与打印。