Python实现带Excel记录功能的猜拳游戏

月亮慢半拍
月亮慢半拍
发布时间:2026-10-14 | 4人浏览过
原创

本文分享一个基于Python的猜拳游戏实现,利用openpyxl库将游戏过程、双方出拳及结果实时写入Excel文件。项目支持多用户独立Sheet管理,自动识别老玩家并显示历史积分,具备输入格式化、胜负判定及积分扣减机制,适合Python初学者学习文件操作与逻辑控制。

Python猜拳游戏项目概述

本文分享一个使用Python编写的猜拳游戏完整代码示例,供读者参考学习。

核心功能需求分析

该项目主要实现以下功能:

1. 系统随机生成石头、剪刀或布,玩家通过输入对应指令进行对战。 2. 针对玩家可能输入的如'shitou'、'st'等非标准格式,程序需进行标准化处理,转换为规范的中文指令。 3. 执行石头剪刀布的对战逻辑,输出每局结果,默认每轮游戏包含5局对战。 4. 将每局的系统出拳、玩家出拳、胜负结果及当前累计得分写入Excel文件中。 5. 游戏启动时显示欢迎信息,并在每局结束后提示剩余游戏次数。 6. 当玩家积分降至0分时,游戏强制结束。 7. 游戏开始前要求输入玩家姓名,系统会创建以该姓名命名的Excel Sheet页。 8. 若检测到玩家姓名已存在,则提示“欢迎回来”并显示其当前累计积分。 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("游戏结束")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表